Home  >  Article  >  Backend Development  >  Which symbol in php can declare an array

Which symbol in php can declare an array

青灯夜游
青灯夜游Original
2022-05-26 11:37:251957browse

The "[]" symbol in php can declare an array. Two declaration methods: 1. Use the "$array variable name = [element value]" statement. "[]" can contain multiple value lists, separated by commas. If the value list is omitted, an empty array is declared; 2 , use "$array variable name [subscript] = value" to directly declare the array and assign a value to the specified subscript element.

Which symbol in php can declare an array

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

"[ in php ]" symbol can declare an array.

In PHP, square brackets "[]" are also called "array literals". There are two ways to use them to declare an array.

Method 1. Use “$array variable name=[element value]

"[]" does not contain a value, then an empty array is defined:

<?php
header("Content-type:text/html;charset=utf-8");
$arr=[];
var_dump($arr);
?>

Which symbol in php can declare an array

"[]" can also contain multiple value lists, with commas between values Separate;

<?php
header("Content-type:text/html;charset=utf-8");
$arr=[1,2,3,4,5];
var_dump($arr);
?>

Which symbol in php can declare an array

<?php
header("Content-type:text/html;charset=utf-8");
$arr=[1=>"1","a"=>"red",2=>"2","b"=>"green","c"=>"blue"];
var_dump($arr);
?>

Which symbol in php can declare an array

##Method 2, use "$array Variable name [subscript] = value

This method can directly declare an array and assign a value to the specified subscript element.

The subscript (index value) can be a string or an integer, and the subscript needs to be wrapped with

[ ].

Example 1:

<?php
header("Content-type:text/html;charset=utf-8");
$array[0] = &#39;欢迎&#39;;
$array[1] = &#39;来到&#39;;
$array[2] = &#39;PHP中文网&#39;;
$array[&#39;url&#39;] = &#39;https://www.php.cn/&#39;;
//输出语句
var_dump($array);
?>

Output result:

Which symbol in php can declare an array

Example 2:

<?php
header("Content-type:text/html;charset=utf-8");
$array[] = &#39;香蕉&#39;;
$array[] = &#39;苹果&#39;;
$array[] = &#39;橘子&#39;;
$array[] = &#39;榴莲&#39;;
//输出语句
var_dump($array);
?>

Output result:

Which symbol in php can declare an array

It can be seen that when we do not specify a specific index value within square brackets, the default is a numeric index, and the index value increases sequentially starting from 0 by default.

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of Which symbol in php can declare an array. 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