PHP functions

a php function are used to encapsulate code that will be executed when 'someone' calls it.

php functions are self-contained blocks of code that perform a specified task.

a user defined function declaration starts with the word "function":


         function   function_name(){    php valid code put here   }

in php language - a valid function name starts with a underscore or letter, followed by any number of letters, numbers or underscores.

note : a function names are case-insensitive.

note :- the length of a complete program can be reduced by using functions at appropriate places.

note :- it is easy to locate and isolate a faulty function for further investigations.

in the example below, we create a function named 'max()'.

               function   maxv(){   //beginning of the function
                                 $a = 20 ;
                                 $b = 30 ;    
                                 if( $a > $b){  echo "max value  a = $a" ; }
                                 else { echo "max value  b = $b" ; }
                    }  // closing of the function             
                       maxv();   // call the function

PHP - Function arguments

information may be passed to functions via the argument argument is just like a variable.

"arguments are specified after the function name, inside the parentheses, separating each argument by commas. the arguments are evaluated from left to right."

an argument is a value that you pass to a function, and a parameter is the variable within the function that receives the arguments.

the below example has a function with one argument ($name). when the display() function is called, we also pass a name, and the $name is used inside the function.

                function  display( $name ){
                                           echo  " my name is $name";
                                           echo  "<br/>";
                                        }   //closing the function
    display( "sandeep kumar nehra" ); // call the function and pass the name
    display( "vinod kumar" );
    display( "ankit kumar nehra" );   

the following example has a function with more then one arguments.

                function  display( $firstname, $lastname){
                                           echo  " my name is $firstname  $lastname";
                                           echo  "<br/>";
                                        }   //closing the function
    display( "software", "developer" ); // call the function and pass the info
    display( "web", "developer" );
    display( "search engine", "optimization" );   

PHP Function Default value

the default value must be a constant expression, not a class member or a function call.

when using default arguments, any defaults should be on the right side of any non-default arguments.

             function  show( $name = "w3webtutorial" ){
                                               echo "this web apps $name";
                                               echo "<br/>";
                         show(); //call function without argument then use default parameter
                         show( "you can develop web site easily" );
             function  user( $fname, $lname = "how you develop this"){
                                                         echo "this web apps $fname $lname";
                                                         echo "<br/>";
                         user("w3web", "tutorial");

PHP Function Returning values

values are returned by using the optional return statement. a function can't return multiple values.

note : if the return is omitted the value null will be returned.

             function  multiply( $x, $y ){  $z = $x * $y ;
                                                    return  $z;
              echo  "20 * 30" . multiply(20, 30) . "<br/>";
              echo  "40 * 50" . multiply(40, 50) . "<br/>";

$o = multiply(2, 3);  //if a function return a value, you can assign the value to a variable
             function  create_array(){  return  array( 20 ,30, 40, 50, 60); }
var_dump( create_array() );// you can also pass the return value directly to another function.                                                         

PHP anonymous functions

a php function which have no specified name.

                 $method = function( $name, $a ){ 
                                         echo "this web apps $name"; 
                                         echo "<br/> ability = $a <br/>";                    
                            $method("bill gates", "business magnate investor");
                            $method("steve jobs", "apple founder");

PHP Recursive Functions

the recursive functions in php language is one that calls itself, either directly or in a cycle of function calls.

  <?php     function  recursive (){ static $x = 1;

                                              if( $x < 10){  echo  "x value = $x++"; 
                                                      recursive(); //itself method call 
                                                    else { echo "end the function"; }}