In php, there are two composite data types, one is array and the other is object.
##Array: a collection of data of the same type;
Object (object): An object is an instance of a class, usually created using the new keyword.
Array(Recommended learning: PHP programming from entry to proficiency)
Collect a series of data, Form an operable whole, which is an array. The data in the array can be scalar data, arrays, objects, resources, etc. We generally call a single piece of data in an array an element, and elements are divided into two parts: index (key name) and value. The index (key name) can be a number or a string, and the value can be of any data type.Array declaration
Format:
$a=array(值1,值2,值3,...); //或 $a=array(key1=>值1,key2=>值2,key3=>值3,...); //或 $a=array(); $a[索引]=值1; $a[索引]=值2; $a[索引]=值3; ...
Example:
<?php $a=array("a","b","c","d"); $b=array("a"=>1,"b"=>2); $b["c"]=3; var_dump($a); var_dump($b); ?>
Note:
The length of the number is dynamic. As long as you add a value to the array, the length of the array will automatically increase; The value in the number can be changed at any time. , as long as a value is assigned to the specified unit, the original value of the unit will be overwritten; the var_dump() function will output the structure of the array, and cannot output the value of the array individually.Object (object)
Object is an instance of a class and is real. Objects are generally created using the new keyword.Creation of objects
new 类名();
Instance
<?php class Dog{ //类 public $name=""; public $color=""; function __construct($name,$color){//构造函数 $this->name=$name; $this->color=$color; } } $xiao=new Dog("小黄","黄色");//创建对象 var_dump($xiao); //打印对象 ?>
The above is the detailed content of What are the two composite data types in PHP?. For more information, please follow other related articles on the PHP Chinese website!