search
HomeBackend DevelopmentPHP TutorialPHP basic tutorial 2 basic data types

The content explained in this section

  • A brief introduction to php

  • The four tag formats of php

  • Variables of php

  • Basic data types of php

A brief introduction to PHP

PHP is an open source scripting language, mainly used for web development. The syntax absorbs the characteristics of C language, Java and perl, which is easy to learn and widely used. It is made with PHP Dynamic pages Compared with other programming languages, PHP embeds programs into HTML documents for execution, and the execution speed is faster.

PHP’s four tag formats

When the PHP code is written in a fixed format, the parser will execute the PHP code, and common PHP tags There are four types:

PHP basic tutorial 2 basic data types

We generally use the first format. It is also the most common format.

PHP Variable

 Variable: refers to a number that does not have a fixed value and can be changed.

 The purpose of creating variables is to store data so that we can later operate on the data.

  Naming rules for variable names: they must be composed of numbers, letters, and underscores and cannot start with a number.

Common naming rules:

  • Camel case naming method: username->>>userName: Starting from the second word, the first letter is capitalized

  • Pascal nomenclature: username->>>UserName: The first letter of each word is capitalized

  • Underscore nomenclature: username-> >>user_name: Words are all lowercase, and words are separated by underscores. Common naming rules:

The naming rules for function names in the future can also be summarized into these three points.

PHP's variable names have a special feature: a $ symbol must be added in front of the variable, which is shift + 4; at the same time, for the convenience of development, the name of the variable must generally have a meaning.

PHP basic data types

 Most languages ​​have the term data type. Data types can classify the data we use in development. It's also for better management.

PHP’s basic data type classification:

  • Basic data types: integer (int/integer), floating point (float/double), Boolean (bool) /boolean), string(string)

  • Composite data type: array, object(object)

  • Special data type: Empty (null), resource (resource)

Integer data type

 Integer data type is the negative integer we usually use, Positive integers, etc.;

The integer type in PHP can be octal (every eight is one), decimal, hexadecimal (hexadecimal).

$a = 2; //十进制
$a = 023;//八进制
$a = 0x23;//十六进制

Octal: There is a 0 in front, indicating octal.

Hexadecimal: There are two 0x in front, indicating hexadecimal.

The size of integers has a limit. The word length of PHP's integer type depends on the platform. It is generally 4 bytes, and 4 bytes is 32 bits. Therefore, the length of PHP is generally 2 to the 31st power, and one of them represents the sign bit.

In PHP, you can use the system constant PHP_INT_MAX to get the maximum value. But when the integer value exceeds the maximum value, it will be automatically converted to float type.

Note: Unsigned numbers are not supported in PHP.

Floating point data type

  Floating point data type is what we usually call decimals. Also called double precision or real number.

The word length of the string is also platform-dependent, generally 1.8e308, and has a precision of 14 decimal digits. The precision of decimals is generally calculated from the first non-0 number from the left.

echo 123456.78912345123;//输出结果123456.78912345--最后面的123四舍五入掉

echo 0000123456789.256486587441;//输出结果123456789.25649--6587441四舍五入向前进一位8变9

Regarding the comparison of floating point types, it should be processed according to different situations

  1. If two floating point numbers are directly assigned, they can be compared directly.

  2. If one or more of the two floating point numbers are the result of an operation, you need to use the abs or round function to expand the multiple and compare.

Boolean data type

 Boolean data type is true (true) or false (false), but in addition to these two forms, when the value of other data types is in a certain This condition is also true or false.

The following situations will be treated as false:

  • Integer value 0

  • Floating point value 0.0

  • Empty string and string '0'

  • An array that does not include any elements

  • Does not include any Object of members

  • Special type null

  • SimpleXML object generated from empty tags.

String data type

 A string is a series of characters plus quotation marks, and the maximum string size in PHP can be 2GB;

We often use strings in development, and the definition of strings can be roughly divided into four types:

  • Single quotes

  • Double quotes

  • ##Heredoc (double quotes)

  • nowdoc (single quotes)

