# PHP Math functions - Exercises, Practice, Solution

## PHP Math functions [ 12 exercises with solution]

1. Write a PHP script to find the maximum and minimum marks from the following set of arrays. Go to the editor
Sample arrays :
\$marks1 = array(360,310,310,330,313,375,456,111,256);
\$marks2 = array(350,340,356,330,321);
\$marks3 = array(630,340,570,635,434,255,298);
Expected Output :
Maximum marks : 635
Minimum marks : 111
Click me to see the solution

2. Write a PHP script which rounds the following values with 1 decimal digit precision. Go to the editor
Sample values and Output :
1.65 --> 1.7
1.65 --> 1.6
-1.54 --> -1.5
Click me to see the solution

3. Write a PHP script to generate random 11 characters string of letters and numbers. Go to the editor
Click me to see the solution

4. Write a PHP script to convert scientific notation to an int and a float. Go to the editor
Sample scientific notation : 4.5e3
Expected Output : 4 & 4500
Click me to see the solution

5. Write a PHP script to convert a date from yyyy-mm-dd to dd-mm-yyyy. Go to the editor
Sample floating value : 0.0456
Expected Output :
Exponent part : -4
Mantissa part : 0.7296
Click me to see the solution

6. Write a PHP script to get the information regarding memory usage in KB or MB etc. Go to the editor
Click me to see the solution

7. Find earliest and latest dates from a list of dates. Go to the editor
Click me to see the solution

8. Write a PHP function to round a float away from zero to a specified number of decimal places. Go to the editor
Sample Data :
(78.78001, 2)
(8.131001, 2)
(0.586001, 4)
(-.125481, 3)
-.125481
Sample Output :
78.79
8.14
0.5861
-0.126
-1
Click me to see the solution

9. Write a PHP function to convert Arabic Numbers to Roman Numerals. Go to the editor
Click me to see the solution

10. Write a PHP function to get random float numbers. Go to the editor
Click me to see the solution

11. Write a PHP function to create a human-readable random string for a captcha. Go to the editor
Sample Output :
hoboh
tynzh
Click me to see the solution

12. Write a PHP function to get the distance between two points on the earth. Go to the editor
Click me to see the solution

PHP Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

﻿

## PHP: Tips of the Day

\$GLOBALS: An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array

Example:

```<?php
\$myGlobal = "global"; // declare variable outside of scope
function test()
{
\$myLocal = "local"; // declare variable inside of scope
// both variables are printed
var_dump(\$myLocal);
var_dump(\$GLOBALS["myGlobal"]);
}
test(); // run function
// only \$myGlobal is printed since \$myLocal is not globally scoped

var_dump(\$myLocal);
var_dump(\$myGlobal);
?>
```

Output:

```string(5) "local"
string(6) "global"
NULL
string(6) "global"
```

In the above example \$myLocal is not displayed the second time because it is declared inside the test() function and then destroyed after the function is closed.

Becoming global

To remedy this there are two options.

Option one: global keyword

```function test()
{
global \$myLocal;
\$myLocal = "local";
var_dump(\$myLocal);
var_dump(\$GLOBALS["myGlobal"]);
}
```

The global keyword is a prefix on a variable that forces it to be part of the global scope.

Note that you cannot assign a value to a variable in the same statement as the global keyword. Hence, why I had to assign a value underneath. (It is possible if you remove new lines and spaces but I don't think it is neat. global \$myLocal; \$myLocal = "local").

Option two: \$GLOBALS array

```function test()
{
\$GLOBALS["myLocal"] = "local";
\$myLocal = \$GLOBALS["myLocal"];
var_dump(\$myLocal);
var_dump(\$GLOBALS["myGlobal"]);
}
```

In this example I reassigned \$myLocal the value of \$GLOBAL["myLocal"] since I find it easier writing a variable name rather than the associative array.