Section 5.1. Calling Functions


5.1. Calling Functions

Functions that are built into PHP can be called from any PHP script. When you call functions, you are executing the code inside them, except the code is reusable and more maintainable. One built-in function, shown in Example 5-2, is phpinfo. It returns configuration and technical information about your PHP installation.

Example 5-2. Displaying information about the PHP environment

 <?php     phpinfo(); ?> 

The function helps you diagnose common problems and issues. You may find that this is one of the most helpful places to look when checking to see whether you meet the requirements of a PHP script. Figure 5-2 shows only part of the information contained on this page. If a function call doesn't work, this page helps diagnose whether PHP is compiled with the necessary modules.

Figure 5-2. Information about PHP displayed in the browser


To call a function, write the name of the function followed by an opening parenthesis (, the parameters, and then a closing parenthesis ), followed by a semicolon (;). It would look like this: function_name(parameters);. Function names aren't case sensitive, so calling phpinfo is the same as calling PhpInfo. As shown in Example 5-3, this is what calling a function looks like: md5($mystring);.

Most functions have return values that you'll either use in a comparison or store in a variable. A great place to start is the md5 function. md5 is a one-way hash function used to verify the integrity of a string, similar to a checksum. md5 converts a message into a fixed string of digits, called a message digest. You can then perform a hashcheck, comparing the calculated message digest against a message digest decrypted with a public key to verify that the message was not tampered with. Example 5-3 creates a 128-bit long md5 signature of the string "mystring".

Example 5-3. Creating an md5 signature

 <?php     $mystring = "mystring";     $signature = md5($mystring);     echo $signature; ?> 

Example 5-3 displays:

 169319501261c644a58610f967e8f9d0 

The return value, which is discussed in detail in this chapter, is assigned to the variable $signature, which then displays the output.

The optional raw_output parameter was added in PHP 5.0 and defaults to FALSE for the md5 function. There is no interpretation for raw_output.


A common use for md5 is to verify that a file didn't become corrupt while it was transferring. The file and its md5 signature are compared after they're received. If they match, you know that it's very unlikely that the file's contents were corrupted during transfer. If they're different, you know that the file is corrupt.

This example demonstrates how you can perform a complex process using a function without having to worry about how that process actually does it. This is the real power of functions.



Learning PHP and MySQL
Learning PHP and MySQL
ISBN: 0596101104
EAN: 2147483647
Year: N/A
Pages: 135

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net