单引号:当字符串定义了单引号时,解析器不会对字符串中的变量进行解析。而是直接输出。

$a = 12;
$b = '这是一个单引号$a';
echo $b;
-----结果------
这是一个单引号$a

双引号:解析器会对字符串中的变量进行解析。

$a = 12;
$b = "这是一个单引号$a";
echo $b;
-----结果------
这是一个单引号12

Heredoc:当定义的字符串过长的时候,可以考虑使用者中方式,这种方式会对字符串中的变量进行解析。

$a = 12;
$b = <<<EOP//这个大写字母的定义可以随意。
这是一个hereDoc的类型$a;
EOP;
echo $b;
-----结果------
这是一个hereDoc的类型12;

注意:EOP的前后不能有空格或其他任何的字符

nowdoc:这种方式不会对字符串中的变量进行解析。

$a = 12;
$str = <<<&#39;COO&#39;
    这是一个nowDoc$a;
COO;
echo $str;
-----结果------
这是一个nowDoc$a;

基本数据类型转换

  数据类型就是从一种类型转换到另外一种类型。转换一般可以分为两种自动类型转换、强制类型转换

在一般情况下,当我们不知道数据是什么类型的时候可以使用var_dump(),这个函数可以打印数据的具体信息,其中就包括数据的类型。而PHP帮助文档中又提供了一种方式得到数据的类型getType()但是文档中明确表示不推荐使用:

PHP basic tutorial 2 basic data types

自动类型转换

  在前面介绍过,当整型的数据越过它的范围的时候,它就会自动转换成浮点型类型。这种自动完成的类型转换就是自动类型转换。
自动类型转换的场景:

整型数过大,自动转换成浮点型。

$a = PHP_INT_MAX;//表示整型的最大值
var_dump($a); //输出$a的类型
$a = PHP_INT_MAX + 1; //$a的值大于整型的最大值。
var_dump($a);
-----结果------
int(2147483647) float(2147483648)

当进行数值运算的时候,经常发生自动类型转换。

$a = 1; //整型
$b = 3.14; //浮点型
$res = $a + $b; //计算两个数的和
var_dump($res); //输出类型
-----结果------
float(4.14)

$a = 10;//整型
$b = 4;//整型
$res = $a / $b; //相除
var_dump($res);
-----结果------
float(2.5)

强制类型转换

  当我们想强制把当前的类型转换成其他类型的时候,可以使用强制类型转换:

使用bool settype ( mixed &

type )

