Home >Backend Development >PHP Problem >What is the biggest difference between php scalar data and arrays

What is the biggest difference between php scalar data and arrays

青灯夜游
青灯夜游Original
2022-05-19 19:37:421883browse

The biggest difference is: a scalar can only store one data, while an array can store multiple data; and the scalar type is passed by value, while the array is passed by reference. In PHP, there are four types of scalar data: Boolean, string, integer, and floating point. They can only store one value at a time; and an array is a collection of data that can store any number of any type. data.

What is the biggest difference between php scalar data and arrays

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

php scalar data and The difference between arrays

  • The biggest difference: a scalar can only store one data, while an array can store multiple data.

  • Other differences: Scalar types are passed by value, while arrays are passed by reference.

What is scalar data

The scalar data type is the most basic unit of the data structure and can only store one piece of data. There are four types of scalar data types in PHP:

Type Function
boolean (Boolean) The simplest data type, only two values: true (true) / false (false)
string (String) A string is a continuous sequence of characters
integer (integer) The integer data type contains all integers. It can be an integer or a negative number
float (floating point type) The floating point data type is also used to store numbers. Unlike integers, it contains decimals

1) Boolean

Boolean type is more commonly used in PHP One of the data types, which holds a true value (true) or a false value (false)

// 代码:
$a = true; // 真值
$b = false; // 假值

2) String (string)

A string is a continuous sequence of characters, consisting of numbers, letters, and symbols. Each character in the string only occupies one byte. Characters include the following types

##Invisible typesFor example:\ n (line feed character), \r (carriage return), \t (tab character), etc.

不可见字符是比较特殊的字符用于控制字符串格式输出,在浏览器上不可见,只是能看到字符串的输出结果。

在 PHP 中有 3 种定义字符串的方式:

a.单引号(')

$a = 'zZ爱吃菜';

b.双引号(")

$b = "zZ爱吃菜";

单引号与双引号的区别:双引号所包含的变量会自动被替换成实际值,而单引号包含的变量则按普通类型输出。例如:

$a = 'hello';
$b = '$a china';
$c = "$a world"; // 个人建议这样写:$c = "{$a} world"; 不容易产生歧义
echo $b;
echo $c;
结果:
$a china
hello world

c.定界符(5a3d5a96c2d13de29b6740a929c5fbbb a34de1251f0d9fe1e645927f19a896e8b6c5a531a458a2e790c1fd6421739d1c {$name}076402276aae5dbec7f672f8f4e5cc81 3f1c4e4b6b16bbbd69b2ee476dc4f83a var p='hello world'; document.writeln(p); 2cacc6d41bbb37262a98f745aa00fbf0 b90dd5946f0946207856a8a37f441edffd273fcf5bcad3dfdad3c41bd81ad3e5 f16b1740fad44fb09bfe928bcc527e08 221f08282418e2996498697df914ce4e 5a07473c87748fb1bf73f23d45547ab8aaaaa4afa15d3069109ac30911f04c56f3338 5a07473c87748fb1bf73f23d45547ab8aaaaa4afa15d3069109ac30911f04c56f3338 5a07473c87748fb1bf73f23d45547ab8aaaaa4afa15d3069109ac30911f04c56f3338 18bb6ffaf0152bbe49cd8a3620346341 Eof;

注意:使用定界符输出字符串,结束标识符必须单独另起一行,并且不允许有空格。

3)整型(integer)

整型数据类型只能包含整数,在 32 位的操作系统中,有效范围是: -2147483648(2的31次方) ~ 217483647 (2的31次方-1)。整型可以使用十进制、八进制和十六进制表示,如:八进制(数字前面必须加0)、十六进制(数字前面必须加0x)

$int1 = 1234;
$int2 = 01234;
$int3 = 0x1234;

echo "十进制的结果是:{$int1}0c6dc11e160d3b678d68754cc175188a";
echo "八进制的结果是:{$int2}0c6dc11e160d3b678d68754cc175188a";
echo "十六进制的结果是:{$int3}0c6dc11e160d3b678d68754cc175188a";
结果
十进制的结果是:1234
八进制的结果是:668
十六进制的结果是:4660

注意:如果给定的数值超出了 int 型所能表示的最大范围,将会被当作 float 型处理,这种情况叫做:整型溢出。表达式最后的运算结果超出 int 范围,也会返回 float 型

4)浮点型(float)

浮点型数据类型可以用来存储整数,也可以保存小数。它提供的精度比整数大得多。 在32系统中有效范围: 1.7E-308 ~ 1.7E+308

在 PHP 4.0 之前的版本 浮点型被标识为 double,也叫双精度浮点数,两者没什么区别

// 定义
$a = 1.036;
$b = 2.035;
$c = 3.48E2; // En代表10*n, E1 代表 * 10, $c = 348

echo $c;
结果:348

什么是数组

数组就是一组数据的集合,把一系列数据组织起来,形成一个可操作的整体。

因为 PHP 是弱数据类型的编程语言,所以 PHP 中的数组变量可以存储任意多个、任意类型的数据,并且可以实现其他强数据类型中的堆、栈、队列等数据结构的功能。

数组 array 是一组有序的变量,其中每个值被称为一个元素。每个元素由一个特殊的标识符来区分,这个标识符称为键(也称为下标)。

数组中的每个实体都包含两项,分别是键(key)和值(value)。可以通过键值来获取相应的数组元素,这些键可以是数值键,也可以是关联键。如果说变量是存储单个值的容器,那么数组就是存储多个值的容器。

PHP 数组比其他高级语言中的数组更加灵活,不但支持以数字为键名的索引数组,而且支持以字符串或字符串、数字混合为键名的关联数组。而在其他高级语言中,如 Java 或者 C++ 等语言的数组,只支持数字索引数组。

PHP 数组的结构如下图所示:

What is the biggest difference between php scalar data and arrays

扩展知识:

在 PHP 中,标量类型数据是值传递的,而复合类型数据(对象和数组)是引用传递的。

但是复合类型数据的引用传递和用 & 符号明确指定的引用传递是有区别的,前者的引用传递是对象引用,而后者是指针引用。

在解释对象引用和指针引用之前,先让咱们 看多个 例子。

<?php
echo "<pre class="brush:php;toolbar:false">";
class SampleClass {
var $value;
}
$a = new SampleClass();
$a->value = $a;

$b = new SampleClass();
$b->value = &$b;

echo serialize($a);
echo "\n";
echo serialize($b);
echo "\n";
echo "
"; ?>

这个例子的输出结果是这样的:

O:11:"SampleClass":1:{s:5:"value";r:1;}
O:11:"SampleClass":1:{s:5:"value";R:1;}

大家 会发觉 ,这里变量 $a 的 value 字段的值被序列化成了 r:1,而 $b 的 value 字段的值被序列化成了 R:1。

但是对象引用和指针引用到底有什么区别呢?

看下面这个例子:

echo "<pre class="brush:php;toolbar:false">";
class SampleClass {
var $value;
}
$a = new SampleClass();
$a->value = $a;
$b = new SampleClass();
$b->value = &$b;
$a->value = 1;
$b->value = 1;
var_dump($a);
var_dump($b);
echo "
";

运行结果也许出乎你的预料:

object(SampleClass)#1 (1) {
   ["value"]=>
   int(1)
}
int(1)

改动 $a->value 的值仅仅是改动 了 $a->value 的值,而改动 $b->value 的值却改动 了 $b 本身,这就是对象引用和指针引用的区别。

推荐学习:《PHP视频教程

Character type name Content
Number type For example: 1, 2, 3, etc.
Letter type For example: a, b, c, etc.
Special types For example: #,$,^,&, etc.

The above is the detailed content of What is the biggest difference between php scalar data and arrays. 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