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

Web Technologies-I



                   Notes         by the function. A function’s documentation will tell you what parameters the function expects
                                 and what values you can expect to be returned.
                                 Here are some examples of functions:
                                 // strlen( ) is a built-in function that returns the length of a string $length = strlen(“PHP”); //
                                 $length is now 3 // sin() and asin( ) are the sine and arcsine math functions $result = sin(asin(1));
                                 // $result is the sine of arcsin(1), or 1.0 // unlink( ) deletes a file $result = unlink(“functions.
                                 txt”); // false if unsuccessful.
                                 In the first example, we give an argument, “PHP”, to the function strlen( ), which gives us the
                                 number of characters in the string it is given. In this case, it returns 3, which is assigned to the
                                 variable $length. This is the simplest and most common way to use a function.
                                 The second example passes the result of asin(1) to the sin( ) function. Since the sine and arcsine
                                 functions are reflexive, taking the sine of the arcsine of any value will always return that same value.
                                 In the final example, we give a filename to the unlink( ) function, which attempts to delete the
                                 file. Like many functions, it returns false when it fails. This allows you to use another built-in
                                 function, die( ), and the short-circuiting property of the logic operators. Thus, this example might
                                 be rewritten as:
                                 $result = unlink(“functions.txt”) or die(“Operation failed!”);

                                 The unlink( ) function, unlike the other two examples, affects something outside of the parameters
                                 given to it. In this case, it deletes a file from the filesystem. All such side effects of a function
                                 should be carefully documented.
                                 PHP has a huge array of functions already defined for you to use in your programs. Everything
                                 from database access, to creating graphics, to reading and writing XML files, to grabbing files
                                 from remote systems can be found in PHP’s many extensions.


                                                Function name can start with a letter or underscore “_”, but not a number!


                                 Self Assessment

                                 True or False:

                                    1.  strlen () function is used to compare two strings.
                                       (a)  True                       (b)  False

                                    2.  Built-in and user defined one the two basic types of PHP functions.
                                       (a)  True                       (b)  False

                                    3.  Function name can start with a number.
                                       (a)  True                       (b)  False

                                 4.3 Variable Scope

                                 The scope of a variable is the context within which it is defined and can be used.

                                 The variables used in PHP functions can be of three types: locals, global and statics. Any variable
                                 defined inside a function is by default limited to the local function scope, is available only in the
                                 code within that function.




        80                                LOVELY PROFESSIONAL UNIVERSITY
   81   82   83   84   85   86   87   88   89   90   91