常见值的TRUE/FALSE判断

<?php
if (-1) {
    echo '-1 is TRUE'; // -1 is TRUE
} else {
    echo '-1 is FALSE';
}

if (0) {
    echo '0 is TRUE';
} else {
    echo '0 is FALSE'; // 0 is FALSE
}

if (1) {
    echo '1 is TRUE'; // 1 is TRUE
} else {
    echo '1 is FALSE';
}

if ('') {
    echo "'' is TRUE";
} else {
    echo "'' is FALSE"; // '' is FALSE
}

if (' ') { // 半角空格
    echo "' ' is TRUE"; // ' ' is TRUE
} else {
    echo "' ' is FALSE";
}

if ('-1') {
    echo "'-1' is TRUE"; // '-1' is TRUE
} else {
    echo "'-1' is FALSE";
}

if ('0') {
    echo "'0' is TRUE";
} else {
    echo "'0' is FALSE"; // '0' is FALSE
}

if ('1') {
    echo "'1' is TRUE"; // '1' is TRUE
} else {
    echo "'1' is FALSE";
}

if (NULL) {
    echo 'NULL is TRUE';
} else {
    echo 'NULL is FALSE'; // NULL is FALSE
}

if (array()) {
    echo 'array() is TRUE';
} else {
    echo 'array() is FALSE'; // array() is FALSE
}

if ('0' == 0) {
    echo "'0' == 0 is TRUE"; // '0' == 0 is TRUE
} else {
    echo "'0' == 0 is FALSE";
}

if ('ABC' == 0) { // 注:字符串和数字比较大小会先将字符串转成数字
    echo "'ABC' == 0 is TRUE"; // 'ABC' == 0 is TRUE
} else {
    echo "'ABC' == 0 is FALSE";
}

if ('123' == '0123') { // 注:如果左右两边均为数字字符串会以数字值进行比较
    echo "'123' == '0123' is TRUE"; // '123' == '0123' is TRUE
} else {
    echo "'123' == '0123' is FALSE";
}

Copyright © 2024 码农人生. All Rights Reserved