How to create an array of names with one statement
$names = array('Ted Lewis', 'Sue Jones', 'Ray Thomas');
How to create an array with multiple statements
$names = array();
$names[0] = 'Ted Lewis';
$names[1] = 'Sue Jones';
$names[2] = 'Ray Thomas';
How to create an array of discounts with one statement
$discounts = array(0, 5, 10, 15);
How to create an array of discounts with multiple statements
$discounts = array();
$discounts[0] = 0;
$discounts[1] = 5;
$discounts[2] = 10;
$discounts[3] = 15;
How to use the print_r()
function to view an array’s indexes and values
print_r($names); // Output: Array ( [0] => Ted Lewis [1] => Sue Jones [2] => Ray Thomas )
");
print_r($names); // Output: Array ( [0] => Ted Lewis [1] => Sue Jones [2] => Ray Thomas )
echo("\n");
?>
How to define a constant for an array (PHP 7.0 and later)
define('MONTHS', array('Jan', 'Feb', 'Mar'));
- An array can store one or more elements. Each element consists of an index and a value. An index can be either an integer or a string. A value can be any PHP data type.
- By default, PHP uses integer indexes where 0 is the first element, 1 is the second element, and so on.
Back