PHP function details with examples

PHP function details with examples

PHP function details with examples

Now the thing we will discuss is the function. Before that, I will know what is the function?
 A function is a block of code that performs a specific function.


 There are two types of functions, one is the built-in function and the other is the user-defined function.
 Now let's talk about user-defined functions.
 There are four types of user-defined fashions:

  1. Function with no argument and no return value
  2. Function with no argument and return value
  3. Function with argument and no return value
  4. Function with argument and return value


1 means that after we create a function, we will not need to pass any argument or parameter when the function is called and will not return any value.


2 This means that after we create a function, no argument or parameter needs to be passed when the function is called, but the function will return a value, be it a string or an integer number or a boolean value. True.


 3 This means that after we create a function, we can pass one or more arguments or parameters when the function is called but the function will not return any value.


 4 This means that after we create a function, we can pass one or more arguments or parameters when the function is called and the function can return a value.


This is the concept of function. Now look at the direct example


Function with no argument and no return value


/*
* Function with NO Arguments and NO Return Value
*/

function showMessage(){
echo "Hi,How are you?";
}

// call the function showMessage()
showMessage();



Here we have created a function called showMessage.The function will not accept any arguments.
When the function is called, between the opening bracket and the closing bracket of the function

If there is something everything will be executed.

This is the only one whenever I call the showMessage function

Will line. Because we use the echo built-in function in the middle of the showMessage function

I have printed the string.



Function with no argument and return value



/*
* Function with NO arguments and Return value
*/

function showMessageThree(){
return "Hello! How are you?";
}

//calling the function and echo it to display the return value

echo showMessageThree();

Secondly, we have defined another function called showMessageTwo where we have passed as a variable parameter named $ name. Then when I call from a function, I have to pass a value, be it directly in the form of a string, or by storing the value in a variable, I can pass the variable as a parameter in the middle of the function. As a result, the showMessageTwo function will accept that variable and print it here.


Function with argument and no return value




/*
* Function with Arguments and No Return Value
*/
function showMessageTwo($name){

echo "Hi ".$name." ! How are you?";
}
// set a string value
$person_name = "Jewel Chowdhury";
// call the function and pass the arguments
showMessageTwo($person_name);

Thirdly, the function we have defined as showMessageThree will not take any argument or parameter but will return a value. When the function is called, it has to be called with an echo, then the return value in the middle of the function will be printed.


Function with argument and return value



/*
* Function with Argument and Return value
*/


function showMessageFour($name){
return "Hello! ".$name." How are you?";
}

$name = "Jewel Chowdhury";

// calling the function and echo it to display

echo showMessageFour($name);



Fourth showMessageFour is a function that can only accept one parameter and at the same time the function returns a value. Hope you understand. If there is a problem to understand, then comment and tell your problem.






You may like these posts