search
HomeBackend DevelopmentPHP ProblemWhat is the biggest difference between php scalar data and arrays

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.定界符(

如果用传统的输出方法——按字符串输出的话,肯定要有大量的转义符来对字符串中的引号等特殊字符进行转义,以免出现语法错误。如果是一两处还可以容忍,但是要是一个完整的html文本或者是一个200行的js我想是谁都会崩溃的。这就是PHP为什么要引入一个定界符的原因——至少一大部分原因是这样的。
1.PHP 定界符的作用就是按照原样,包括换行格式什么的,输出在其内部的东西;
2.PHP 定界符中字符串内容不需要转义

// 定义
<blockquote><p>不需要对付出转义的好处:直接输出你想要的 html 字符串</p></blockquote><pre class="brush:php;toolbar:false">$name = 'kitty';
echo 
<tr><td>
{$name}<br>
<script>
var p=&#39;hello world&#39;;
document.writeln(p);
</script>
</td></tr>
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.
Eof;

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

3)整型(integer)

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

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

echo "十进制的结果是:{$int1}<br>";
echo "八进制的结果是:{$int2}<br>";
echo "十六进制的结果是:{$int3}<br>";
结果
十进制的结果是: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视频教程

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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software