The print_r function prints an array.
The array_rand function selects a random array element.
The implode function converts an array into a string.
<?php
print (”<br> The sort function sorts array <br>”);
sort ($names);
//Get size of array
$asize=sizeof ($names);
for ($i=0; $i <$asize; $i++) {
//Check if it is female name put Mrs. prefix
//else put Mr. prefix
if (($names [$i] ==“Anna”) || ($names [$i]
==“Maria”))
{
print (“Hello Mrs.”. $names [$i].”<br>”);
}
else
{
print’ Hello Mr.”. $names [$i].”<br>”);
}
} //for
print (”<br>”);
?>
Output:
The sort function sorts an array
Hello Mrs. Anna
Hello Mr. George
Hello Mr. George
Hello Mr. James
Hello Mr. James
Hello Mr. James
Hello Mr. John
Hello Mr. John
Hello Mrs. Maria
Hello Mr. Peter
Hello Mr. Robert
<?php
echo “The array_unique function removes duplicate array values <br>”;
$array=array ();
$array=array_unique ($names);
foreach ($array as $key => $value) {
echo $key. "-”. $value. "<br>”;
}
print (”<br>”);
?>
Output:
The array_unique function removes duplicate values.
0-Anna
1-George
3-James
6-John
8-Maria
9-Peter
10-Robert
<?php
rsort ($array);
print (“The rsort function sorts an array in reverse order <br>”);
foreach ($array as $key => $value) {
echo $key. "-”. $value. "<br>”;
}
?>
Output:
The rsort function sorts an array in reverse order
0-Robert
1-Peter
2-Maria
3-John
4-James
5-George
6-Anna
<?php
print (”<br> The array_pop () functions returns the last element. <br>”);
$lastelement=array_pop ($array);
print (”<br> The last element=”. $lastelement.”<br>”);
?>
Output:
The array_pop () function returns the last element
The last element=Anna
print (”<br> Array after calling the array_pop ().
The last element removed. <br> <br>”);
foreach ($array as $key => $value) {
echo $key. "-”. $value. "<br>”;
}
Output:
Array after calling array_pop (): The last element removed
0-Robert
1-Peter
2-Maria
3-John
4-James
5-George
The array_push function adds elements to the end of an array.
The print_r prints array function prints array key – value pairs.
<?php
array_push ($array, “Chris”, “Colin”);
print_r ($array);
?>
Array after calling array_push ($array, “Chris”, “Colin”)
and print_r functions: Chris and Colin are added to the end of the array.
Output:
Array ([0] => Robert [1] => Peter [2] => Maria [3] => John [4] => James [5] => George [6] => Chris [7] => Colin)
The array_rand ($array) function returns random array index.
$random=array_rand ($array);
print (”<br> print array element by random
index <br>”);
print (”<br> Random element=”. $array [$random].”<br>”);
Output:
print array element by random index
Random element=Colin
<?php
$string=implode ($array);
print (”<br> Array is imploded in a string: <br>”);
print ($string);
?>
Array is imploded in a string:
RobertPeterMariaJohnJamesGeorgeChrisColin
The for loop
for ($i=0; $i <10; $i++) {
print ($i.”<br>”);
}
The $i variable is local because it is defined inside the loop. However, if you try to Output: the $i variable outside the loop, its value will be 10.
Break and Continue
Break; statement breaks out of the loop. Loop execution stops.
$names [0] =“Anna”;
$names [1] =“George”;
$names [2] =“James”;
$names [3] =“James”;
$names [4] =“John”;
$names [5] =“Maria”;
$names