" symbol is very common in PHP scripts, so what is the use of the "=>" symbol? The following article will introduce you to the "=>" symbol and briefly introduce how to use the "=>" symbol. I hope it will be helpful to you."/> " symbol is very common in PHP scripts, so what is the use of the "=>" symbol? The following article will introduce you to the "=>" symbol and briefly introduce how to use the "=>" symbol. I hope it will be helpful to you.">

Home  >  Article  >  Backend Development  >  What is the use of the '=>' symbol in PHP? (code example)

What is the use of the '=>' symbol in PHP? (code example)

青灯夜游
青灯夜游Original
2019-02-28 15:17:294268browse

The "=>" symbol is very common in PHP scripts, so what is the use of the "=>" symbol? The following article will introduce you to the "=>" symbol and briefly introduce how to use the "=>" symbol. I hope it will be helpful to everyone.

What is the use of the '=>' symbol in PHP? (code example)

PHP's "=>" symbol

In PHP"=> The ;" symbol is used to allocate key-value pairs in an array, mainly used in associative arrays. [Video tutorial recommendation: PHP tutorial]

Basic sentence structure:

key => value

Explanation: The value on the left side of the "=>" symbol is called the key, and the value on the right side is called key value.

Examples of using the "=>" symbol

The following is a simple code example to introduce how to use the "=>" symbol.

Example 1: Create an associative array using "=>" notation

<?php  
header("content-type:text/html;charset=utf-8");  
$subject = array( "Maths" => 95, 
                  "Physics" => 90,  
                  "Chemistry" => 96, 
                  "English" => 93,  
                  "Computer" => 98 
            );  
      
echo "学生成绩:<br><br>";  
echo "数学:" . $subject["Maths"], "<br>";  
echo "物理:" . $subject["Physics"], "<br>";  
echo "化学:" . $subject["Chemistry"], "<br>";  
echo "英语:" . $subject["English"], "<br>";  
echo "计算机:" . $subject["Computer"], "<br>";   
?>

Output:

What is the use of the => symbol in PHP? (code example)

Example 2: Create a numeric index array using the "=>" notation

<?php  
$arr = array( "0" => 7, 
              "1" => 10, 
              "2" => 8, 
              "3" => 5 
        ); 
foreach($arr as $key => $value) { 
    echo $key . " => " . $value . "<br>"; 
} 
?>

Output:

What is the use of the => symbol in PHP? (code example)

Example 3: Use For assigning numeric indexes without using the "=>" symbol

<?php  
header("content-type:text/html;charset=utf-8");  
$name = array("Zack", "Anthony",  
        "Ram", "Salim", "Raghav");  
  
foreach($name as $key => $value) { 
    echo $key . " => " . $value . "<br>"; 
} 
?>

Output:

What is the use of the => symbol in PHP? (code example)

[Related article recommendations]

What is the use of the $$ symbol in PHP? how to use? (code example)

#How to use the double NOT (!!) operator in PHP? (Code example)

What is the use of the short opening tag "=" in PHP?

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of What is the use of the '=>' symbol in PHP? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn