Home > Backend Development > PHP Problem > What are the two composite data types in PHP?

What are the two composite data types in PHP?

(*-*)浩
Release: 2023-02-23 17:56:01
Original
7557 people have browsed it

In php, there are two composite data types, one is array and the other is object.

What are the two composite data types in PHP?

##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;
...
Copy after login

Example:

<?php
$a=array("a","b","c","d");
$b=array("a"=>1,"b"=>2);
$b["c"]=3;
var_dump($a);
var_dump($b);
?>
Copy after login

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 类名();
Copy after login

Instance

<?php
class Dog{ //类
public $name="";
public $color="";
function __construct($name,$color){//构造函数
$this->name=$name;
$this->color=$color;
}
}
$xiao=new Dog("小黄","黄色");//创建对象
var_dump($xiao); //打印对象
?>
Copy after login

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!

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