Some PHP Predefined Functions and Arrays
Basics of PHP – Part 11
Forward: In this part of the series, we look at some predefined functions and arrays.
By: Chrysanthus Date Published: 28 Jul 2012
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at firstname.lastname@example.org. That is, contact me for the slightest problem you have about what you are reading.
Some Predefined Math Functions
We look at some predefined math functions first before we look at predefined arrays. There are many predefined functions; we shall talk only about some predefined math functions in this article. After that you will know how to use other predefined functions.
Some Math Functions
The mt_rand Function
The mt_rand function returns a random integer for you. The syntax is
int mt_rand ( int $min , int $max )
Try the following code:
$myRandom = mt_rand(20, 80);
You should have received a random integer between 20 and 80. From the syntax, the function will generate a random integer between (int $min) minimum and (int $max) maximum. As seen from the syntax, the returned value is an integer. As seen from the parentheses of the syntax, the arguments are also integers. If you do not put any argument, that is, you type,
then the return integer will be any number from zero upward.
The round function rounds a float value to a specified precision (number of digits after the decimal point). This is the syntax:
float round ( float $val [, int $precision ] )
So the function takes a float value, $val and returns a slightly different float value. In a PHP syntax, anything in square brackets is optional. In this syntax, it means you may or may not give a number for precision. Well, if you have the float value, 46.56923 to round to a precision of 2 (that is 2 decimal places), you would have the result of 46.57. Try the following code:
$myRound = round(46.56923, 2);
The precision number is optional. If you do not put the precision number, PHP will use a value of zero for the precision. In other words, zero is the default precision number. When the precision number is zero, the number is rounded to its integer. Try the following code:
$myRound = round(46.56923);
Your result should be 47 .
The sqrt Function
The sqrt function takes a number and returns the square root. The syntax is:
float sqrt ( float $arg )
Try the following code:
$mySqrt = sqrt(25);
The result should be 5.
There are other math functions that can do things like return the sine and cosine of an angle. You should consult some reference document for the other math functions.
We shall look at two predefined arrays. You submit a web form with either the HTTP POST or GET method. Before we go into the details, let us look at a loop statement that works only with arrays. It is called the foreach statement.
A simple syntax of the foreach statement is
forach ($arrayName as $myKey => $myValue)
Read through the syntax. This means that for each element in the array, beginning from the first to the last, the key will be held by $myKey and the corresponding value will be held by $myValue. $myKey and $myValue are variables whose names you make. You can give them any names.
Read and try the following code:
$myArr = array();
$myArr["foo"] = 13;
$myArr = "bar";
$myArr["man"] = 23;
$myArr = "woman";
$myArr["boy"] = "man tomorrow";
foreach ($myArr as $myKey => $myValue)
echo "$myKey : $myValue <br />";
Remember, when a string is in double quotes (not single quotes) the variables in the string will be replaced by their values. So, the variables $myKey and $myValue of the echo string will be replaced by their values. In this string, the colon (:) is just a character like any other character, that will be printed when it is in a string. The <br /> is the line break HTML tag that will be sent as well to the browser whether it is in a double quote or single quote string; at the browser it has its effect.
When a form is submitted with the HTTP POST method to a PHP file, the predefined PHP $_POST variable of the PHP interpreter, collects all the name/value pairs of the controls of the form. The $_POST array is initially empty. When the PHP file receives the form data set, the name/value pairs of the form controls are fitted into the array. For each control, the name becomes the key for an array element and the value of the control becomes the value for the corresponding element. You can then write PHP code to do whatever you want to do in the file, with the name/value control pairs, now the key/value array pairs.
Let us look at an example. In this pedagogic example, the PHP code simply reads the key/value pairs and sends them back to the web page as strings. Read and try this code following the instructions given below the code.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<form method="post" action=myFile.php>
First Name <input type="text" name="FirstName"><br /><br />
Last Name <input type="text" name="LastName"><br /><br />
Email <input type="text" name="email"><br /><br />
Message <br />
<textarea name="msg" rows="3" cols="30"></textarea><br />
<br /><br />
foreach ($_POST as $myKey => $myValue)
echo "$myKey : $myValue <br />";
Type this file and save it with the name myFile.php . Start your browser. Type the address, http://localhost/myFile.php , in the address bar and click Go. You should see the form and its submit button. The form will be blank. Fill it with any text of your choice. Click the Submit button. You should see the name/value pairs below the submit button.
The $_POST and $_GET are called predefined variables by the inventors. They are actually predefined arrays. The $_GET array works in the same way as the $_POST array, but for the HTTP GET method.
We have seen some predefined functions and some predefined arrays in PHP. Let us stop here and continue in the next part of the series.
Related LinksMajor in Website Design
Web Development Course