Php Data type usage

Source: Internet
Author: User
The data types in php are not as detailed as in java. below I will introduce some basic knowledge about the php Data types for your reference. PHP Data type PHP supports eight primitive types ). four scalar types: 1 .... the data types in php are not as detailed as in java. below I will introduce some basic knowledge about the php Data types for your reference.

PHP data type

PHP supports eight primitive types ).

Four scalar types:

1. string (string) 2. integer (integer) 3. float (float type, also double) 4. boolean (boolean type)

Two composite types:

1. array (array) 2. object (object)

Two special types:

1. resource (resource) 2. NULL (NULL)

View variable types

The gettype () function allows you to conveniently view the type of a variable:

$ Bool = TRUE; // boolean

$ Str = "foo"; // string

$ Int = 12; // integer

Echo gettype ($ bool); // output boolean

Echo gettype ($ str); // output string

Echo gettype ($ int); // output integer


?>

Judge variable type

To determine the variable type, use the is_type function:

$ Var_int = 12;

// If $ var_int is of the int type, add

If (is_int ($ var_int )){

$ Var_int = $ var_int + 4;

}

Echo $ var_int; // output 16


?>

The above is the basic content of the PHP Data type. for details about the usage of each data type, refer to the PHP Manual.

Data type conversion

There are three conversion methods for PHP Data types:

Target type enclosed in parentheses before the variable to be converted

Use three types of conversion functions: intval (), floatval (), strval ()

Settype (mixed var, string type)

First conversion method: (int) (bool) (float) (string) (array) (object)

$ Num1 = 3.14;

$ Num2 = (int) $ num1;

Var_dump ($ num1); // output float (3.14)

Var_dump ($ num2); // output int (3)


?>

Second conversion method: intval () floatval () strval ()

$ Str = "123.9abc ";

$ Int = intval ($ str); // converted value: 123

$ Float = floatval ($ str); // converted value: 123.9

$ Str = strval ($ float); // converted string: "123.9"


?>

Third conversion method: settype ();

$ Num4 = 12.8;

$ Flg = settype ($ num4, "int ");

Var_dump ($ flg); // output bool (true)

Var_dump ($ num4); // output int (12)


?>

The trap of implicit conversion of PHP Data types. here I am talking about running on php5 +. please move over php4. open the error report first to prevent the error message from being visible.

Error_reporting (E_ALL );

Ini_set ('display _ errors ', true );

?>

According to the http://www.php.net/manual/zh/language.operators.comparison.php in php manual

The description in the "Comparison Operators" chapter shows that when comparing number and string, the string type is first converted to number and then compared.

1. type is automatically converted to array

When we call a non-array variable as an array, the data type is automatically converted to an array during the call.

The instance code is as follows:

$ Str = 'string ';

Var_dump ($ str ['AAA']); // string (1) "s"

Var_dump ($ str); // string (6) "string"

If ($ str ['AAA'] ===$ str [0]) {

Print "= ";

}

?>

The following example shows that the subscript type is automatically converted.

$ Link = 'yulans ';

$ Key = '1-10 ';

Echo "$ link [$ key] \ n"; // same as $ link [1]

Echo "{$ link [$ key]} \ n"; // same as $ link [1]

// Echo "$ link ['$ key'] \ n"; // error

Echo "{$ link ['$ key']} \ n"; // same as $ link [0]


?>

Here, the string in var_dump ($ str ['AAA']) is temporarily converted into an array ('s', 'T', 'R', 'I', 'n ', 'G'), and use the join array method.

$ Str ['AAA'] reads the index array value. The subscript 'AAA' associated with the array will be converted to an integer subscript, therefore, $ str ['AAA'] here is equal to $ str [0]. implicit conversion from other data types to arrays also hides traps. Generally, the undefined index error is not reported.

Example code:

/**

* Implicit conversion of test variables into arrays

*

* @ Param mixed $ param

*/

Function test2Arr ($ param ){

Var_dump ($ param ['ABC']);

}

Test2Arr (false); // NULL

Test 2arr (123); // NULL

Test 2arr (123.456); // NULL

Test2Arr ('string'); // string (1) "s"

Test2Arr (array (

'ABC' => 'text'

); // String (4) text

Test2Arr (new ArrayObject (); // Notice: undefined index: abc


?>

Solution:

When the data type of function parameters is array, it prevents errors caused by user input strings. in the following example, when adding a user, we require the user to enter the user name. no SB passed in the request as an array parameter, but it was quite easy. maybe I worked for more than a decade and then accidentally became the SB, maybe someone wants to bypass the code to perform operations.

/**

* Add a user (incorrect syntax)

*

* @ Param array $ user

*/

Function addUser ($ user ){

If (emptyempty ($ user ['name']) {// an error occurs when the input type is a string that is not null,

Echo "username required \ n ";

Return false;

}

// Do something.

Echo "test \ n ";

Return true;

}

/**

* Add a user (correct syntax)

*

* @ Param array $ user

*/

Function addUser2 ($ user ){

If (! Is_array ($ user) | emptyempty ($ user ['name']) {

Echo "username required \ n ";

Return false;

}

// Do something.

Echo "test \ n ";

Return true;

}

$ User = 'xiaoxiao ';

AddUser ($ user );

AddUser2 ($ user );

?>

2. overflow occurs when the value of a pure numeric string is automatically converted to an integer that exceeds the specified range.

$ X1 = '000000 ';

$ X2 = '000000 ';

Echo ($ x1 ===$ x2 )? "True": "false"; // false as we wish, the two strings are indeed different.

Echo ($ x1 = $ x2 )? "True": "false"; // true indicates that the conversion type is stolen,

// Echo (intval ($ x1) = intval ($ x2 ))? "True": "false"; integer overflow


?>

3. implicit data type conversion may occur during integer and string comparison.

$ Number = 0;

$ String = 'text ';

If ($ number = $ string ){

Print "true ";

} Else {

Print "false ";

}

?>

Unfortunately, the output here is true. we know that $ number = $ string must be false. in the manual, = is a comparison value & data type, use = to compare values. $ number = $ string. isn't it a comparison value? '0' and 'text' are obviously different. be careful. here, $ string is converted to the same integer as $ number and then compared. $ number = (int) $ string is true.

4. in_array small trap

Because in_array will compare 0 and 'S', 0 is the number type, 'S' is the string type, and the result of converting 's' to number is 0, the result of 0 = 0 is true, so the result of in_array (0, array ('s', 'SS') is true. if the third in_array parameter strict is set to true, the system checks whether the value and type are equal. true is returned only when the values are equal. otherwise, false is returned.

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.