PHP Access Arrays

Access Array Item

To access an array item, you can refer to the index number for indexed arrays, and the key name for associative arrays.

Example

Access an item by referring to its index number:

$cars = array("Volvo", "BMW", "Toyota");
echo $cars[2];

Note: The first item has index 0.

To access items from an associative array, use the key name: Continue reading PHP Access Arrays

PHP Create Arrays

Create Array

You can create arrays by using the array() function:

Example

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

You can also use a shorter syntax by using the [] brackets:

Example

$cars = ["Volvo", "BMW", "Toyota"];

Continue reading PHP Create Arrays

PHP Associative Arrays

PHP Associative Arrays

Associative arrays are arrays that use named keys that you assign to them.

Example

$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
var_dump($car);

Continue reading PHP Associative Arrays

PHP Indexed Arrays

PHP Indexed Arrays

In indexed arrays each item has an index number.

By default, the first item has index 0, the second item has item 1, etc.

Example

Create and display an indexed array:

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

Continue reading PHP Indexed Arrays

PHP Arrays

An array stores multiple values in one single variable:

Example

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

What is an Array?

An array is a special variable that can hold many values under a single name, and you can access the values by referring to an index number or name. Continue reading PHP Arrays

PHP Functions

The real power of PHP comes from its functions.

PHP has more than 1000 built-in functions, and in addition you can create your own custom functions.

PHP Built-in Functions

PHP has over 1000 built-in functions that can be called directly, from within a script, to perform a specific task.

Please check out our PHP reference for a complete overview of the PHP built-in functions. Continue reading PHP Functions

PHP Continue

The continue statement can be used to jump out of the current iteration of a loop, and continue with the next.

Continue in For Loops

The continue statement stops the current iteration in the for loop and continue with the next.

Example

Move to next iteration if $x = 4:

for ($x = 0; $x < 10; $x++) {
  if ($x == 4) {
    continue;
  }
  echo "The number is: $x <br>";
}

Continue reading PHP Continue

PHP Break

The break statement can be used to jump out of different kind of loops.

Break in For loop

The break statement can be used to jump out of a for loop.

Example

Jump out of the loop when $x is 4:

for ($x = 0; $x < 10; $x++) {
  if ($x == 4) {
    break;
  }
  echo "The number is: $x <br>";
}

Continue reading PHP Break

PHP foreach Loop

The foreach loop – Loops through a block of code for each element in an array or each property in an object.

The foreach Loop on Arrays

The most common use of the foreach loop, is to loop through the items of an array.

Example

Loop through the items of an indexed array:

$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $x) {
  echo "$x <br>";
}

For every loop iteration, the value of the current array element is assigned to the variable $x. The iteration continues until it reaches the last array element. Continue reading PHP foreach Loop

PHP for Loop

The for loop – Loops through a block of code a specified number of times.

The PHP for Loop

The for loop is used when you know how many times the script should run.

Syntax

for (expression1, expression2, expression3) {
  // code block
}

This is how it works:

  • expression1 is evaluated once
  • expression2 is evaluated before each iteration
  • expression3 is evaluated after each iteration

Continue reading PHP for Loop