$a = 100; //整型
settype($a, &#39;float&#39;);//强制类型转换
var_dump($a);
-----结果------
float(100)

$type的可能值是:

  1. “boolean” (或为“bool”,从 PHP 4.2.0 起)

  2. “integer” (或为“int”,从 PHP 4.2.0 起)

  3. “float” (只在 PHP 4.2.0 之后可以使用,对于旧版本中使用的“double”现已停用)

  4. “string”

  5. “array”

  6. “object”

  7. “null” (从 PHP 4.2.0 起)

使用类型

$a = 200;
$b = (string)$a;
var_dump($b);
-----结果------
string(3) "200"

使用函数得到对应的数据类型,比如intval , floatval ,boolval, strval

$a = 12.923;//浮点型
$b = intval($a);
var_dump($b);
-----结果------
int(12)

注意:当浮点型转换成整型的时候,会自动忽略小数点后的数,并不会四舍五入。

NULL数据类型

  NULL:表示一个变量没有值,NULL 类型唯一可能的值就是 NULL。

在下列情况下一个变量被认为是 NULL:

  • 被赋值为 NULL

  • 尚未被赋值

  • 被 unset()

其他的数据类型,数组,对象,资源在后面会介绍。

特别说明:在PHP中当我们想输出一个数的时候,可以使用echo进行输出,而字符串连接使用一个点.表示。也可以通过var_dump()进行输出,var_dump()可以输出当前数据的类型是什么。

总结

  基本数据类型使我们必须要掌握的,在以后的开发中,我们操作的数据都是基本数据类型。

本节讲解的内容

  • php的简单介绍

  • php的四种标签格式

  • php的变量

  • php的基础数据类型

PHP的简单介绍

  php是开源的脚本语言,主要用于web开发,语法吸收了C语言,Java和perl的特点,利于学习,使用广泛,用php做出来的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML文档中去执行,执行速度更快。

PHP的四种标签格式

当php的代码写到固定的格式中时,解析器才会去执行php代码,而常见的PHP标签有四种:

PHP basic tutorial 2 basic data types

我们一般用的是第一种格式。也是最常见的格式。

PHP变量

  变量:是指没有固定的值,可以改变的数。

  而我们创建变量的目的是:为了存放数据,以便后来对数据进行操作。

  变量名的命名规则:必须是数字,字母,下划线组成的且不能以数字开头的。

常用命名规则:

  • 驼峰式命名法:username->>>userName: 从第二个单词开始,首字母大写

  • Pascal nomenclature: username->>>UserName: The first letter of each word is capitalized

  • Underscore nomenclature: username-> >>user_name: Words are all lowercase, and words are separated by underscores. Common naming rules:

The naming rules for function names in the future can also be summarized into these three points.

PHP's variable names have a special feature: a $ symbol must be added in front of the variable, which is shift + 4; at the same time, for the convenience of development, the name of the variable must generally have a meaning.

PHP basic data types

 Most languages ​​have the term data type. Data types can classify the data we use in development. It's also for better management.

PHP’s basic data type classification:

  • Basic data types: integer (int/integer), floating point (float/double), Boolean (bool) /boolean), string(string)

  • Composite data type: array, object(object)

  • Special data type: Empty (null), resource (resource)

Integer data type

 Integer data type is the negative integer we usually use, Positive integers, etc.;

The integer type in PHP can be octal (every eight is one), decimal, hexadecimal (hexadecimal).

$a = 2; //十进制
$a = 023;//八进制
$a = 0x23;//十六进制

Octal: There is a 0 in front, indicating octal.

Hexadecimal: There are two 0x in front, indicating hexadecimal.

The size of integers has a limit. The word length of PHP's integer type depends on the platform. It is generally 4 bytes, and 4 bytes is 32 bits. Therefore, the length of PHP is generally 2 to the 31st power, and one of them represents the sign bit.

In PHP, you can use the system constant PHP_INT_MAX to get the maximum value. But when the integer value exceeds the maximum value, it will be automatically converted to float type.

Note: Unsigned numbers are not supported in PHP.

Floating point data type

  Floating point data type is what we usually call decimals. Also called double precision or real number.

The word length of the string is also platform-dependent, generally 1.8e308, and has a precision of 14 decimal digits. The precision of decimals is generally calculated from the first non-0 number from the left.

echo 123456.78912345123;//输出结果123456.78912345--最后面的123四舍五入掉

echo 0000123456789.256486587441;//输出结果123456789.25649--6587441四舍五入向前进一位8变9

Regarding the comparison of floating point types, it should be processed according to different situations

  1. If two floating point numbers are directly assigned, they can be compared directly.

  2. If one or more of the two floating point numbers are the result of an operation, you need to use the abs or round function to expand the multiple and compare.

Boolean data type

 Boolean data type is true (true) or false (false), but in addition to these two forms, when the value of other data types is in a certain This condition is also true or false.

The following situations will be treated as false:

  • Integer value 0

  • Floating point value 0.0

  • Empty string and string '0'

  • An array that does not include any elements

  • Does not include any Object of members

  • Special type null

  • SimpleXML object generated from empty tags.

String data type

 A string is a series of characters plus quotation marks, and the maximum string size in PHP can be 2GB;

We often use strings in development, and the definition of strings can be roughly divided into four types:

  • Single quotes

  • Double quotes

  • ##Heredoc (double quotes)

  • nowdoc (single quotes)

Single quotes: When the string defines single quotes, the parser will not parse the variables in the string. Instead, it is output directly.

$a = 12;
$b = &#39;这是一个单引号$a&#39;;
echo $b;
-----结果------
这是一个单引号$a

Double quotes: The parser will parse the variables in the string.

$a = 12;
$b = "这是一个单引号$a";
echo $b;
-----结果------
这是一个单引号12

Heredoc: When the defined string is too long, you can consider the user method, which will parse the variables in the string.

$a = 12;
$b = <<<EOP//这个大写字母的定义可以随意。
这是一个hereDoc的类型$a;
EOP;
echo $b;
-----结果------
这是一个hereDoc的类型12;

Note: There cannot be spaces or any other characters before and after EOP

nowdoc: This method will not parse the variables in the string.

$a = 12;
$str = <<<&#39;COO&#39;
    这是一个nowDoc$a;
COO;
echo $str;
-----结果------
这是一个nowDoc$a;

Basic data type conversion

 Data type is to convert from one type to another type. Conversion can generally be divided into two types

automatic type conversion and forced type conversion;

In general, when we don’t know what type of data we can use var_dump(), this function Specific information of the data can be printed, including the type of data. The PHP help document provides a way to get the type of data

getType(), but the document clearly states that it is not recommended:

PHP basic tutorial 2 basic data types

Automatic type conversion

As mentioned before, when integer data exceeds its range, it will be automatically converted to a floating-point type. This automatically completed type conversion is automatic type conversion.

Automatic type conversion scenarios:

整型数过大,自动转换成浮点型。

$a = PHP_INT_MAX;//表示整型的最大值
var_dump($a); //输出$a的类型
$a = PHP_INT_MAX + 1; //$a的值大于整型的最大值。
var_dump($a);
-----结果------
int(2147483647) float(2147483648)

当进行数值运算的时候,经常发生自动类型转换。

$a = 1; //整型
$b = 3.14; //浮点型
$res = $a + $b; //计算两个数的和
var_dump($res); //输出类型
-----结果------
float(4.14)

$a = 10;//整型
$b = 4;//整型
$res = $a / $b; //相除
var_dump($res);
-----结果------
float(2.5)

强制类型转换

  当我们想强制把当前的类型转换成其他类型的时候,可以使用强制类型转换:

使用bool settype ( mixed &

type )

$a = 100; //整型
settype($a, &#39;float&#39;);//强制类型转换
var_dump($a);
-----结果------
float(100)

$type的可能值是:

  1. “boolean” (或为“bool”,从 PHP 4.2.0 起)

  2. “integer” (或为“int”,从 PHP 4.2.0 起)

  3. “float” (只在 PHP 4.2.0 之后可以使用,对于旧版本中使用的“double”现已停用)

  4. “string”

  5. “array”

  6. “object”

  7. “null” (从 PHP 4.2.0 起)

使用类型

$a = 200;
$b = (string)$a;
var_dump($b);
-----结果------
string(3) "200"

使用函数得到对应的数据类型,比如intval , floatval ,boolval, strval

$a = 12.923;//浮点型
$b = intval($a);
var_dump($b);
-----结果------
int(12)

注意:当浮点型转换成整型的时候,会自动忽略小数点后的数,并不会四舍五入。

NULL数据类型

  NULL:表示一个变量没有值,NULL 类型唯一可能的值就是 NULL。

在下列情况下一个变量被认为是 NULL:

  • 被赋值为 NULL

  • 尚未被赋值

  • 被 unset()

其他的数据类型,数组,对象,资源在后面会介绍。

特别说明:在PHP中当我们想输出一个数的时候,可以使用echo进行输出,而字符串连接使用一个点.表示。也可以通过var_dump()进行输出,var_dump()可以输出当前数据的类型是什么。

总结

  基本数据类型使我们必须要掌握的,在以后的开发中,我们操作的数据都是基本数据类型。

 以上就是PHP基础教程二之基本数据类型的内容,更多相关内容请关注PHP中文网(www.php.cn)!


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
PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and ComparisonPHP and Python: Code Examples and ComparisonApr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

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: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

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.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment