Home  >  Article  >  Backend Development  >  Whether the php array key name should be quoted or not

Whether the php array key name should be quoted or not

青灯夜游
青灯夜游Original
2022-07-07 19:00:522468browse

In PHP, if the array key name is an integer numeric type, you do not need to add quotes; and if the array key name is a string type, you need to add quotes. If a key name in an array is not a number, then the array is an associative array. The key name of the associative array can be any integer or string; and if the key name is a string, add a Delimitation modifier, that is, add single quotes "''" or double quotes """".

Whether the php array key name should be quoted or not

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

In php, the array key name Whether to add quotes depends on the data type of the key name.

  • If the array key name is an integer numeric type, no quotation marks are needed;

<?php
header("Content-type:text/html;charset=utf-8");
$arr = array(1=>"a",2=>"b",3=>"c",4=>"d",5=>"e",);
var_dump($arr);
?>

Whether the php array key name should be quoted or not

  • If the array key name is a string type, you need to add quotes

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array(1=>"1","a"=>"",2=>"2","b23"=>0,"12r3"=>"blue");
var_dump($arr);
?>

Whether the php array key name should be quoted or not

Instructions:

In a PHP array, no matter what type of key name there will be a value corresponding to it, that is, a key/value pair. According to the different data types of the array key names, we can divide the PHP array into There are two types:

  • Using numbers as key names is called an indexed array (Indexed Array);

  • Using strings or strings, An array in which numbers are mixed as keys is called an Associative Array.

The subscript (key name) of an associative array is composed of a mixture of numeric values ​​and strings. If a key name in an array is not a number, then the array is an associative array.

The key name of the associative array can be any integer or string. If the key name is a string, add a delimiting modifier to the key name - single quote ' ' or double quote " ".

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Whether the php array key name should be quoted or not. 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