How to work in functions PHP?

If there is any task in your program which you train to execute repeatedly then instead of writing the code for that task at a different place in the program you can create a task and whenever you want to execute that task You can call that function at a different place if you need to.

By doing this, your time is also saved and computer memory is also saved, as well as your program also becomes short and readable, this is called code re-usability because you can use the same code at different places.

Syntax:-

<?php

    function functionName(){
        Statement;
    }

    functionName();
    
?>

Example:-

<?php
	function functionName(){
		echo "This is PHP function <br>" ;
	}

        function newFunction(){
   
		echo "This is New PHP function <br>" ;
	}

	functionName();
        functionName();
        echo "Hi this is an example <br>";
        newFunction();
        newFunction();
?>

Output:-

PHP Functions with Parameters

PHP gives you the option of passing your parameters inside a function so that you can pass more parameters.

<?php

    function functionName(parameter1,parameter2){
        Statement;
    }

    functionName(argument1,argument2); 
    
?>

Example:-

<?php
	function sum($a, $b){
        $sum = $a + $b; 
		echo "This in parameter function $sum" ;
	}

	sum(10,20);
?>

Output:-

Related Posts

PHP explode() Function Example Tutorial

A PHP explode function example is given in this article. I will demonstrate how to use PHP’s explode string to array function. This is a basic example…

How to Convert Array to String in PHP?

We will cover the example of converting an array to a string in PHP in this lesson. We’ll examine an example of converting an array to a…

How Can I Convert a String in PHP Into an Array?

We will go over how to transform a string into an array in PHP in this little tutorial. Describe how to convert a string value into an…

How to Use the array_merge() Function in PHP?

We will go over the simple and quick method to use the PHP array_merge() function example in this brief tutorial. Let’s talk about the PHP example of…

How to Merge Two Arrays in PHP?

I explained simply how to merge two arrays in PHP Laravel. you will learn how to append two arrays in PHP. you will learn how to merge…

How to Get the First 5 Elements of Array in PHP?

Here, I will show you the php array to get the first 5 elements. let’s discuss about php array getting 5 first element values. you can see…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x