CodesJava

Easy learning with example program codes

PHP Indexed Array


PHP Array can be used to store number, string and object. The index of the array element is assigned automatically starting from 0 or can be assigned manually.

Syntax 1:

$arrayName = array(value 1, value 2, ...);

Syntax 2:

$arrayName[0] = "value 1";
$arrayName[1] = "value 2";
$arrayName[2] = "value 3";

Example 1: Using elements of an array by its index.

<!DOCTYPE html>
<html>
<body>
 
<?php
$food = array("PIZZA", "BURGER", "COKE");
echo "I love to eat " . $food[0] . ", " . $food[1] . " and " . $food[2] . ".";
?>
 
</body>
</html>

Output

I love to eat PIZZA, BURGER and COKE.

Example 2: Looping through the elements in an array.

<!DOCTYPE html>
<html>
<body>
 
<?php
$food = array("PIZZA", "BURGER", "COKE");
$arrlength = count($food);
 
 
for($x = 0; $x < $arrlength; $x++) {
	echo $food[$x];
	echo "<br>";
}
?>
</body>
</html>

Output

PIZZA
BURGER
COKE
Posted in PHP   


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap