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

Form for numbers

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.

  1. Split the long text string into an array of smaller text strings (numbers). The function explode may be useful.
  2. Make a for-each loop to print each smaller string on a separate line.
  3. 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?
  4. Try the function array_sum.
  5. Sort the numbers in the array using one (or more) of the sort functions.
  6. Try some other array related functions.