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   
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki