Functions in PHP


PHP function is a piece of code that can be reused several times It can take input as the logic list and return value. PHP has thousands of built-in functions

In PHP, we can also define functional function, functions within function and recursive function.

Syntax - 

function functionname(){  
//code to be executed  
}  

Example- 

<?php  
function sayHello(){  
echo "Hello PHP Function";  
}  
sayHello();//calling function  
?>  

PHP Function Arguments

PHP supports Call by Value (default), Call by ReferenceDefault argument values and Variable-length argument list

Example- 

<?php  
function sayHello($name){  
echo "Hello $name<br/>";  
}  
sayHello("Sonoo");  
sayHello("Vimal");  
sayHello("John");  
?>  

Call By Reference -

The value value near the function does not modify the actual value by default (value by call). But we can do this by giving recognition as a reference.

By default, the value given to the function is called value. To pass the reference as the value, you must use the ampersand (&) symbol before the logic name.

Example- 

<?php  
function adder(&$str2)  
{  
    $str2 .= 'Call By Reference';  
}  
$str = 'Hello ';  
adder($str);  
echo $str;  
?>  





Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment