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

青灯夜游
Release: 2023-04-05 12:48:01
Original
4266 people have browsed it

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
Copy after login

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>";   
?>
Copy after login

Output:

' symbol in PHP? (code example)" > symbol in PHP? (code example)" title="155133715739596What is the use of the => symbol in PHP? (code example)" alt="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>"; 
} 
?>
Copy after login

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>"; 
} 
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template