Page 84 - Open Soource Technologies 304.indd
P. 84

Web Technologies-I



                   Notes                Example:

                                 $length = strlen(“Pradip”);
                                 strlen is a standard PHP function that returns the length of a string. Therefore, $length is assigned
                                 the length of the string “Pradip”: five.
                                 Here’s an example of a function call being used as a function argument:
                                 $length = strlen(strlen(“Pradip “));

                                 You probably already guessed the result of this example. First, the innerstrlen(“Pradip “) is
                                 executed, which results in the integer 4. So, the code simplifies to $length = strlen(5); strlen ()
                                 expects a string, and therefore (due to PHP’s magical auto conversion between types) converts
                                 the integer 5 to the string “5”, and thus, the resulting value of $length is 1, the length of “5”

                                 There are basically two types of PHP functions. The first type is the built-in function. These
                                 functions are already part of the PHP language and therefore you do not need to write them
                                 yourself. To use these built-in functions you just add a function call to your program. A function
                                 call is a piece of code that tells your program to “call in” the built-in function whenever you need
                                 it. The second type of function is the user defined function. These are the functions that you write
                                 yourself. Then, after you write the function, you can call it into your program any time by coding
                                 a function call.
                                 The general way of defining a function is

                                 function function_name (arg1, arg2, arg3, …)
                                  statement list
                                   }

                                 To return a value from a function, you need to make a call to return expr inside your function.
                                 This stops execution of the function and returns expr as the function’s value.

                                 The following example function accepts one argument, $x, and returns its square:
                                     function square ($x)
                                    {
                                     return $x*$x;

                                         }
                                 After defining this function, it can be used as an expression wherever you desire.

                                        Example:
                                 print ‘The square of 5 is ‘. square (5);

                                 4.1 Defining a Function


                                 The PHP syntax to define a function is rather easy to remember:
                                 Function function_name($par_1, ... , $par_n)

                                 {
                                 php_instructions
                                 }




        78                                LOVELY PROFESSIONAL UNIVERSITY
   79   80   81   82   83   84   85   86   87   88   89