Broad Network


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

Introduction

This is part 11 of my series, Basics of PHP. We have seen how to defined functions and create arrays. PHP comes with some predefined variables, predefined arrays and predefined functions. In this part of the series, we look at some predefined functions and arrays. Predefined functions and arrays are pieces of code that have already been written for you. They are in the interpreter. You do the see the code for the predefined functions and arrays; you just learn how to use them and use them. You use them in the same way that you use your own defined functions and created arrays.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. 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:

    <?php

        $myRandom = mt_rand(20, 80);
        echo $myRandom;

    ?>

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,

    mt_rand();

then the return integer will be any number from zero upward.

The round Function
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:

    <?php

        $myRound = round(46.56923, 2);
        echo $myRound;

    ?>

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:

    <?php

        $myRound = round(46.56923);
        echo $myRound;

    ?>

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:

    <?php

        $mySqrt = sqrt(25);
        echo $mySqrt;

    ?>

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.

Some Predefined Arrays
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.

foreach Statement
A simple syntax of the foreach statement is

    forach ($arrayName as $myKey => $myValue)
        {
            Statements
        }

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:

    <?php

        $myArr = array();

        $myArr["foo"] = 13;
        $myArr[9] = "bar";
        $myArr["man"] = 23;
        $myArr[5] = "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.

The $_POST Variable
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"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>

    <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 />

        <button type="submit">Submit</button>
    </form>
    <br /><br />

    <?php

            foreach ($_POST as $myKey => $myValue)
                {
                    echo "$myKey : $myValue <br />";
                }

    ?>

</body>
</html>

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 $_GET Variable
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.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message