PHP Data Type Summary _php tutorial

Source: Internet
Author: User
PHP Total 8 data type in:
type name type represents Take value
bool Boolean type True,false
Integer Integral type -2147483647-2147483648
String String type String length depends on machine memory
Float Floating point Type Maximum Value 1.8e308
Object Object Instantiate $obj =new person () through new;
Array Array type $arr =array (1,2,3,4,5,6);//one-dimensional array
Resourse
Null Null value Null


boolean bool:

For other types we can use (bool) or (Boolean) to cast an eg: (bool) 1=true;

The following conditions are false by default when forced conversions:


Conversion Results
False Var_dump ((bool) false) for the Boolean type BOOL (FALSE)
Integral type 0 var_dump ((bool) 0); BOOL (FALSE)
Float 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 used only in PHP4 and true in PHP5 BOOL (FALSE)
Null or a variable that has not been assigned Var_dump ((bool) NULL) BOOL (FALSE)
SimpleXML object generated from an XML document without any tags (tags) BOOL (FALSE)


The string ' 0.0 ' converts the result to bool (true)
Note:-1 and other non-0 values (either positive or negative) are true

Integer integer:
Integral type range -2147483647--2147483647, if more than this value is automatically converted to float type
We can use echo Php_int_szie to output the word length of an integer, which is related to the machine. echo Php_int_max The maximum value of the output integer
There is no division operation in PHP if execution 1/2 produces a float of 0.5 if you want to achieve the division effect, you can use (int) (1/2) =0 or use round (25/7) =4
Cast to Integer (int) or (integer) bool type ture converted to 1, false to 0

Floating-point float:
Maximum value range: 1.8e308 do not know what the minimum value is? Ask the Master to inform
The length of the floating-point number is also related to the machine, as if there is no php_float_size ah, ask the master to tell how to the floating-point digital long

String-type string:
4 ways to define a string:
1. Single quotation marks
2. Double quotes
3.HEREDOC syntax structure
4.NOWDOC syntax structure (after PHP5.3.0)
Single quotation marks
Single quotes define the most primitive strings, all of which are processed by string, and can be escaped if the string contains single quotes
Double quotes
The string defined by double quotation marks resolves some special characters (\n,\b) and variables
You can place a variable in double quotation marks instead of converting a variable to a string (string):
$num = 10;
$str = "$num"; $STR is 10 of the string type
HEREDOC syntax structure
<< <标示符
<>
The string itself
Identifiers
At the end of the identifier must be at the beginning of a line, and the definition of the identifier must be in accordance with the rules defined by PHP, can only contain numbers, letters, underscores, and cannot start with a numeric underscore
End identifier which line does not allow other characters, you can add a semicolon after the identifier, the Shard number must not have a tab or a space, or PHP will not be able to resolve the identifier, will continue to look down the identifier, if not found before the end of the file will produce an error
Heredoc is a double quote that does not use double quotes, which can contain double quotes without escaping, and can parse special characters and variables
NOWDOC syntax structure
<<< ' identifiers '
The string itself
Identifier www.2cto.com
Nowdoc start identifiers must be enclosed in single quotes, end identifiers and other rules are heredoc
Nowdoc is a single quotation mark that does not use single quotes, Nowdoc contains strings that are output as-is, with special characters and variables that are not parsed

If the double quotation marks contain several cases in the array variable
Let's first define the following array
[PHP]
1. $arr =array (
2. ' One ' =>array (
3. ' Name ' = ' Jiangtong ',
4. ' Sex ' = ' male '
5.),
6. ' Both ' = ' Zhaohaitao ',
7. ' Three ' = ' FANCHANGFA '
8.);

Above is the first element in the array two-dimensional, the latter two are one-dimensional, we visit one dimension in the following ways:
[PHP]
1. echo "$arr [two]"//key no single quotes
2. echo "$arr [' two ']"// Key has a single quote error, if we change to echo "{$arr [']}"; You can correctly output the result
3. echo "{$arr [two]}"//have double curly braces, but the key does not have single quotation marks in this case PHP will first look for a constant banana, some words will be replaced, because there is no
can be seen when accessing a one-dimensional array either without a key without quotation marks (considering the third case), the addition is to be surrounded by {}, it is completely non-additive.
Multidimensional array test
[PHP]
1. echo "$arr [One][name]";//output as Array[name] visible it returns an array, parsing only one dimension
2. echo "{$arr [" ' One '] [' Name '} ';//The output is Jiangtong
The curly braces must be used in access to the multidimensional array key must be enclosed in double quotation marks

Array type
is already mentioned in the string type, enclosed in curly braces, and is legal if no key quotes are added. , then PHP will first look for a constant known as key, and some will be replaced, there will not be a constant can not find a warning to the normal string processing, it is recommended that you must add a single quotation mark
to convert the array type or array (type), However, if you convert an array with only one value, and you get a valarray of an element with a subscript of 0, the null conversion array will get an empty arrays
We can change the value of the array while iterating over the array, and you can use the reference implementation
[PHP]
1 for PHP5.0. $arr =array (' A ', ' B ', ' C ', ' d ', ' e ');
2. foreach ($arr as & $value)
3. {
4. $value =strtoupper ($value);
5. Echo $value;
6.}//Output Results ABCDE

Object type
Instantiating an object we use new to include a person class, which we can do in the following ways

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

Cast (object): If an object is converted to an object then it has no change, and for any other value it instantiates an Stdclass object, and if the value is null, an empty object is instantiated, and if the array is converted to an object then the array's key is used as the object's property. Value is a property value, and a member variable named scalar contains the value of the other type
[PHP]
1. $arr =array (' one ' = ' a ', ' one ' and ' B ');
2. $obj = (object) $arr;
3. Echo $obj->one//output result is a;
Note: This is an array of keys by key, if there is no character in the array of key, I do not know how to access, who knows want to tell the younger brother, thank you.
For other values
[PHP]
1. $obj 1= (object) ' Jiang ';
2. Echo $obj 1->scalar;//output Results jiang

Null empty type
NULL case insensitive, NULL type has only one value, indicates a variable has no value, the following three case variables are considered null
1. Assigned value is NULL
2. Has not been assigned a value
3. Being unset ();


Excerpt from JT521XLG's column

http://www.bkjia.com/PHPjc/478264.html www.bkjia.com true http://www.bkjia.com/PHPjc/478264.html techarticle PHP has 8 data types: Type name type represents the value bool Boolean type True,false integer integer -2147483647-2147483648 string string string length depends on the machine inside ...

  • 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.