Case analysis of type judgment in PHP weakly typed language

Source: Internet
Author: User
This article mainly introduces the type judgment operation in the weak type language of PHP, and analyzes the conversion, judgment and function use skills of PHP for data type in combination with the example, and the need of friends can refer to

In this paper, we describe the operation of type judgment in PHP weakly typed language. Share to everyone for your reference, as follows:

1, php a number and a string for the comparison or operation, PHP will convert the string into a number and then compare. The rules for PHP conversion are: If the string starts with a number, the first number is taken as the result of the conversion, and if none is output 0.

For example: 123ABC after the conversion should be 123, and ABC is 0,0==0 This is of course established. Refer to the Official manual: If you compare an integer and a string, the string is converted to an integer


$a = ($b =4) +5;echo $a;  9echo ' <p> '; Var_dump (1== ' 1a ');  Trueecho ' <p> '; Var_dump (1== ' 2a ');  Falseecho ' <p> '; Var_dump (1== ' A1 ');  Falseecho ' <p> '; Var_dump ("1" = = ' 1a ');  Falseecho ' <p> '; Var_dump (' 51a ' +6);  57echo ' <p> '; Var_dump (' A51 ' +6);  6

2, in php, = = will be the type conversion first, and then compare, and = = = will compare the type, if the type is different directly return inequality, refer to the following example


$a = null; $b = "; $c = 0;echo ($a = = $b)? 1:0;  Output 1echo ($a = = = $b)? 1:0;  Output 0echo ($a = = $c)? 1:0;  Output 1echo ($a = = = $c)? 1:0;  Output 0echo ($b = = $c)? 1:0;  Output 1echo ($b = = = $c)? 1:0;  Output 0

3. Empty and Isset functions in PHP

1) The variable is: 0, "0", NULL, "', False,array (), use the empty function, return True

2) When a variable is undefined or null, the Isset function returns False, and none of the others is true


$a = null; $b = "; $c = 0; $d =" 0 "; $e = false; $f = Array (); Var_dump (empty ($a));  Trueecho ' <p> '; Var_dump (Empty ($b));  Trueecho ' <p> '; Var_dump (Empty ($c));  Trueecho ' <p> '; Var_dump (Empty ($d));  Trueecho ' <p> '; Var_dump (Empty ($e));  Trueecho ' <p> '; Var_dump (Empty ($f));  Trueecho ' <p> '; Var_dump (Isset ($a));  Falseecho ' <p> '; Var_dump (Isset ($b));  Trueecho ' <p> '; Var_dump (Isset ($c));  Trueecho ' <p> '; Var_dump (Isset ($d));  Trueecho ' <p> '; Var_dump (Isset ($e));  Trueecho ' <p> '; Var_dump (Isset ($f));  Trueecho ' <p> '; Var_dump (Isset ($g));  False

4, use Strpos and the like function to use constant equals to judge


$sms = "abc"; if (Strpos ($sms, ' a ')! = False) {  echo 1;} Else{  echo 2;}

In this case, it is clear that the ABC string, whether it contains a, the expected result should be to output 1, the actual result output is 2. This is because the Strpos function does not match the target string, returns False when it matches the target string, returns the target string, the index position in the search string, and returns 0 here.


$sms = "abc"; if (Strpos ($sms, ' a ')!== false) {  echo 1;} Else{  echo 2;}

Change to constant equals, is correct.

5, newline needs double quotation marks


$time = Date (' y-m-d h:i:s '). ' \ r \ n '; file_put_contents (' Filename.txt ', $time, file_append);

If this is the case, open the file is like this 2016-09-02 08:04:04\r\n2016-09-02 08:04:05\r\n2016-09-02 08:04:05\r\n2016-09-02 08:04:05\r\ N2016-09-02 08:04:22

The correct wording is \ r \ n, wrapped with double quotation marks.

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.