PHP 中空字符串介绍0、null、empty和false之间的关系
0是数字,是empty,是false,不是null,值相当于空字符串,但类型不是字符串,去空格或强制转换为字符串型时不等于空字符串
""的值相当于0,是empty,是空字符串,是false,不是null,不是数字字符串
null的值相当于空字符串,值相当于0,是empty,是false,不是字符串,不是数字0
"=="只要值相等就满足条件; "==="需要两个变量的值和类型都相等;
strval();将变量转换为字符串类型;
intval();将变量转换为整型;
is_numeric();检测变量是否为数字或数字字符串,返回true或false;
1 // 判断 0 与 ''、null、empty、false 之间的关系 2 $a = 0; 3 echo "0 与 ''、 empty、null、false 之间的关系:"; 4 5 if($a == '') 6 { 7 echo "0 == '';"; 8 } 9 else 10 { 11 echo "0 != '';"; 12 } 13 14 if(trim($a) == '') 15 { 16 echo "trim(0) == '';"; 17 } 18 else 19 { 20 echo "trim(0) != '';"; 21 } 22 23 if(strval($a) == '') 24 { 25 echo "strval(0) == '';"; 26 } 27 else 28 { 29 echo "strval(0) != '';"; 30 } 31 //0=='',trim(0)!='',strval(0)!='' 不是空字符串 32 33 if($a === '') 34 { 35 echo "0 === '';"; 36 } 37 else 38 { 39 echo "0 !=== '';"; 40 } 41 //0!==='' 42 43 if(empty($a)) 44 { 45 echo "0 is empty;"; 46 } 47 else 48 { 49 echo "0 is not empty;"; 50 } 51 //0 is empty 52 53 if(is_null($a)) 54 { 55 echo "0 is null;"; 56 } 57 else 58 { 59 echo "0 is not null;"; 60 } 61 //0 is not null 62 63 if(is_numeric($a)) 64 { 65 echo "0 is numeric;"; 66 } 67 else 68 { 69 echo "0 is not numeric;"; 70 } 71 //0 is not numeric 72 73 if(is_string($a)) 74 { 75 echo "0 is string;"; 76 } 77 else 78 { 79 echo "0 is not string;"; 80 } 81 //0 is not string 82 83 if(!$a) 84 { 85 echo "0 is false;"; 86 } 87 else 88 { 89 echo "0 is not false;"; 90 } 91 //0 is false 92 93 // 判断 '' 和 0、null、empty、false 之间的关系 94 $a = ''; 95 echo "'' 和 0、empty、null、false 之间的关系:"; 96 if($a == 0) 97 { 98 echo "'' == 0;"; 99 } 100 else 101 { 102 echo "'' != 0;"; 103 } 104 105 if(intval($a) == 0) 106 { 107 echo "intval('') == 0;"; 108 } 109 else 110 { 111 echo "intval('') != 0;"; 112 } 113 114 if(empty($a)) 115 { 116 echo "'' is empty;"; 117 } 118 else 119 { 120 echo "'' is not empty;"; 121 } 122 123 if(is_null($a)) 124 { 125 echo "'' is null;"; 126 } 127 else 128 { 129 echo "'' is not null;"; 130 } 131 132 if(is_numeric($a)) 133 { 134 echo "'' is numeric;"; 135 } 136 else 137 { 138 echo "'' is not numeric;"; 139 } 140 141 if(is_string($a)) 142 { 143 echo "'' is string;"; 144 } 145 else 146 { 147 echo "'' is not string;"; 148 } 149 150 if(!$a) 151 { 152 echo "'' is false;"; 153 } 154 else 155 { 156 echo "'' is not false;"; 157 } 158 159 // 判断 null 和 ''、0、empty、false 之间的关系 160 $a = null; 161 echo "null 和 ''、0、empty、false 之间的关系:"; 162 if($a == '') 163 { 164 echo "null == '';"; 165 } 166 else 167 { 168 echo "null != '';"; 169 } 170 171 if($a == 0) 172 { 173 echo "null == 0;"; 174 } 175 else 176 { 177 echo "null != 0;"; 178 } 179 180 if($a === '') 181 { 182 echo "null === '';"; 183 } 184 else 185 { 186 echo "null !=== '';"; 187 } 188 189 if($a === 0) 190 { 191 echo "null === 0;"; 192 } 193 else 194 { 195 echo "null !=== 0;"; 196 } 197 198 if(strval($a) == '') 199 { 200 echo "strval(null) == '';"; 201 } 202 else 203 { 204 echo "strval(null) != '';"; 205 } 206 207 if(intval($a) == 0) 208 { 209 echo "intval(null) == 0;"; 210 } 211 else 212 { 213 echo "intval(null) != 0;"; 214 } 215 216 if(empty($a)) 217 { 218 echo "null is empty;"; 219 } 220 else 221 { 222 echo "null is not empty;"; 223 } 224 225 if(is_numeric($a)) 226 { 227 echo "null is numeric;"; 228 } 229 else 230 { 231 echo "null is not numeric;"; 232 } 233 234 if(is_string($a)) 235 { 236 echo "null is string;"; 237 } 238 else 239 { 240 echo "null is not string;"; 241 } 242 243 if(!$a) 244 { 245 echo "null is false;"; 246 } 247 else 248 { 249 echo "null is not false;"; 250 }
posted on 2018-12-06 19:28 zhuyongzhe 阅读(426) 评论(0) 编辑 收藏 举报