Summary of PHP Data types

Source: Internet
Author: User
Tags integer division
PHP has a total of 8 data types: type name type indicates the value is Boolean true, falseinteger-2147483647-2147483648string string length depends on the machine memory float maximum 1.8e308object object instantiated through new $ objnewperson ()... PHP Total 8 Data type in:
 Type name  Type representation  Value
Bool Boolean True, false
Integer Integer -2147483647-2147483648
String String type The string length depends on the machine memory.
Float Floating point type Maximum 1.8e308
Object Object Instantiate $ obj = new person () through new ();
Array Array type $ Arr = array (1, 2, 3, 4, 5, 6); // A one-dimensional array
Resourse    
Null Null value Null
 

Boolean bool:

For other types, we can use (bool) or (boolean) to forcibly convert eg :( bool) 1 = true;

In the following cases, the default value is false for forced conversion:


 
 Conversion  Result
Boolean false var_dump (bool) false) Bool (false)
Integer 0 var_dump (bool) 0 ); Bool (false)
Floating point 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 used only in PHP4, and true in PHP5 Bool (false)
NULL or the variable var_dump (bool) NULL) that has not been assigned a value) Bool (false)
SimpleXML object generated from an XML document without any tags Bool (false)
 

String '0. 0' is converted to bool (true)
Note:-1 and other non-zero values (both positive and negative) are true.
 
Integer:
The integer value range is-2147483647--2147483647. if this value is exceeded, it is automatically converted to float type.
We can use echo PHP_INT_SZIE to output the integer length, which is related to the machine. Echo PHP_INT_MAX outputs the maximum integer value
PHP does not perform an integer division operation. If 1/2 is executed, float 0.5 is generated. if you want to perform the integer division, you can use (int) (1/2) = 0 or round (25/7) = 4.
Forcibly convert to integer (int) or (integer) bool type true to 1, false to 0
 
Float:
Maximum Value: 1.8e308. what is the minimum value? Please advise
The font length of a floating point number is also related to the machine. it seems that there is no PHP_FLOAT_SIZE. please tell me how to extend it to a floating point number.
 
String type string:
Four methods for defining strings:
1. single quotes
2. double quotation marks
3. heredoc syntax structure
4. nowdoc syntax structure (after PHP5.3.0)
Single quotes
The single quotation mark defines the original string, and all content in it is processed by the string. if the string contains single quotation marks, it can be escaped \.
Double quotation marks
Strings defined by double quotation marks will parse some special characters (\ n, \ B) and variables
You can place variables in double quotation marks instead of converting them into strings ):
$ Num = 10;
$ Str = "$ num"; // $ str is 10 of the string type
Heredoc syntax structure
< <标示符
String itself
Identifier
The identifier at the end must be at the beginning of a line, and the definition format of the identifier must follow the rules defined by PHP. it can only contain numbers, letters, and underscores, and cannot start with a number underline.
If a line of the ending identifier does not contain any other character, you can add a semicolon after the identifier, and there cannot be tabs or spaces before and after the semicolon; otherwise, PHP will not be able to parse the identifier, the identifier will be searched down. if it is not found before the end of the file, an error will be generated.
Heredoc is a double quotation mark without double quotation marks. it can contain double quotation marks without escaping and can parse special characters and variables.
Nowdoc syntax structure
<'Identifier'
String itself
Www.2cto.com
The start identifier of nowdoc must be enclosed in single quotes. The end identifier and other rules are the same as those of heredoc.
Nowdoc is a single quotes without single quotes. the strings contained in nowdoc are output as they are, and the special characters and variables contained in nowdoc are not parsed.
 
If double quotation marks contain several situations in array variables
// Define the following array first
[Php]
1. $ arr = array (
2. 'one' => array (
3. 'name' => 'jiangtongg ',
4. 'sex' => 'male'
5 .),
6. 'two' => 'zhaohaitao ',
7. 'Three '=> 'fanchangfa'
8 .);

The first element in the array is two-dimensional, and the last two are one-dimensional. when we access one-dimensional, the following methods are provided:
[Php]
1. echo "$ arr [two]" // The key does not have single quotes.
2. echo "$ arr ['two']" // an error occurs when the key has single quotes. if we change it to echo "{$ arr ['two']}", the result can be output correctly.
3. echo "{$ arr [two]}" // There are double braces, but the key does not have single quotes. in this case, PHP will first find whether the constant banana exists. If yes, it will be replaced, an error occurred because no two constant exists.
It can be seen that when accessing a one-dimensional array, either the key is not enclosed by quotation marks (considering the third case). if it is added, it will be included.
Multi-dimensional 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 resolves one dimension.
2. echo "{$ arr ['one'] ['name']}"; // The output result is jiangtong.
The braces key must be enclosed in double quotation marks for multi-dimensional array access.
 
Array type
As mentioned in the string type, if it is legal to enclose it in braces without the key quotation marks, PHP will first look for a constant named as the key, if yes, it will be replaced. If no, it will generate a warning that the constant cannot be found, which will be processed according to the normal string. Therefore, we recommend that you add single quotation marks.
To convert to an array, use (array) type or array (type). However, if only one value is converted to an array, an array of elements is obtained and the subscript is 0, convert NULL to an array to obtain an empty array.
We can change the value of the array when traversing the array. you can use the reference implementation above PHP5.0.
[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 Type
To instantiate an object, we use new to add a person class. we can use the following method:
 
[Php]
1. $ objPerson = new person ();

Forced conversion (object): If an object is converted to an object, it does not change. for any other value, an object of stdclass will be instantiated. if this value is NULL, an empty object will be instantiated. If an array is converted into an object, the key of the array will be used as the object's attribute, and value is the attribute value, for other types of values, a member variable named scalar contains this value.
[Php]
1. $ arr = array ('one' => 'A', 'two' => 'B ');
2. $ obj = (object) $ arr;
3. echo $ obj-> one // The output result is;
Note: This is an array composed of keys. if there is no character key array, I don't know how to access it. I want to tell my younger brother. thank you.
For other values
[Php]
1. $ obj1 = (object) 'Jiang ';
2. echo $ obj1-> scalar; // output result jiang

NULL type
Null is case insensitive. The NULL type has only one value, indicating that a variable has no value. in the following three cases, the variable is considered NULL.
1. The value is NULL.
2. not assigned
3. unset ();

 
From the column jt521xlg

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.