Exercise: PHP basic syntax and functions
In this exercise you will try some basic PHP syntax, like if statements and loops.
You will also write functions and call function.
Getting started
Make a new PHP project in PhpStorm.
Form
In the index page make a form similar to this
You are supposed to type some numbers separated by one or more spaces.
Action page
Make another page to handle the input from the form.
The input is one long text string.
- Split the long text string into an array of smaller text strings (numbers). The function explode may be useful.
- Make a for-each loop to print each smaller string on a separate line.
- Make a user defined function to calculate the sum of the numbers.
For-each might be handy.
What happens if your input consists of numbers mixed with letters?
What happens if your input is empty?
- Try the function array_sum.
- Sort the numbers in the array using one (or more) of the sort functions.
- Try some other array related functions.