PHP Programming Language

PHP foreach

The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes:

foreach (iterable_expression as $value)
foreach (iterable_expression as $key => $value)

The first form traverses the iterable given by iterable_expression. On each iteration, the value of the current element is assigned to $value.

The second form will additionally assign the current element’s key to the $key variable on each iteration.

foreach (array(1, 2, 3, 4) as &$value) {
    $value = $value * 2;
JavaScript Programming Language

JavaScript For Loop

Loops are handy, if you want to run the same code over and over again, each time with a different value.

Often this is the case when working with arrays:


for (i = 0; i < cars.length; i++) {
  text += cars[i] + "<br>";

Different Kinds of Loops

JavaScript supports different kinds of loops:

  • for – loops through a block of code a number of times
  • for/in – loops through the properties of an object
  • for/of – loops through the values of an iterable object
  • while – loops through a block of code while a specified condition is true
  • do/while – also loops through a block of code while a specified condition is true

The For Loop

The for loop has the following syntax:

for (statement 1; statement 2; statement 3) {
  // code block to be executed

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed.

for (i = 0; i < 5; i++) {
  text += "The number is " + i + "<br>";

PHP Arrays

An array stores multiple values in one single variable:


$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";

What is an Array?

An array is a special variable, which can hold more than one value at a time.

If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";

However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?

The solution is to create an array!

An array can hold many values under a single name, and you can access the values by referring to an index number.

Create an Array in PHP

In PHP, the array() function is used to create an array:


In PHP, there are three types of arrays:

  • Indexed arrays – Arrays with a numeric index
  • Associative arrays – Arrays with named keys
  • Multidimensional arrays – Arrays containing one or more arrays

Get The Length of an Array – The count() Function

The count() function is used to return the length (the number of elements) of an array:


$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);


preg_replace — Perform a regular expression search and replace


preg_replace ( string|array $pattern , string|array $replacement , string|array $subject [, int $limit = -1 [, int &$count = null ]] ) : string|array|null


Example #1 Using backreferences followed by numeric literals

$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);

The above example will output:


PHP str_replace

str_replace — Replace all occurrences of the search string with the replacement string


str_replace ( array|string $search , array|string $replace , string|array $subject [, int &$count = null ] ) : string|array

If you don’t need fancy replacing rules (like regular expressions), you should use this function instead of preg_replace().

This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.


Example #1 Basic str_replace() examples

// Provides: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");

// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");

// Provides: You should eat pizza, beer, and ice cream every day
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);

// Provides: 2
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count;