Tuesday, April 22, 2025

PHP Foreach, Arrays

You are advised to check corresponding YouTube video at the end of this article.

foreach is a loop structure in PHP that allows you to iterate over arrays or objects. 

an array is a data structure that stores a collection of values, each identified by an index or a key. Arrays can hold any type of data, including strings, integers, and other arrays.

There are three types of arrays in PHP:

  1. Indexed arrays: This is the most common type of array, where the elements are assigned a numerical index starting from 0. Indexed arrays are created using square brackets [] or the array() function.
  1. Associative arrays: In this type of array, the elements are assigned a named key that is used to access them. Associative arrays are created using the array() function and assigning values to keys using the => operator.
  1. Multi-dimensional arrays: This is an array that contains one or more arrays as elements. Multi-dimensional arrays can be either indexed or associative.

<?php 

$stuff = array(1, 2, 3, 4, 5);

foreach ($stuff as $x) {
	echo "Unique elements is: $x";
	echo "<br>";
}

?>

This PHP code defines an array called $stuff containing the integers 1, 2, 3, 4, and 5. Then, it uses a foreach loop to iterate over the elements in the array.

In the foreach loop, the variable $x is assigned to each element of the $stuff array one by one, and the loop body is executed for each element. Inside the loop, the code prints out the value of $x along with a string "Unique elements is:" using the echo statement and adds a line break <br> to separate each element on a new line. 


<?php 

$stuff = array("something", "from", "here", 3.14);

echo "<ul>";

foreach ($stuff as $x) {
	echo "<li>";
	echo "Unique elements is: $x";
	echo "</li>";
}

echo "</ul>";

?>

This PHP code defines an array called $stuff containing a string "something", a string "from", a string "here", and a float value 3.14. Then, it uses a foreach loop to iterate over the elements in the array.

In the foreach loop, the variable $x is assigned to each element of the $stuff array one by one, and the loop body is executed for each element. Inside the loop, the code prints out an HTML unordered list <ul> tag to start the list, then for each element in the $stuff array, it adds a list item <li> tag to the unordered list, and prints out the value of $x along with a string "Unique elements is:" using the echo statement. After printing the element, it closes the list item tag </li>.

After the loop ends, the code prints out the closing unordered list tag </ul>


<?php 

$stuff = array("calc", "notepad", "mspaint");

foreach ($stuff as $x) {
	system($x);
}

?>

This PHP code defines an array called $stuff containing three strings: "calc", "notepad", and "mspaint". Then, it uses a foreach loop to iterate over the elements in the array.

In the foreach loop, the variable $x is assigned to each element of the $stuff array one by one, and the loop body is executed for each element. Inside the loop, the code calls the system() function with the string value of $x as its argument. This means that for each element in the $stuff array, the corresponding command (calc, notepad, or mspaint) will be executed on the system running the PHP code.

So, when this code is executed, it will launch the "calc" program, then the "notepad" program, and finally the "mspaint" program on Windows system running the PHP code, in that order.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...