Changes in converting other types of PHP to the Bool type-php Tutorial

Source: Internet
Author: User
In PHP, when other types are converted to the Bool type, the following error occurs: if (true = 2) in PHP, Will true or false be returned?

The result is true. in this case, I will test other php Data types.

Conclusion:

When converted to the bool type, the following data types are converted to false:

1. integer 0

2. empty string

3. empty array

4. NULL

Please add...

Test code:

 Function p ($ title, $ mybool ){
Echo"
".$title;
echo var_dump($mybool)."
";
}
Class foo {
Function do_foo (){
Echo "Hello! ";
}
}
Echo"
Convert other types in PHP to Bool type
";
// Zero
$ N0 = boolval (0 );
P ("zero:", $ n0 );

// Positive integer
$ N = boolval (2 );
P ("positive integer:", $ n );

// Negative integer
$ Nx = boolval (-2 );
P ("negative integer:", $ nx );

// Character space
$ Ss = boolval ("");
P ("Space:", $ ss );

// Empty string
$ Sn = boolval ("");
P ("null string:", $ sn );

// String
$ S = boolval ("chinacion ");
P ("string:", $ s );

// Empty array
$ An = boolval (array ());
P ("empty array:", $ );

// Array
$ A = boolval (array (0 => 1 ));
P ("array:", $ );

// Null type
$ Nu = boolval (NULL );
P ("NULL:", $ nu );

// Object
$ Bar = new foo;
$ Bar;
$ Obj = boolval ($ bar );
P ("Object:", $ obj );

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.