w3resource

PHP Math functions - Exercises, Practice, Solution

PHP Math functions [ 12 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

1. Write a PHP script to find the maximum and minimum marks from the following set of arrays.
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.
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.
Click me to see the solution

4. Write a PHP script to convert scientific notation to an int and a float.
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.
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.
Click me to see the solution

7. Find earliest and latest dates from a list of dates.
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.
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.
Click me to see the solution

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

11. Write a PHP function to create a human-readable random string for a captcha.
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.
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.



Follow us on Facebook and Twitter for latest update.

PHP: Tips of the Day

PHP: How to pass an array within a query string?

Submitting multi-value form fields, i.e. submitting arrays through GET/POST vars, can be done several different ways, as a standard is not necessarily spelled out.

Three possible ways to send multi-value fields or arrays would be:

  • ?cars[]=Saab&cars[]=Audi (Best way- PHP reads this into an array)
  • ?cars=Saab&cars=Audi (Bad way- PHP will only register last value)
  • ?cars=Saab,Audi (Haven't tried this)

Form Examples

On a form, multi-valued fields could take the form of a select box set to multiple:

<form> 
    <select multiple="multiple" name="cars[]"> 
        <option>Volvo</option> 
        <option>Saab</option> 
        <option>Mercedes</option> 
    </select>
</form>

(NOTE: In this case, it would be important to name the select control some_name[], so that the resulting request vars would be registered as an array by PHP)

or as multiple hidden fields with the same name:

<input type="hidden" name="cars[]" value="Volvo">
<input type="hidden" name="cars[]" value="Saab">
<input type="hidden" name="cars[]" value="Mercedes">

NOTE: Using field[] for multiple values is really poorly documented. I don't see any mention of it in the section on multi-valued keys in Query string - Wikipedia, or in the W3C docs dealing with multi-select inputs.

UPDATE

As commenters have pointed out, this is very much framework-specific. Some examples:

Query string:

?list_a=1&list_a=2&list_a=3&list_b[]=1&list_b[]=2&list_b[]=3&list_c=1,2,3 

Rails:

"list_a": "3", 
"list_b":[
    "1",
    "2",
    "3"
  ], 
"list_c": "1,2,3"

Angular:

"list_a": [
    "1",
    "2",
    "3"
  ],
  "list_b[]": [
    "1",
    "2",
    "3"
  ],
  "list_c": "1,2,3"

(Angular discussion)

Maintaining order: One more thing to consider is that if you need to maintain the order of your items (i.e. array as an ordered list), you really only have one option, which is passing a delimited list of values, and explicitly converting it to an array yourself.

Ref : https://bit.ly/2FGfPLj

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook