PHP 数据类型 之 标量数据类型
写在前面
在程序员开发世界,程序操作的对象是数据,并且每一个数据都有其类型,具备相同类型的数据才可以互相操作,PHP 的数据类型可分为三种:标量数据类型、复合数据类型和特殊数据类型。
一、数据类型
1.标量数据类型
标量数据类型是数据结构最基础的单元,只能存储一个数据。在 PHP 中的标量数据类型分为四种:
类型 | 功能 |
---|---|
boolean(布尔型) | 最简单的数据类型,只有两个值:true(真) / false(假) |
string(字符串) | 字符串是连续的字符序列 |
integer(整型) | 整型数据类型包含所有的整数,其可以是整数也可以是负数 |
float(浮点型) | 浮点型数据类型也是用来存储数字,与整型不同它包含小数 |
1)布尔型(boolean)
布尔型是 PHP 中较为常用的数据类型之一,它保存一个真值(true) 或 假值(false)
// 代码:$a = true; // 真值$b = false; // 假值
2)字符串(string)
字符串是连续的字符序列,由数字、字母、符号组成,字符串的每个字符只占一个字节。字符包含以下几种类型
字符类型名 | 内容 |
---|---|
数字类型 | 如:1、2、3等 |
字母类型 | 如:a、b、c等 |
特殊类型 | 如:#、$、^、&等 |
不可见类型 | 如:\n(换行符)、\r(回车)、\t(tab字符)等 |
不可见字符是比较特殊的字符用于控制字符串格式输出,在浏览器上不可见,只是能看到字符串的输出结果。
在 PHP 中有 3 种定义字符串的方式:
a.单引号(')$a = 'zZ爱吃菜';
b.双引号(")$b = "zZ爱吃菜";
单引号与双引号的区别:双引号所包含的变量会自动被替换成实际值,而单引号包含的变量则按普通类型输出。例如:
$a = 'hello';$b = '$a china';$c = "$a world"; // 个人建议这样写:$c = "{$a} world"; 不容易产生歧义echo $b;echo $c;结果:$a chinahello world
如果用传统的输出方法——按字符串输出的话,肯定要有大量的转义符来对字符串中的引号等特殊字符进行转义,以免出现语法错误。如果是一两处还可以容忍,但是要是一个完整的html文本或者是一个200行的js我想是谁都会崩溃的。这就是PHP为什么要引入一个定界符的原因——至少一大部分原因是这样的。1.PHP 定界符的作用就是按照原样,包括换行格式什么的,输出在其内部的东西;2.PHP 定界符中字符串内容不需要转义
// 定义<<<Eof …… Eof;
不需要对付出转义的好处:直接输出你想要的 html 字符串
$name = 'kitty';echo <<<Eof<table height="20"><tr><td>{$name}<br/><script>var p='hello world';document.writeln(p);</script></td></tr></table><select><option>aaaaa</option><option>aaaaa</option><option>aaaaa</option></select>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 = 348echo $c;结果:348

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.
