Eight types of data in PHP

Source: Internet
Author: User

The dollar sign $ is the identifier of the variable, and all variables begin with the $ symbol, and the $ symbol should be used, whether declaring the variable or calling the variable.

PHP supports a total of 8 primitive types:

1, Boolean (Boolean): The value is only true or False;true and false is the internal keyword of PHP.

Typically, Boolean variables are applied in an expression of a condition or a loop statement. In PHP, not only false values are false, but in some special cases the Boolean value is also considered false. These special cases are: 0, 0.0, "0", a blank string (""), an array that declares no duplicates, and so on.

2, Integer (integer type):

An integer data type can contain only integers. Can be expressed in decimal, octal, hexadecimal. Octal digits must be added in front of 0, hexadecimal requires 0x.

Note 1: If an illegal number (8 and 9) is present in the octal system, the subsequent number is ignored.

NOTE 2: If the given value exceeds the maximum range ( -2147483648~+2147483647) that the int can represent, it will be treated as a float type, which is known as an integer overflow. Similarly, float is returned if the result of the last operation of the expression is outside the range of type int.

3, float/double (floating point type):

Floating-point data types can be used to store numbers or to save decimals. It provides a much greater precision than an integer. In a 32-bit operating system, the valid range is

1.7e-308~1.7e+308. In previous versions of PHP4.0, floating-point identifiers were double, or double-precision floating-point numbers, without distinction.

Floating-point data has two writing formats by default,

One is the standard format: 3.1415-35.8

One is scientific notation format: 3.58E1 849.72E-3

Floating-point values are just an approximation, so try to make the size of the floating-point numbers as small as possible, because the final result is often accurate.

4, String (strings):

A string is a sequential sequence of characters, consisting of numbers, letters, and symbols. Each character in a string occupies only one byte. In PHP, there are 3 ways to define strings, namely single quotation marks ('), double quotation marks ("), and delimiters (<<<).

The difference between single and double quotation marks is that the variables contained in the double quotation marks are automatically replaced with the actual values, and the variables contained in the single quotation mark are output as normal strings.

Another difference between single and double quotes: the use of escape characters. When using single quotes, simply escape the single quote "'". But when you use double quotation marks ("), you also pay attention to the use of characters such as" "", "$", and so on. These special characters are displayed by the escape character "\".

When defining simple strings, using single quotes is a more appropriate way to handle them. If you use double quotes, PHP will take some time to handle the escape of the string and the parsing of the variable.

Escape characters:

\ nthe line break

\ r Enter

\ t Horizontal tab

\ \ counter Slash

\$ dollar Sign

\ ' Single quotation mark

\ "Double quotation marks

\[0-7]{1,3} This regular expression sequence matches a character represented by an octal symbol, such as \467

\x[0-9a-fa-f]{1,2} This regular expression sequence matches a character that is represented by a hexadecimal symbol, such as \x9f

Delimiter (<<<): followed by an identifier after use, followed by a string, and finally the same identifier end string. For example:

<?php

$str = ' delimiter example '; Declaring variable $str

echo <<str//delimiter start

There is no difference between a delimiter and a double quote, and \ $str can also be output. <p>//Output string

\ $str content is: $STR//Output variable $str

Str Delimiter End

?>

The end identifier must be a separate row, and no spaces are allowed. An error also occurs when there are other symbols or characters before and after the identifier. The comment section in the above example must not be entered in the utility, otherwise there will be an error message.

5. Array (arrays):

An array is a set of data that organizes a series of data into a single, actionable whole. The array can include a lot of data, such as scalar data, arrays, objects, resources, and other syntactic structures supported in PHP.

Each of the data in the array becomes an element that includes an index (key name) and a value of two parts. The index of an element can consist of a number or a string, and the value of an element can be a multi-bell data type. Defines the syntax format for an array:

$array =array (' value1 ', ' value2 ',......);

$array [key]= ' value ';

$array =array (key1=>value1,key2=>value2,......);

Where the parameter key is the subscript of the array element, and value is the element that corresponds to the array subscript. Examples are as follows:

<?php
$arr 1=array (' This ', ' was ', ' an ', ' example ');
$arr 2[0]= ' tempname ';
$arr 3=array (1=> ' You ',2=> ' is ', ' the ' = ' and ' the ', ' str ' = ' best ');
Echo $arr 1[0]. ' <br> ';
Echo $arr 2[0]. ' <br> ';
echo $arr 3[' the ']. <br> ';
?>

Once an array is declared, the number of elements in the array can also be changed freely. As long as you assign a value to the array, the array automatically increments the length.

6. Object: There are two methods that the programming language applies To: process-oriented and object-oriented.

7. Resource (Resources): When using resources, the system automatically enables the garbage collection mechanism, frees resources that are no longer in use, and avoids memory exhaustion.

8, NULL (null value):

A null value that indicates that no value is set for the variable, and that null values (NULL) are not case sensitive.

There are 3 scenarios in which null values are assigned:

A. No value has been assigned

b, assigned value NULL

C, variables processed by the unset () function

Instance:

$string 1=null;
$string 3= ' str ';
if (Is_null ($string 1)) {
Echo ' string1 is null<br> ';
}
if (Is_null ($string 2)) {
Echo ' string2 is null<br> ';
}
Unset ($string 3);
if (Is_null ($string 3)) {
Echo ' string3 is null<br> ';
}



Operation Result:

String1 is null

http://www.onesheng.cn notice:undefined variable:string2 in E:\PHPnow\htdocs\test2.php on line 36
String2 is null

http://www.onesheng.cn notice:undefined variable:string3 in E:\PHPnow\htdocs\test2.php on line 40
String3 is null

Description: The Is_null () function is to determine whether a variable is NULL, the function returns a Boolean, or True if the variable is null, otherwise false is returned. The unset () function is used to destroy the specified variable. Starting with PHP4, the unset () function will no longer have a return value, so do not attempt to get or output unset ().

The above error is undefined $string2, unset ($string 3) does not have a return value, which is equivalent to not defining $string3 later.


Detection data type:

Is_bool Check if the variable is Boolean type Is_bool (true), Isbool (false)

is_string Check if the variable is a string type is_string (' string '), is_string (1234)

Is_float/is_double Check if the variable is floating-point type Is_float (3.1415), is_float (' 3.1415 ')

Is_integer/is_int Check if the variable is an integer Is_integer (34), Is_integer (' 34 ')

Is_null checks if the variable is null is_null (NULL)

Is_array checks if a variable is an array type Is_array ($arr)

Is_object Check if the variable is an object type Is_object ($obj)

Is_numeric checks whether a variable is a number or a string consisting of a number is_numeric (' 5 '), Is_numeric (' abcd123 ')

Eight types of data in PHP

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.