PHP – Superglobals ”; Previous Next The PHP parser populates the current script with a number of predefined variables in its global namespace. The predefined variables are known as “PHP superglobals“. Any user defined variable declared outside of any function, method, or class also is a global variable. However, to access it, you need to use the global keyword. In contrast, superglobals are always available anywhere in the PHP script, without mentioning them with the global keyword. Most of the superglobals in PHP are associative arrays, and the web server populates them. Hence, if a script is run in the command-line environment, some of the superglobals may be empty. The list of superglobal variables in PHP includes the following − $GLOBALS $_SERVER $_GET $_POST $_FILES $_COOKIE $_SESSION $_REQUEST $_ENV In this chapter, we will have a brief introduction to these superglobal variables in PHP. In the subsequent chapters, we will discuss these superglobal variables in detail. $GLOBALS It is an associative array of references to all globally defined variables. Names of variables form keys and their contents are values of associative array. $_SERVER All the server and execution environment related information is available in this associative array. PHP versions prior to 5.4.0 contained $HTTP_SERVER_VARS contained the same information but has now been removed. $_GET It is an associative array of variables passed to the current script via query string appended to URL of HTTP request. Note that the array is populated by all requests with a query string in addition to GET requests. A query string is a list of all variables and their values in the form var=val and concatenated by the “&” symbol. The query string itself is appended to the name of PHP script after the “?” symbol. For example, http://localhost/hello.php?first_name=Amar&last_name=Sharma. $_POST It is an associative array of key-value pairs passed to a URL by HTTP POST method that uses URLEncoded or multipart/form-data content-type in request. $HTTP_POST_VARS also contains the same information as $_POST, but is not a superglobal, and now been deprecated. The easiest way to send data to a server with POST request is specifying the method attribute of HTML form as POST. $_FILES The variable $_FILES is an associative array containing items uploaded via HTTP POST method. A file is uploaded when a HTML form contains an input element with file type, its enctype attribute set to multipart/form-data, and the method attribute set to HTTP POST method. $_COOKIE Cookies are text files stored by a server on the client computer and they are kept of use tracking purpose. The superglobal $_COOKIE stores variables passed to the current PHP script along with the HTTP request in the form of cookies. $_SESSION An HTTP session is the time duration between the time a user establishes connection with a server and the time the connection is terminated. During this interval, some data is persistently available across pages in the form of session variables. The $_SESSION superglobal is an associative array of session variables available to the current script. $_REQUEST $_REQUEST is an associative array which is a collection of contents of $_GET, $_POST and $_COOKIE variables. The order of these variables is decided by the values of requests_order and varables_order settings in the “php.ini” file. $_ENV $_ENV is an associative array that stores all the environment variables available to the current script. This array also includes CGI variables in case PHP is running as a server module or CGI processor. Print Page Previous Next Advertisements ”;
Category: php
PHP – Constant Arrays
PHP – Constant Arrays ”; Previous Next It was not possible to declare a constant array before PHP version 5.6. From PHP 5.6 onwards, you can use the “const” keyword to declare a constant array. From PHP 7 onwards, constant arrays can also be formed with define() function. A constant array is an array which cannot be modified after it has been formed. Unlike a normal array, its identifier doesn’t start with the “$” sign. The older syntax for declaring constant array is − const ARR = array(val1, val2, val3); Example <?php const FRUITS = array( “Watermelon”, “Strawberries”, “Pomegranate”, “Blackberry”, ); var_dump(FRUITS); ?> It will produce the following output − array(4) { [0]=> string(10) “Watermelon” [1]=> string(12) “Strawberries” [2]=> string(11) “Pomegranate” [3]=> string(10) “Blackberry” } You can also use the conventional square bracket syntax to declar a constant array in PHP − const FRUITS = [ “Watermelon”, “Strawberries”, “Pomegranate”, “Blackberry”, ]; Example It is not possible to modify any element in a constant array. Hence, the following code throws a fatal error − <?php const FRUITS = [ “Watermelon”, “Strawberries”, “Pomegranate”, “Blackberry”, ]; FRUITS[1] = “Mango”; ?> It will produce the following output − PHP Fatal error: Cannot use temporary expression in write context Constant Arrays PHP 7 Onwards The newer versions of PHP allow you to declare a constant array with define() function. <?php define (”FRUITS”, [ “Watermelon”, “Strawberries”, “Pomegranate”, “Blackberry”, ]); print_r(FRUITS); ?> It will produce the following output − Array ( [0] => Watermelon [1] => Strawberries [2] => Pomegranate [3] => Blackberry ) You can also use the array() function to declare the constant array here. define (”FRUITS”, array( “Watermelon”, “Strawberries”, “Pomegranate”, “Blackberry”, )); Example It is also possible to declare an associative constant array. Here is an example − <?php define (”CAPITALS”, array( “Maharashtra” => “Mumbai”, “Telangana” => “Hyderabad”, “Gujarat” => “Gandhinagar”, “Bihar” => “Patna” )); print_r(CAPITALS); ?> It will produce the following output − Array ( [Maharashtra] => Mumbai [Telangana] => Hyderabad [Gujarat] => Gandhinagar [Bihar] => Patna ) Print Page Previous Next Advertisements ”;
PHP – $GLOBALS
PHP – $GLOBALS ”; Previous Next $GLOBALS is one of the “superglobal” or “automatic global” variables in PHP. It is available in all scopes throughout a script. There is no need to do “global $variable;” to access it within functions or methods. $GLOBALS is an associative array of references to all globally defined variables. The names of variables form keys and their contents are the values of an associative array. Example This example shows $GLOBALS array containing the name and contents of global variables − <?php $var1=”Hello”; $var2=100; $var3=array(1,2,3); echo $GLOBALS[“var1”] . “n”; echo $GLOBALS[“var2”] . “n”; echo implode($GLOBALS[“var3”]) . “n”; ?> It will produce the following output − Hello 100 123 Example In the following example, $var1 is defined in the global namespace as well as a local variable inside the function. The global variable is extracted from the $GLOBALS array. <?php function myfunction() { $var1=”Hello PHP”; echo “var1 in global namespace: ” . $GLOBALS[”var1”]. “n”; echo “var1 as local variable: “. $var1; } $var1=”Hello World”; myfunction(); ?> It will produce the following output − var1 in global namespace: Hello World var1 as local variable: Hello PHP Example Prior to PHP version 8.1.0, global variables could be modified by a copy of $GLOBALS array. <?php $a = 1; $globals = $GLOBALS; $globals[”a”] = 2; var_dump($a); ?> It will produce the following output − int(1) Here, $globals is a copy of the $GLOBALS superglobal. Changing an element in the copy, with its key as “a” to 2, actually changes the value of $a. It will produce the following output − int(2) Example As of PHP 8.1.0, $GLOBALS is a read-only copy of the global symbol table. That is, global variables cannot be modified via their copy. The same operation as above won’t change $a to 2. <?php $a = 1; $globals = $GLOBALS; $globals[”a”] = 2; var_dump($a); ?> It will produce the following output − int(1) Print Page Previous Next Advertisements ”;
PHP – Indexed Array
PHP – Indexed Array ”; Previous Next In PHP, the array elements may be a collection of key-value pairs or it may contain values only. If the array consists of values only, it is said to be an indexed array, as each element is identified by an incrementing index, starting with “0”. An indexed array in PHP may be created either by using the array() function or with the square bracket syntax. $arr1 = array(“a”, 10, 9.99, true); $arr2 = [“a”, 10, 9.99, true]; Each element in the array has a positional index, the first element being at index “0”. The var_dump() function reveals the structured information of these arrays as − array(4) { [0]=> string(1) “a” [1]=> int(10) [2]=> float(9.99) [3]=> bool(true) } We can use the index to traverse the array, fetch the value at a given index or modify the value of an element in place. Traversing an Indexed Array in PHP Any type of PHP loop can be employed to traverse an array. If we want to use a for or while loop, we have to find the number of elements in the array with count() function and use its value as the test condition for the counted for or while loop. Example The following code uses a for loop to list all the elements in an indexed array. <?php $numbers = array(10, 20, 30, 40, 50); for ($i=0; $i<count($numbers); $i++){ echo “numbers[$i] = $numbers[$i] n”; } ?> It will produce the following output − numbers[0] = 10 numbers[1] = 20 numbers[2] = 30 numbers[3] = 40 numbers[4] = 50 You can also use a while or do-while loop to traverse an indexed array. Here too, we need to find the array length with count() function. Example The following code traverses the given indexed array in reverse order − <?php $numbers = array(10, 20, 30, 40, 50); $i = count($numbers)-1; while ($i>=0){ echo “numbers[$i] = $numbers[$i] n”; $i–; } ?> It will produce the following output − numbers[4] = 50 numbers[3] = 40 numbers[2] = 30 numbers[1] = 20 numbers[0] = 10 Accessing the Array Elements Using Index You can access any value from an array using the array[index] syntax. The value at a specific index may be assigned with a new value. The array is thus modified in place. Example The following program fetches the values from an array $arr1 and places them in $arr2 in the reverse order. So the value at 0th position in $arr1 becomes the last value in $arr2. <?php $arr1 = array(10, 20, 30, 40, 50); $size = count($arr1); for ($i=0; $i<$size; $i++){ $arr2[$size-$i-1] = $arr1[$i]; } for ($i=0; $i<$size; $i++){ echo “arr1[$i] = $$arr1[$i] arr2[$i] = $$arr2[$i] n”; } ?> It will produce the following output − arr1[0] = $10 arr2[0] = $50 arr1[1] = $20 arr2[1] = $40 arr1[2] = $30 arr2[2] = $30 arr1[3] = $40 arr2[3] = $20 arr1[4] = $50 arr2[4] = $10 Traversing an Indexed Array Using “foreach” Loop You can also use the foreach loop to iterate through an indexed array. Take a look at the following example − <?php $arr1 = [10, 20, 30, 40, 50]; foreach ($arr1 as $val){ echo “$val n”; } ?> It will produce the following output − 10 20 30 40 50 Note that PHP internally treats the indexed array as an associative array, with the index being treated as the key. This fact can be verified by the var_dump() output of the array. Example We can unpack each element of an indexed array in the key and value variables with foreach syntax − <?php $arr1 = [10, 20, 30, 40, 50]; foreach ($arr1 as $key => $val){ echo “arr1[$key] = $val n”; } ?> It will produce the following output − arr1[0] = 10 arr1[1] = 20 arr1[2] = 30 arr1[3] = 40 arr1[4] = 50 In PHP, an array may be a mix of only values and key-value pairs. PHP assigns the index only to the values without keys. Example In this example, PHP assigns incrementing index to the numbers, skipping the key-value pair appearing in it. <?php $arr1 = [10, 20, “vals” => [“ten”, “twenty”], 30, 40, 50]; var_dump($arr1); ?> It will produce the following output − array(6) { [0]=> int(10) [1]=> int(20) [“vals”]=> array(2) { [0]=> string(3) “ten” [1]=> string(6) “twenty” } [2]=> int(30) [3]=> int(40) [4]=> int(50) } Print Page Previous Next Advertisements ”;
PHP – Strict Typing
PHP – Strict Typing ”; Previous Next PHP is widely regarded as a weakly typed language. In PHP, you need not declare the type of a variable before assigning it any value. The PHP parser tries to cast the variables into compatible type as far as possible. For example, if one of the values passed is a string representation of a number, and the second is a numeric variable, PHP casts the string variable to numeric in order to perform the addition operation. Example Take a look at the following example − <?php function addition($x, $y) { echo “First number: $x Second number: $y Addition: ” . $x+$y; } $x=”10″; $y=20; addition($x, $y); ?> It will produce the following output − First number: 10 Second number: 20 Addition: 30 However, if $x in the above example is a string that doesn’t hold a valid numeric representation, then you will encounter an error. <?php function addition($x, $y) { echo “First number: $x Second number: $y Addition: ” . $x+$y; } $x=”Hello”; $y=20; addition($x, $y); ?> It will produce the following output − PHP Fatal error: Uncaught TypeError: Unsupported operand types: string + int in hello.php:5 Type Hints Type-hinting is supported from PHP 5.6 version onwards. It means you can explicitly state the expected type of a variable declared in your code. PHP allows you to type-hint function arguments, return values, and class properties. With this, it is possible to write more robust code. Let us incorporate type-hinting in the addition function in the above program − function addition(int $x, int $y) { echo “First number: $x Second number: $y Addition: ” . $x+$y; } Note that by merely using the data types in the variable declarations doesn’t prevent the unmatched type exception raised, as PHP is a dynamically typed language. In other words, $x=”10″ and $y=20 will still result in the addition as 30, whereas $x=”Hello” makes the parser raise the error. Example <?php function addition($x, $y) { echo “First number: $x n”; echo “Second number: $y n”; echo “Addition: ” . $x+$y . “nn”; } $x=10; $y=20; addition($x, $y); $x=”10″; $y=20; addition($x, $y); $x=”Hello”; $y=20; addition($x, $y); ?> It will produce the following output − First number: 10 Second number: 20 Addition: 30 First number: 10 Second number: 20 Addition: 30 First number: Hello Second number: 20 PHP Fatal error: Uncaught TypeError: Unsupported operand types: string + int in hello.php:5 strict_types PHP can be made to impose stricter rules for type conversion, so that “10” is not implicitly converted to 10. This can be enforced by setting strict_types directive to 1 in a declare() statement. The declare() statement must be the first statement in the PHP code, just after the “<?php” tag. Example Take a look at the following example − <?php declare (strict_types=1); function addition(int $x, int $y) { echo “First number: $x Second number: $y Addition: ” . $x+$y; } $x=10; $y=20; addition($x, $y); ?> It will produce the following output − First number: 10 Second number: 20 Addition: 30 Now, if $x is set to “10”, the implicit conversion won”t take place, resulting in the following error − PHP Fatal error: Uncaught TypeError: addition(): Argument #1 ($x) must be of type int, string given From PHP 7 onwards, type-hinting support has been extended for function returns to prevent unexpected return values. You can type-hint the return values by adding the intended type after the parameter list prefixed with a colon (:) symbol. Example Let us add a type hint to the return value of the division() function below. <?php declare (strict_types=1); function division(int $x, int $y) : int { return $x/$y; } $x=10; $y=20; $result = division($x, $y); echo “First number: $x Second number: $y Addition: ” . $result; ?> Because the function returns 0.5, which is not of int type (that is, the type hint used for the return value of the function), the following error is displayed − Fatal error: Uncaught TypeError: division(): Return value must be of type int, float returned in hello.php:5 Print Page Previous Next Advertisements ”;
PHP – Break Statement
PHP – Break Statement ”; Previous Next The break statement along with the continue statement in PHP are known as “loop control statements”. Any type of loop (for, while or do-while) in PHP is designed to run for a certain number of iterations, as per the test condition used. The break statement inside the looping block takes the program flow outside the block, abandoning the rest of iterations that may be remaining. The break statement is normally used conditionally. Otherwise, the loop will terminate without completing the first iteration itself. The syntax of break statement is as follows − while(expr){ if (condition){ break; } } The following flowchart explains how the break statement works − Example The following PHP code is a simple example of using break in a loop. The while loop is expected to perform ten iterations. However, a break statement inside the loop terminates it when the counter exceeds 3. <?php $i = 1; while ($i<=10){ echo “Iteration No. $i n”; if ($i>=3){ break; } $i++; } ?> It will produce the following output − Iteration No. 1 Iteration No. 2 Iteration No. 3 An optional numeric argument can be given in front of break keyword. It is especially useful in nested looping constructs. It tells how many nested enclosing structures are to be broken out of. The default value is 1, only the immediate enclosing structure is broken out of. Example The following example has three nested loops: a for loop inside which there is a while loop which in turn contains a do-while loop. The innermost loop executes the break. The number “2” in front of it takes the control out of the current scope into the for loop instead of the immediate while loop. <?php for ($x=1; $x<=3; $x++){ $y=1; while ($y<=3){ $z=1; do { echo “x:$x y:$y z:$z n”; if ($z==2){ break 2; } $z++; } while ($z<=3); $z=1; $y++; } } ?> It will produce the following output − x:1 y:1 z:1 x:1 y:1 z:2 x:2 y:1 z:1 x:2 y:1 z:2 x:3 y:1 z:1 x:3 y:1 z:2 Note that each time the value of “z” becomes 2, the program breaks out of the “y” loop. Hence, the value of “y” is always 1. Print Page Previous Next Advertisements ”;
PHP – Assignment Operators
PHP – Assignment Operators Examples ”; Previous Next You can use assignment operators in PHP to assign values to variables. Assignment operators are shorthand notations to perform arithmetic or other operations while assigning a value to a variable. For instance, the “=” operator assigns the value on the right-hand side to the variable on the left-hand side. Additionally, there are compound assignment operators like +=, -= , *=, /=, and %= which combine arithmetic operations with assignment. For example, “$x += 5” is a shorthand for “$x = $x + 5”, incrementing the value of $x by 5. Assignment operators offer a concise way to update variables based on their current values. The following table highligts the assignment operators that are supported by PHP − Operator Description Example = Simple assignment operator. Assigns values from right side operands to left side operand C = A + B will assign value of A + B into C += Add AND assignment operator. It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A -= Subtract AND assignment operator. It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C – A *= Multiply AND assignment operator. It multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A /= Divide AND assignment operator. It divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A %= Modulus AND assignment operator. It takes modulus using two operands and assign the result to left operand C %= A is equivalent to C = C % A Example The following example shows how you can use these assignment operators in PHP − <?php $a = 42; $b = 20; $c = $a + $b; echo “Addition Operation Result: $c n”; $c += $a; echo “Add AND Assignment Operation Result: $c n”; $c -= $a; echo “Subtract AND Assignment Operation Result: $c n”; $c *= $a; echo “Multiply AND Assignment Operation Result: $c n”; $c /= $a; echo “Division AND Assignment Operation Result: $c n”; $c %= $a; echo “Modulus AND Assignment Operation Result: $c”; ?> It will produce the following output − Addition Operation Result: 62 Add AND Assignment Operation Result: 104 Subtract AND Assignment Operation Result: 62 Multiply AND Assignment Operation Result: 2604 Division AND Assignment Operation Result: 62 Modulus AND Assignment Operation Result: 20 Print Page Previous Next Advertisements ”;
PHP – Arrow Functions
PHP – Arrow Functions ”; Previous Next Arrow functions were introduced in PHP 7.4 version. Arrow functions provide a simpler and more concise syntax for writing anonymous functions. With PHP 7.4, a keyword “fn” has been introduced for defining arrow functions, instead of the conventional use of the “function” keyword. fn (argument_list) => expr There is only one expression after the “=>” symbol, and its value is the return value of the arrow function. The arrow function doesn’t have an explicit return statement. Like in the anonymous function, the arrow function is assigned to a variable for it to be called. Example The following example demonstrates how you can use the arrow function in PHP − <?php $add = fn ($a, $b) => $a + $b; $x = 10; $y = 20; echo ” x: $x y: $y Addition: ” . $add($x, $y); ?> It will produce the following output − x: 10 y: 20 Addition: 30 Using the Arrow Function as a Callback Function We can also use the arrow function as a callback function. Callback functions are used as one of the arguments of another function. The arrow function is executed on the fly and the value of the expression after “=>” becomes the argument of the parent function, which may be either a built-in or a user-defined function. Example In this example, we use an arrow function inside usort() function, a built_in function that sorts an array by values using a user-defined comparison function. <?php $arr = [10,3,70,21,54]; usort ($arr, fn ($x , $y) => $x > $y); foreach ($arr as $x){ echo $x . “n”; } ?> It will produce the following output − 3 10 21 54 70 Accessing Variables from the Parent Scope Arrow functions can automatically access variables from the parent scope. Unlike the anonymous functions, the “use” keyword is not necessary for it to act as a closure. When a variable used in the expression is defined in the parent scope, it will be implicitly captured by-value. <?php $maxmarks=300; $percent=fn ($marks) => $marks*100/$maxmarks; $m = 250; echo “Marks = $m Percentage = “. $percent($m); ?> It will produce the following output − Marks = 250 Percentage = 83.333333333333 Example Arrow functions capture variables by value automatically, even when nested. In the following example, an arrow function is defined in the expression part of another arrow function. <?php $z = 1; $fn = fn($x) => fn($y) => $x * $y + $z; $x = 5; $y = 10; echo “x:$x y:$y n”; echo “Result of nested arrow functions: ” . ($fn($x)($y)); ?> It will produce the following output − x:5 y:10 Result of nested arrow functions: 51 Just like anonymous functions, the arrow function syntax allows arbitrary function signatures, including parameter and return types, default values, variadics, as well as by-reference passing and returning. Print Page Previous Next Advertisements ”;
PHP – Hello World
PHP – Hello World ”; Previous Next Conventionally, learners write a “Hello World” program as their first program when learning a new language or a framework. The objective is to verify if the software to be used has been installed correctly and is working as expected. To run a Hello World program in PHP, you should have installed the Apache server along with PHP module on the operating system you are using. PHP is a server-side programming language. The PHP code must be available in the document root of the web server. The web server document root is the root directory of the web server running on your system. The documents under this root are accessible to any system connected to the web server (provided the user has permissions). If a file is not under this root directory, then it cannot be accessed through the web server. In this tutorial, we are using XAMPP server software for writing PHP code. The default document root directory is typically “C:xampphtdocs” on Windows and “/opt/lamp/htdocs/” on Linux. However, you can change the default document root by modifying the DocumentRoot setting in Apache server’s configuration file “httpd.conf”. While on a Windows operating system, start the Apache server from the XAMPP control panel. Browse to the “htdocs” directory. Save the following script as “hello.php” in it. <?php echo “Hello World!”; ?> Open a new tab in your browser and enter http://localhost/hello.php as the URL. You should see the “Hello World” message in the browser window. A PHP script may contain a mix of HTML and PHP code. <!DOCTYPE html> <html> <body> <h1>My PHP Website</h1> <?php echo “Hello World!”; ?> </body> </html> The “Hello World” message will be rendered as a plain text. However, you can put HTML tags inside the “Hello World” string. The browser will interpret the tags accordingly. In the following code, the “echo” statement renders “Hello World” so that it is in <h1> heading with the text aligned at the center of the page. <?php echo “<h1 align=”center”>Hello World!</h1>”; ?> PHP Script from Command Prompt You can run your PHP script from the command prompt. Let”s assume you have the following content in your “hello.php” file. <?php echo “Hello PHP!!!!!”; ?> Add the path of the PHP executable to your operating system’s path environment variable. For example, in a typical XAMPP installation on Windows, the PHP executable “php.exe” is present in “c:xamppphp” directory. Add this directory in the PATH environment variable string. Now run this script as command prompt − C:xampphtdocs>php hello.php You will get the following output − Hello PHP!!!!! Print Page Previous Next Advertisements ”;
PHP – Maths Functions
PHP â Maths Functions ”; Previous Next To enable mathematical operations, PHP has mathematical (arithmetic) operators and a number of mathematical functions. In this chapter, the following mathematical functions are explained with examples. PHP abs() Function The abs() function is an in-built function in PHP iterpreter. This function accepts any number as argument and returns a positive value, disregarding its sign. Absolute value of any number is always positive. abs( mixed $num) PHP abs() function returns the absolute value of num. If the data type of num is float, its return type will also be float. For integer parameter, the return type is integer. Example Take a look at this following example − <?php $num=-9.99; echo “negative float number: ” . $num . “n”; echo “absolute value : ” . abs($num) . “n”; $num=25.55; echo “positive float number: ” . $num . “n”; echo “absolute value : ” . abs($num). “n”; $num=-45; echo “negative integer number: ” . $num . “n”; echo “absolute value : ” . abs($num) . “n”; $num=25; echo “positive integer number: ” . $num . “n”; echo “absolute value : ” . abs($num); ?> It will produce the following output − negative float number: -9.99 absolute value : 9.99 positive float number: 25.55 absolute value : 25.55 negative integer number: -45 absolute value : 45 positive integer number: 25 absolute value : 25 PHP ceil() Function The ceil() function is an in-built function in PHP iterpreter. This function accepts any float number as argument and rounds it up to the next highest integer. This function always returns a float number as the range of float is bigger than that of integer. ceil ( float $num ) : float PHP ceil() function returns the smallest integer value that is bigger than or equal to given parameter. Example 1 The following code rounds 5.78 to its next highest integer which is 6 <?php $arg=5.78; $val=ceil($arg); echo “ceil(” . $arg . “) = ” . $val; ?> It will produce the following output − ceil(5.78) = 6 Example 2 The following example shows how you can find the next highest integer of 15.05. <?php $arg=15.05; $val=ceil($arg); echo “ceil(” . $arg . “) = ” . $val; ?> It will produce the following output − ceil(15.05) = 16 Example 3 For negative number, it is rounded towards 0. <?php $arg=-3.95; $val=ceil($arg); echo “ceil(” . $arg . “) = ” . $val; ?> It will produce the following output − ceil(-3.95) = -3 PHP exp() Function The exp() function calculates exponent of e that is Euler Number. PHP has a predefined constant M_E that represents Euler Number and is equal to 2.7182818284590452354. Hence, exp(x) returns 2.7182818284590452354x This function always returns a float. exp ( float $arg ) : float PHP exp() function returns the Euler Number e raised to given arg. Note that e is the base of natural algorithm. The exp() function is the inverse of natural logarithm. Example 1 One of the predefined constants in PHP is M_LN2 which stands for loge2 and is equal to 0.69314718055994530942. So, the exp() of this value will return 2. <?php echo “exp(” . M_LN2 . “) = ” . exp(M_LN2); ?> It will produce the following output − exp(0.69314718055995) = 2 Example 2 M_LN10 is another predefined constant representing loge10. This program calculates exp(M_LN10) and returns 10. <?php echo “exp(” . M_LN10 . “) = ” . exp(M_LN10); ?> It will produce the following output − exp(2.302585092994) = 10 PHP floor() Function The floor() function is another in-built function in PHP interpreter. This function accepts any float number as argument and rounds it down to the next lowest integer. This function always returns a float number as the range of float is bigger than that of integer. floor ( float $num ) : float PHP floor() function returns the largest integer less than or equal to the given parameter. Example 1 The following example shows how to round 15.05 to its next highest integer which is 15 <?php $arg=15.05; $val=floor($arg); echo “floor(” . $arg . “) = ” . $val; ?> It will produce the following output − floor(15.05) = 15 Example 2 The following example shows how to find the next lowest integer of 5.78. <?php $arg=5.78; $val=floor($arg); echo “floor(” . $arg . “) = ” . $val; ?> It will produce the following output − floor(5.78) = 5 Example 3 Negative numbers are rounded away from 0. <?php $arg=-3.95; $val=floor($arg); echo “floor(” . $arg . “) = ” . $val; ?> It will produce the following output − floor(-3.95) = -4 PHP intdiv() Function The intdiv() function returns the integer quotient of two integer parameters. If x/y results in “i” as division and “r” as remainder, then − x = y*i+r In this case, intdiv(x,y) returns “i” intdiv ( int $x , int $y ) : int The “x” parameter forms numerator part of the division expression, while the “y” parameter forms the denominator part of the division expression. PHP intdiv() function returns the integer quotient of division of “x” by “y”. The return value is positive if both the parameters are positive or both the parameters are negative. Example 1 The following example shows that if the numerator is less than the denominator, then intdiv() function returns 0. <?php $x=10; $y=3; $r=intdiv($x, $y);