Home >Backend Development >PHP Tutorial >PHP basic tutorial 2 basic data types
A brief introduction to php
The four tag formats of php
Variables of php
Basic data types of 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.
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:
We generally use the first format. It is also the most common format.
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.
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 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 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
If two floating point numbers are directly assigned, they can be compared directly.
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 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.
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
单引号:当字符串定义了单引号时,解析器不会对字符串中的变量进行解析。而是直接输出。
$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 = <<<'COO' 这是一个nowDoc$a; COO; echo $str; -----结果------ 这是一个nowDoc$a;
数据类型就是从一种类型转换到另外一种类型。转换一般可以分为两种自动类型转换、强制类型转换;
在一般情况下,当我们不知道数据是什么类型的时候可以使用var_dump(),这个函数可以打印数据的具体信息,其中就包括数据的类型。而PHP帮助文档中又提供了一种方式得到数据的类型getType()但是文档中明确表示不推荐使用:
在前面介绍过,当整型的数据越过它的范围的时候,它就会自动转换成浮点型类型。这种自动完成的类型转换就是自动类型转换。
自动类型转换的场景:
整型数过大,自动转换成浮点型。
$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, 'float');//强制类型转换 var_dump($a); -----结果------ float(100)
$type的可能值是:
“boolean” (或为“bool”,从 PHP 4.2.0 起)
“integer” (或为“int”,从 PHP 4.2.0 起)
“float” (只在 PHP 4.2.0 之后可以使用,对于旧版本中使用的“double”现已停用)
“string”
“array”
“object”
“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
尚未被赋值
被 unset()
其他的数据类型,数组,对象,资源在后面会介绍。
特别说明:在PHP中当我们想输出一个数的时候,可以使用echo进行输出,而字符串连接使用一个点.表示。也可以通过var_dump()进行输出,var_dump()可以输出当前数据的类型是什么。
基本数据类型使我们必须要掌握的,在以后的开发中,我们操作的数据都是基本数据类型。
php的简单介绍
php的四种标签格式
php的变量
php的基础数据类型
php是开源的脚本语言,主要用于web开发,语法吸收了C语言,Java和perl的特点,利于学习,使用广泛,用php做出来的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML文档中去执行,执行速度更快。
当php的代码写到固定的格式中时,解析器才会去执行php代码,而常见的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.
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 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 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
If two floating point numbers are directly assigned, they can be compared directly.
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 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.
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
$a = 12; $b = '这是一个单引号$a'; echo $b; -----结果------ 这是一个单引号$aDouble quotes: The parser will parse the variables in the string.
$a = 12; $b = "这是一个单引号$a"; echo $b; -----结果------ 这是一个单引号12Heredoc: 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 = <<<'COO' 这是一个nowDoc$a; COO; echo $str; -----结果------ 这是一个nowDoc$a;
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 datagetType(), but the document clearly states that it is not recommended:
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, 'float');//强制类型转换 var_dump($a); -----结果------ float(100)
$type的可能值是:
“boolean” (或为“bool”,从 PHP 4.2.0 起)
“integer” (或为“int”,从 PHP 4.2.0 起)
“float” (只在 PHP 4.2.0 之后可以使用,对于旧版本中使用的“double”现已停用)
“string”
“array”
“object”
“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
尚未被赋值
被 unset()
其他的数据类型,数组,对象,资源在后面会介绍。
特别说明:在PHP中当我们想输出一个数的时候,可以使用echo进行输出,而字符串连接使用一个点.表示。也可以通过var_dump()进行输出,var_dump()可以输出当前数据的类型是什么。
基本数据类型使我们必须要掌握的,在以后的开发中,我们操作的数据都是基本数据类型。
以上就是PHP基础教程二之基本数据类型的内容,更多相关内容请关注PHP中文网(www.php.cn)!