Hello, I am new to php and am trying to do the following task.
Complete the following PHP script so that it prints the numbers, given in a form, in a specific order. The script should organize the numbers from largest to the smallest and from smallest to largest and print both of these number strings on screen. The points are sent to the script as a character string, where points are separated with comma (e.g. 4,5,2). Points are divided into an array with the explode-function. Using the sort-function is not allowed. Do the organizing with a for-statement. Incomplete program:
<?php
$numberstring = $_GET['numberstring'];
$array = explode(',',$numberstring);
echo "Order in the beginning: $numberstring\n";
// Your code here and only here
echo "Largest to smallest: $largest_smallest\n";
echo "Smallest to largest: $smallest_largest\n";
?>
First I tried it if it works with the following straight forward code:
$largest_smallest=rsort($numberstring );
$smallest_largest=sort($numberstring );
And it gave me the following error:
Order in the beginning: 4,7,-2,0,6
Warning: rsort() expects parameter 1 to be array, string given in /tmp/K2U5XgShJ9/1/.php on line 9
Warning: sort() expects parameter 1 to be array, string given in /tmp/K2U5XgShJ9/1/.php on line 10
Largest to smallest:
Smallest to largest:
So can any one help me why there is an error and also according to the assignment I am supposed to do it with for statement. what is the equivalent way of sort() or rsort() doing this? thank you so much!
Example output:
Order in the beginning: 4,7,-2,0,6
Largest to smallest: 7,6,4,0,-2
Smallest to largest: -2,0,4,6,7