Php Data type usage

Source: Internet
Author: User
Tags arrays mixed

PHP data type
PHP supports eight primitive types ).

Four scalar types:

1. string (string)
2. integer)
3. float (float, also double)
4. boolean (boolean)
Two composite types:

1. array (array)
2. object)
Two special types:

1. resource)
2. NULL (NULL)
View variable types
The gettype () function allows you to conveniently view the type of a variable:

<? Php
$ 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:

<? Php
$ 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:

• The Target type enclosed in parentheses before the variable to be converted
• Three types of conversion functions are used: intval (), floatval (), and strval ()
• Use the common type conversion function settype (mixed var, string type)
First conversion method: (int) (bool) (float) (string) (array) (object)
 
• <? Php
• $ 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 ()

 
• <? Php
• $ 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 ();

 
• <? Php
• $ Num4 = 12.8;
• $ Flg = settype ($ num4, "int ");
• Var_dump ($ flg); // output bool (true)
• Var_dump ($ num4); // output int (12)
•?>

Hidden conversion of PHP Data types

I am talking about running on php5 +. Please see php4.

Open the error report first to prevent the error message from being visible.
<? Php
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 the number and 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.
For example, the following code:
<? Php
$ 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.
<? Php
$ Link = '<a href = "http://yulans.cn"> yulans </a> ';
$ 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:
<? Php
/**
* 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 an array, it prevents errors caused by user input strings.
In the following example, when adding a user, you must enter the user name. No SB passed in the array parameter.
Character string, but the defensive heart is inevitable. Maybe I have worked for more than 10 hours in a row and then become the SB accidentally. Maybe
Someone wants to bypass the code to perform operations.
<? Php
/**
* Add a user (incorrect syntax)
 *
* @ Param array $ user
*/
Function addUser ($ user ){
If (empty ($ user ['name']) {// here, an error occurs when the input type is not null,
Echo "user name 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) | empty ($ user ['name']) {
Echo "user name 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.
<? Php
$ 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.
<? Php
$ 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, and = is a comparison value,
$ Number = $ string isn't the comparison value here? '0' and 'text' are obviously different. Be careful. $ string here is
It is converted to an integer that is the same as $ number and then compared. $ number = (int) $ string is true.


4. in_array small trap
<? Php

Var_dump (in_array (0, array ('s '); // true

?>
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 parameter strict of in_array is set to true, the system checks whether the value and type are equal during comparison.
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.