Home  >  Article  >  Backend Development  >  Do you know what automatic conversion and forced conversion between PHP data types are? (Source code attached)

Do you know what automatic conversion and forced conversion between PHP data types are? (Source code attached)

慕斯
慕斯Original
2021-05-21 17:19:333177browse

What is automatic conversion and forced conversion between PHP data types? Are you an expert or a "novice"? Let's discuss the fun of forced conversion and automatic conversion! ! !

Do you know what automatic conversion and forced conversion between PHP data types are? (Source code attached)

Automatic type conversion of Boolean values:

#1: Integer 0 is false, other integer The values ​​are all true

2: Floating point 0.0, the Boolean value is false, as long as there is a non-zero value after the decimal point, it is true;

3: The empty string is false, as long as If there is a space in it, it is considered true;

4: 0 in the string is also considered false, and everything else is true;

For example, the code is as follows:

<?php
$bool = 0.6;
if($bool){
    echo &#39;欢迎来到PHP中文网&#39;;
}else{
    echo&#39;PHP中文网欢迎你的到来&#39;;
}
?>

The running results are as follows:

Do you know what automatic conversion and forced conversion between PHP data types are? (Source code attached)

#1: The integer 0 is false, and the other integer values ​​are all true. The code demonstration is as shown in the figure :

<?php
$bool = 0;
if($bool){
    echo &#39;欢迎来到PHP中文网&#39;;
}else{
    echo&#39;PHP中文网欢迎你的到来&#39;;
}
?>

The running result is as follows:

Do you know what automatic conversion and forced conversion between PHP data types are? (Source code attached)

2: The empty string is false, as long as there is a space in it, it will be counted True; the code demonstration is as shown in the figure:

<?php
$str = &#39;&#39;;
if($str){
    echo &#39;欢迎来到PHP中文网&#39;;
}else{
    echo&#39;PHP中文网欢迎你的到来&#39;;
}
?>

The running result is as follows:

Do you know what automatic conversion and forced conversion between PHP data types are? (Source code attached)

3: 0 of the string, It is also regarded as false, and everything else is true; the code demonstration is as shown in the figure:

<?php
$str = &#39;1&#39;;
if($str){
    echo &#39;欢迎来到PHP中文网&#39;;
}else{
    echo&#39;PHP中文网欢迎你的到来&#39;;
}
?>

The running result is as follows:

Do you know what automatic conversion and forced conversion between PHP data types are? (Source code attached)

Recommended: "PHP Video Tutorial"

The above is the detailed content of Do you know what automatic conversion and forced conversion between PHP data types are? (Source code attached). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn