Home Backend Development PHP Tutorial Summary of PHP data types_PHP tutorial

Summary of PHP data types_PHP tutorial

Jul 13, 2016 pm 05:50 PM
false int php true value name Summarize data type express

PHP has a total of 8 data types:

 类型名称  类型表示  取值
 bool  布尔型   true,false
 integer  整型 -2147483647-2147483648
 string  字符串型  字符串长度取决于机器内存
 float  浮点型  最大值1.8e308
 object  对象 通过new实例化 $obj=new person(); 
 array  数组类型  $arr=array(1,2,3,4,5,6);//一维数组
 resourse    
 null  空值  null
Type name

Type representation

Value

bool Boolean true,false
integer Integer type -2147483647-2147483648
string String type The string length depends on the machine memory
float Floating point type Maximum value 1.8e308
object Object Instantiated by new $obj=new person();
array Array type $arr=array(1,2,3,4,5,6);//One-dimensional array
resource
null Null value null

Boolean bool:
 转换  结果
 布尔型的false var_dump((bool) false)  bool(false)
 整型0  var_dump((bool) 0);  bool(false)
 浮点型0.0  var_dump((bool) 0.0);  bool(false)
 字符串‘0’ var_dump((bool) '0');  bool(false)
 空数组$arr=array(); var_dump((bool) $arr)  bool(false)
 不包含任何成员变量的空对象只在PHP4使用,PHP5中为true  bool(false)
 NULL或者尚未赋值的变量var_dump((bool) NULL)  bool(false)
 从没有任何标记(tags)的XML文档生成的SimpleXML 对象  bool(false)
For other types we can use (bool) or (boolean) for forced conversion eg: (bool)1=true; The following situations default to false during forced conversion: ​
Convert Results
Boolean false var_dump((bool) false) bool(false)
Integer type 0 var_dump((bool) 0); bool(false)
Floating point type 0.0 var_dump((bool) 0.0); bool(false)
String ‘0’ var_dump((bool) ‘0’); bool(false)
Empty array $arr=array(); var_dump((bool) $arr) bool(false)
Empty objects that do not contain any member variables are only used in PHP4 and are true in PHP5 bool(false)
NULL or a variable that has not yet been assigned a value var_dump((bool) NULL) bool(false)
SimpleXML object generated from an XML document without any tags bool(false)


The conversion result of string '0.0' is bool(true)
Note: -1 and other non-zero values ​​(whether positive or negative) are true

Integer type integer:
The range of integer type is -2147483647--2147483647. If the value exceeds this value, it will be automatically converted to float type
We can use echo PHP_INT_SZIE to output the word length of integer, which depends on the machine. echo PHP_INT_MAX outputs the maximum value of integer
There is no integer division operation in PHP. If you execute 1/2, it will produce float 0.5. If you want to achieve the integer division effect, you can use (int)(1/2)=0 or round(25/7)=4
Force conversion to integer type (int) or (integer) bool type true is converted to 1, false is converted to 0

Floating point type float:
Value range Maximum value: 1.8e308 I don’t know what the minimum value is? Please let the experts know
The word length of floating point numbers is also related to the machine. It seems that there is no PHP_FLOAT_SIZE. Please let me know how to get the length of floating point numbers

String type string:
4 ways to define strings:
1. Single quotes
2.Double quotes
3.heredoc syntax structure
4.nowdoc syntax structure (after PHP5.3.0)
Single quote
Single quotes define the original string, and everything inside is processed as a string. If the string contains single quotes, you can use escape
Double quotes
Strings defined by double quotes will parse some special characters (n, b) and variables
Instead of converting a variable to a string, you can place it in double quotes:
$num=10;
$str = "$num"; //$str is a string type 10
heredoc syntax structure
<< The string itself
Identifier
The identifier at the end must be at the beginning of the line, and the definition format of the identifier must also follow the rules defined by PHP. It can only contain numbers, letters, and underscores, and cannot start with a number or underscore
No other characters are allowed on which line of the end identifier. You can add a semicolon after the identifier. There can be no tabs or spaces before and after the semicolon. Otherwise, PHP will not be able to parse the identifier and will continue to search for the identifier. If If it is not found before the end of the file, an error will be generated
A heredoc is a double quote without double quotes, that is, it can contain double quotes without escaping, and it can parse special characters and variables
nowdoc syntax structure
<<<'Identifier'
The string itself
Identifier www.2cto.com
The start identifier of nowdoc must be enclosed in single quotes, and the end identifier and other rules are the same as heredoc
nowdoc means single quotes without single quotes. The string contained in nowdoc will be output as is, and the special characters and variables contained in it will not be parsed

If double quotes contain several situations in array variables
//We first define the following array
[php]
1. $arr=array(
2. 'one'=>array(
3. 'name'=>'jiangtong',
4. 'sex'=>'male'
5. ),
6. 'two'=>'zhaohaitao',
7. 'three'=>'fanchangfa'
8. );

The first element in the array above is two-dimensional, and the last two are one-dimensional. When we access one dimension, there are several ways:
[php]
1. echo "$arr[two]"//key has no single quotes
2. echo "$arr['two']"//key has single quotes, which will cause an error. If we change it to echo "{$arr['two']}"; the result can be correctly output
3. echo "{$arr[two]}"//There are double curly braces, but the key does not have single quotes. In this case, PHP will first look for the constant banana, and if so, replace it. Since there is no two constant, an error will occur.
It can be seen that when accessing a one-dimensional array, either the key is not added or the quotation marks are added (considering the third situation). If it is added, it will be enclosed by {}, and it can be omitted at all.
Multidimensional array test
[php]
1. echo "$arr[one][name]"; //The output result is Array[name]. It can be seen that it returns an array and only parses one dimension
2. echo "{$arr['one']['name']}";//The output result is jiangtong
Braces must be used when accessing multi-dimensional arrays, and the key must be enclosed in double quotes

Array type
As mentioned in the string type, it is legal if it is enclosed in curly brackets without adding key quotes. Then PHP will first look for whether there is a constant named key. If there is a constant named key, it will be replaced. If not, it will be generated. A warning that a constant cannot be found is treated as an ordinary string, so it is recommended that you always add single quotes
Convert to an array using (array)type or array(type), but if you convert a value with only one value to an array, you will get an array of one element, and the subscript is 0. Converting NULL to an array will get an empty array
We can change the value of the array when traversing the array. In PHP5.0 and above, we can use references
[php]
1. $arr=array('a','b','c','d','e' );
2. foreach($arr as &$value)
3. {
4. $value=strtoupper($value);
5. echo $value;
6. }//Output result ABCDE

Object object type
To instantiate an object, we use new to add a person class. We can use the following methods

[php]
1. $objPerson=new person();

Coercion (object): If an object is converted into an object, it will not have any changes. For any other value, an object of stdclass will be instantiated. If the value is NULL, an empty object will be instantiated. If When the array is converted into an object, the key of the array will be used as the attribute of the object, and the value will be the attribute value. For other types of values, the member variable named scalar contains the value
[php]
1. $arr=array('one'=>'a','two'=>'b' );
2. $obj=(object)$arr;
3. echo $obj->one //The output result is a;
Note: This is an array of keys. If there is no array of character keys, I don’t know how to access it. If anyone knows, I hope you can tell me, thank you.
For other values ​​
[php]
1. $obj1=(object)'jiang';
2. echo $obj1->scalar;//output result jiang

NULL empty type
null is not case-sensitive. The NULL type has only one value, indicating that a variable has no value. The variable in the following three situations is considered NULL
1. Is assigned a value of NULL
2. Has not been assigned a value yet
3. Being unset();


Excerpted from jt521xlg’s column

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478264.htmlTechArticlePHP has a total of 8 data types: Type name Type represents value bool Boolean true, false integer Integer -2147483647 -2147483648 string The length of the string string depends on the machine...
Statement of this Website
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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 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.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 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.

See all articles