12.6 Built-In PHP Functions

PHP has a number of built-in functions. We sample a few, but there is no way we can cover all of them in this overview. See the documents (www.php.net/manual/en/funcref.php) or a suggested book for a complete list.

12.6.1 Important Functions

Let's start with two important functions: print() and die() .


This function outputs a string that ends up being displayed in the browser. For example:

 print "<h1>PHP Results</h1>";  print "$name : $age : $result[0]";  print "<hr>"; 

The print() function is essentially the same as the echo statement. These two do the same thing ”they both produce the text "hello, world!":

 echo "hello, world!";  print "hello, world!"; 

PHP also has the die() function, which outputs a string and then exits the script. It is usually used to output an error message when something unexpected happens. For example:

 if ($name == "") {      die("the name field was not entered in the form");  } 

12.6.2 Array Functions

PHP has a number of functions that can be used to manipulate arrays. Many of these have some similarities to Perl array functions ”for instance, there are functions to push onto PHP arrays ( array_push() ), reverse an array ( array_reverse() ), sort an array ( sort() , rsort() , and others), and many more.

The following is a description of some of the array functions. For an exhaustive discussion, see the documents.

array_push() , array_pop() , array_unshift() , and array_shift()

These four functions are similar to the Perl functions push() , pop() , unshift() , and shift() , as discussed in Chapter 4.

To add elements to the right side, use array_push() . Use array_pop() to remove the rightmost element.

To add elements to the left side, use array_unshift() . Use array_shift() to remove the leftmost element. Here is an example:

 <?      $languages = array("C", "C++", "Java");      $lang1 = array_pop($languages);      // $lang1 is: "Java"      // $languages is now: ("C", "C++")      array_push($languages, "Perl");      // $languages is now: ("C", "C++", "Perl")      $lang2 = array_shift($languages);      // $lang2 is: "C"      // $languages is now: ("C++", "Perl")      array_unshift($languages, "PHP", "Python");      // $languages is now: ("PHP", "Python", "C++", "Perl")  ?> 
sort() and rsort()

These functions sort an enumerated array in ASCII ascending order ( sort() ) and ASCII descending order ( rsort() ):

 $names = array("Ryan", "Christian", "Madeline", "Reegen");  sort($names);  // $names is now: ("Christian", "Madeline", "Reegen", "Ryan")  $numbers = array(10, 3, 49, 4, 1, 30, 22);  rsort($numbers);  // $numbers is now: (49, 30, 22, 10, 4, 3, 1) 

As you can see, these functions sort numerically if the elements are numeric. You can force these functions to sort in a specific way by using either SORT_REGULAR , SORT_NUMERIC , or SORT_STRING as the second argument:

 rsort($numbers, SORT_STRING); 
asort() , arsort() , ksort() , and krsort()

These functions sort associated arrays. The functions asort() and arsort() sort by value in ascending or descending order, respectively. The functions ksort() and krsort() sort by key in ascending or descending order, respectively:

 $person = array(      "firstname" => "John",      "lastname"  => "Doe",      "phone"     => "555-1234",      "address"   => "123 Main St.",      "city"      => "Chicago",      "state"     => "IL"  );  asort($person);  // ascending by value  // $person is now: "address"   => "123 Main St."  //                 "phone"     => "555-1234"  //                 "city"      => "Chicago"  //                 "lastname"  => "Doe"  //                 "state"     => "IL"  //                 "firstname" => "John"  arsort($person);  // descending by value  // $person is now: "firstname" => "John"  //                 "state"     => "IL"  //                 "lastname"  => "Doe"  //                 "city"      => "Chicago"  //                 "phone"     => "555-1234"  //                 "address"   => "123 Main St."  ksort($person);  // ascending by key  // $person is now: "address"   => "123 Main St."  //                 "city"      => "Chicago"  //                 "firstname" => "John"  //                 "lastname"  => "Doe"  //                 "phone"     => "555-1234"  //                 "state"     => "IL"  krsort($person);  // descending by key  // $person is now: "state"     => "IL"  //                 "phone"     => "555-1234"  //                 "lastname"  => "Doe"  //                 "firstname" => "John"  //                 "city"      => "Chicago"  //                 "address"   => "123 Main St." 

The function count() returns the number of elements of its argument. If it is passed the value of a scalar variable, it returns 1. If it is passed the value of an array, it returns the number of things in the array:

 $nums = array(2,4,6,8);  echo count($nums);  // echoes 4  $stuff = array(      "one"   => "first",      "two"   => "second",      "three" => "third"  );  echo count($stuff);  // echoes 3  $name = "John Doe";  echo count($name);  // echoes 1 
array_keys() and array_values()

These return the keys and the values of an associative array, like the Perl functions keys() and values() :

 $person = array(      "firstname" => "John",      "lastname"  => "Doe",      "phone"     => "555-1234",      "address"   => "123 Main St.",      "city"      => "Chicago",      "state"     => "IL"  );  $p_keys = array_keys($person);  // $p_keys is: ("firstname", "lastname", "phone", "address",  //              "city", "state")  $p_values = array_values($person);  // $p_values is: ("John", "Doe", "555-1234", "123 Main St.",  //                "Chicago", "IL")  $numbers = array(2, 4, 6, 8);  $n_keys = array_keys($person);  // $n_keys is:  (0, 1, 2, 3)  $n_values = array_values($person);  // $n_values is: (2, 4, 6, 8) 

This function returns the values of its arguments in reverse order. It does not return the keys or indices. As an example:

 $person = array(      "firstname" => "John",      "lastname"  => "Doe",      "phone"     => "555-1234",      "address"   => "123 Main St.",      "city"      => "Chicago",      "state"     => "IL"  );  $new_person = array_reverse($person);  // $new_person is: ("IL", "Chicago", "123 Main St.", "555-1234",  //                  "Doe", "John")  $numbers = array(2, 4, 6, 8);  $new_numbers = array_reverse($numbers);  // $numbers is: (8, 6, 4, 2) 

12.6.3 String Functions

PHP has a multitude of string functions. Here are but a few of them. There are far too many to talk about in detail, but keep this idea in mind: If you want to process text, there is probably a built-in function to do what you want to do.


PHP's printf() outputs a string that ends up being displayed in the browser. It works much like C's (and Perl's) printf() function:

 printf "Integer: %d, Float: %6.2f", $i, $f; 

PHP also has the sprintf() function, which returns a string in the format specified.


The join() function joins array elements into a string:

 $array = array(2, 4, 6, 8);  $string = join(":", $array);  // $string is now "2:4:6:8" 

This function returns a portion of a string from a character index ( zero-based ), for a number of characters :

 $string = "hello, world!";  $substring = substr($string, 2, 6);  // $substring is "llo, w" 

The trim() function removes whitespace from each end of a string and returns the result:

 $string = " hello, world!\n\n\n";  $string2 = trim($string);  // $string2 is "hello, world!" 

12.6.4 Other Functions

PHP has a large number of built-in functions. There is not enough space in this chapter to discuss them all. See the docs for more information (www.php.net/manual/en/funcref.php). Another helpful place to start that shows all the functions is www.php.net/quickref.php.

Open Source Development with Lamp
Open Source Development with LAMP: Using Linux, Apache, MySQL, Perl, and PHP
ISBN: 020177061X
EAN: 2147483647
Year: 2002
Pages: 136

Similar book on Amazon

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