<?php
echo date("Y/m/d") . "<br />";
echo date("Y.m.d") . "<br />";
echo date("Y:->m:->d")."<br>";
echo("hello") ."<br/>";
 $app5=5;
echo($app5);
$baby="going";
echo("$baby i love to ");
//echo ($baby$app5);
if($app5==5)
echo "dance"."<br>";
echo strlen("dance");
echo strpos("dance","h");
switch($ab=1)
{
	case 1:
		echo 1;
	case 2:
		echo 2;
		default :
		echo "default";
}
$a = array(1,2,5,3,6);
for($i=0;$i<5;$i++)
echo $a[$i]."<br>";
$a = array(1=>"apple",2=>"boy",3=>"girl",4=>"koel",5=>"good");
for($i=1;$i<=5;$i++)
echo $a[$i]."<br>";
echo $a[2]."<br>";
echo $a['2']."<br>";
$a=array	(

$0=array(40,60,20),
$1=array(278,102,173)
);
for($i=0;$i<2;$i++)
{	echo "<br>".$a[$i];
	for($j=0;$j<3;$j++)
	echo $a[$i][$j]." ";
}
?>

I have started learning PHP today and I was trying out some very basic commands.
I am getting an error in line 35.
How to use 2-d arrays in PHP?
Also give example of Associated 2-d array.
Any help will be appreciated.

Dani AI

Generated

As pointed out, the parse error in ’s snippet comes from using variable names that begin with digits (e.g. $0, $1) and from trying to do assignments inside the array() literal. PHP variable names must start with a letter or underscore, so that construct produces a syntax error. Also, attempting to echo an array directly will produce an "Array to string" notice — use print_r()/var_dump() or iterate the array to show its contents.

Enable error reporting and quick linting while learning to see the exact problem:

ini_set('display_errors', 1);
error_reporting(E_ALL);

// command-line check:
// php -l script.php

Numeric 2‑D arrays can be built by assigning rows (short-array syntax shown; use array(...) on older PHP versions):

$matrix = [];
$matrix[0] = [40, 60, 20];
$matrix[1] = [278, 102, 173];

// access a value:
echo $matrix[1][0]; // prints 278

An associative (keyed) 2‑D array example:

$students = [
  'john' => ['math'=>40, 'eng'=>60],
  'jane' => ['math'=>278, 'eng'=>102],
];

echo $students['jane']['eng']; // 102

Preferred way to print all values is nested foreach, and use strict checks with functions like strpos() (it returns integer 0 when found at position 0, false when not found):

foreach ($students as $name => $scores) {
  foreach ($scores as $subject => $score) {
    echo "$name - $subject: $score<br>";
  }
}

Final notes: if a switch has no break statements, execution will fall through all subsequent cases; strpos() must be compared with === false to detect "not found"; and short array syntax [] requires PHP 5.4+.

A variable cannot start with a number, you have to use an alphabetic character instead, so $0 and $1 are wrong. For example use $a0 and $a1.

Anyway, in your case a bi-dimensional array can be written as below:

$a = array(
 array(40,60,20),
 array(278,102,173)
);

To display data, then write:

echo $a[0][1]; # will display 60

If you want to assign specific keys you can write:

$a = array(
'alfa' => array(40,60,20),
'beta' => array(278,102,173)
);

echo $a['alfa'][2]; # output 20

bye.

commented: Nicely explained +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.