1. Computing
Send to a Friend via Email

Learn PHP - All About PHP Functions

By

1 of 5

PHP Functions
A function is something that performs a specific task. People write functions if they plan on doing the same task over and over again. This allows you to only write the code once and save a lot of time and space.

Although in the next few pages we will lean how to write our own functions, PHP has several functions that already exist for us to use. Although they all have different uses, all functions are phrased as: name(argument). The name being the name of the function, and the argument being the value(s) it is using.

Here are some examples of functions already in PHP:

 <?php 
 $a = abs(-.43);
 $b = sqrt(16); 
 $c = round(12.3); 
 print "The absolute value of -.43 is " . $a . "<br>"; 
 print "The square root of 16 is " . $b . "<br>"; 
 print "12.3 rounded is " . $c . " and 12.5 rounded is " . round(12.5); 
 ?> 

This gives an example of three functions; absolute value, square root, and rounding. As you can see you can use the function right in the print statement or you can assign it to a variable. A list of functions can be found here.

  1. About.com
  2. Computing
  3. PHP / MySQL
  4. Learn PHP
  5. How to write functions in PHP

©2014 About.com. All rights reserved.