What is the use of “=>” symbol in PHP ?
Last Updated :
13 Feb, 2019
The ‘=>’ symbol is used to assign key value pairs in an array. The value of left side is called key and the value of right side is called value of key. It is mostly used in associative array.
Syntax:
key => value
Example 1: PHP program to create associative array using ‘=>’ symbol.
<?php
$subject = array ( "Maths" => 95,
"Physics" => 90,
"Chemistry" => 96,
"English" => 93,
"Computer" => 98
);
echo "Marks for students:\n" ;
echo "Maths:" . $subject [ "Maths" ], "\n" ;
echo "Physics:" . $subject [ "Physics" ], "\n" ;
echo "Chemistry:" . $subject [ "Chemistry" ], "\n" ;
echo "English:" . $subject [ "English" ], "\n" ;
echo "Computer:" . $subject [ "Computer" ], "\n" ;
?>
|
Output:
Marks for students:
Maths:95
Physics:90
Chemistry:96
English:93
Computer:98
Example 2: PHP program to create numeric indexed array using ‘=>’ symbols.
<?php
$arr = array ( "0" => 7,
"1" => 10,
"2" => 8,
"3" => 5
);
foreach ( $arr as $key => $value ) {
echo $key . " => " . $value . "\n" ;
}
?>
|
Output:
0 => 7
1 => 10
2 => 8
3 => 5
Example 3: PHP program to assign numeric index without using ‘=>’ symbol.
<?php
$name = array ( "Zack" , "Anthony" ,
"Ram" , "Salim" , "Raghav" );
foreach ( $name as $key => $value ) {
echo $key . " => " . $value . "\n" ;
}
?>
|
Output:
0 => Zack
1 => Anthony
2 => Ram
3 => Salim
4 => Raghav
Share your thoughts in the comments
Please Login to comment...