如果声明评估错误条件PhP

时间:2015-03-13 03:16:51

标签: php

出于某种原因,我的条件语句在不是

时评估为false
$file_check=strpos(file_get_contents("database/file_register.txt"),basename($_FILES["fileToUpload"]["name"]));

echo "file=$file_check"; \\ equals 0 -> which is not False.

if(isset($_POST["submit"]) && ($file_check == False)) {$file_check='ok';}

else{echo "The document is already in use.";}

echo "file=$file_check"; \\ now it equals 'ok'

2 个答案:

答案 0 :(得分:4)

您的条件是检查0 == False是否为真

确保设置submit

PHP类型比较表

$ x与PHP函数的比较

+------------------+------------+----------+------------+----------+------------------+
|   Expression     | gettype()  | empty()  | is_null()  | isset()  | boolean : if($x) |
+------------------+------------+----------+------------+----------+------------------+
| $x = "";         | string     | TRUE     | FALSE      | TRUE     | FALSE            |
| $x = null;       | NULL       | TRUE     | TRUE       | FALSE    | FALSE            |
| var $x;          | NULL       | TRUE     | TRUE       | FALSE    | FALSE            |
| $x is undefined  | NULL       | TRUE     | TRUE       | FALSE    | FALSE            |
| $x = array();    | array      | TRUE     | FALSE      | TRUE     | FALSE            |
| $x = false;      | boolean    | TRUE     | FALSE      | TRUE     | FALSE            |
| $x = true;       | boolean    | FALSE    | FALSE      | TRUE     | TRUE             |
| $x = 1;          | integer    | FALSE    | FALSE      | TRUE     | TRUE             |
| $x = 42;         | integer    | FALSE    | FALSE      | TRUE     | TRUE             |
| $x = 0;          | integer    | TRUE     | FALSE      | TRUE     | FALSE            |
| $x = -1;         | integer    | FALSE    | FALSE      | TRUE     | TRUE             |
| $x = "1";        | string     | FALSE    | FALSE      | TRUE     | TRUE             |
| $x = "0";        | string     | TRUE     | FALSE      | TRUE     | FALSE            |
| $x = "-1";       | string     | FALSE    | FALSE      | TRUE     | TRUE             |
| $x = "php";      | string     | FALSE    | FALSE      | TRUE     | TRUE             |
| $x = "true";     | string     | FALSE    | FALSE      | TRUE     | TRUE             |
| $x = "false";    | string     | FALSE    | FALSE      | TRUE     | TRUE             |
+------------------+------------+----------+------------+----------+------------------+

== VS ===

  • 宽松比较仅考虑值。
  • 严格比较会考虑值和类型

==

的宽松比较
    +----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+----------+--------+-------+
    |          | TRUE   | FALSE  |   1    |   0    |  -1    |  "1"   |  "0"   | "-1"   | NULL   | array()  | "php"  |  ""   |
    +----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+----------+--------+-------+
    | TRUE     | TRUE   | FALSE  | TRUE   | FALSE  | TRUE   | TRUE   | FALSE  | TRUE   | FALSE  | FALSE    | TRUE   | FALSE |
    | FALSE    | FALSE  | TRUE   | FALSE  | TRUE   | FALSE  | FALSE  | TRUE   | FALSE  | TRUE   | TRUE     | FALSE  | TRUE  |
    | 1        | TRUE   | FALSE  | TRUE   | FALSE  | FALSE  | TRUE   | FALSE  | FALSE  | FALSE  | FALSE    | FALSE  | FALSE |
    | 0        | FALSE  | TRUE   | FALSE  | TRUE   | FALSE  | FALSE  | TRUE   | FALSE  | TRUE   | FALSE    | TRUE   | TRUE  |
    | -1       | TRUE   | FALSE  | FALSE  | FALSE  | TRUE   | FALSE  | FALSE  | TRUE   | FALSE  | FALSE    | FALSE  | FALSE |
    | "1"      | TRUE   | FALSE  | TRUE   | FALSE  | FALSE  | TRUE   | FALSE  | FALSE  | FALSE  | FALSE    | FALSE  | FALSE |
    | "0"      | FALSE  | TRUE   | FALSE  | TRUE   | FALSE  | FALSE  | TRUE   | FALSE  | FALSE  | FALSE    | FALSE  | FALSE |
    | "-1"     | TRUE   | FALSE  | FALSE  | FALSE  | TRUE   | FALSE  | FALSE  | TRUE   | FALSE  | FALSE    | FALSE  | FALSE |
    | NULL     | FALSE  | TRUE   | FALSE  | TRUE   | FALSE  | FALSE  | FALSE  | FALSE  | TRUE   | TRUE     | FALSE  | TRUE  |
    | array()  | FALSE  | TRUE   | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | TRUE   | TRUE     | FALSE  | FALSE |
    | "php"    | TRUE   | FALSE  | FALSE  | TRUE   | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE    | TRUE   | FALSE |
    | ""       | FALSE  | TRUE   | FALSE  | TRUE   | FALSE  | FALSE  | FALSE  | FALSE  | TRUE   | FALSE    | FALSE  | TRUE  |
    +----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+----------+--------+-------+

===

进行严格比较
+----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+----------+--------+-------+
|          | TRUE   | FALSE  |   1    |   0    |  -1    |  "1"   |  "0"   | "-1"   | NULL   | array()  | "php"  |  ""   |
+----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+----------+--------+-------+
| TRUE     | TRUE   | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE    | FALSE  | FALSE |
| FALSE    | FALSE  | TRUE   | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE    | FALSE  | FALSE |
| 1        | FALSE  | FALSE  | TRUE   | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE    | FALSE  | FALSE |
| 0        | FALSE  | FALSE  | FALSE  | TRUE   | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE    | FALSE  | FALSE |
| -1       | FALSE  | FALSE  | FALSE  | FALSE  | TRUE   | FALSE  | FALSE  | FALSE  | FALSE  | FALSE    | FALSE  | FALSE |
| "1"      | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | TRUE   | FALSE  | FALSE  | FALSE  | FALSE    | FALSE  | FALSE |
| "0"      | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | TRUE   | FALSE  | FALSE  | FALSE    | FALSE  | FALSE |
| "-1"     | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | TRUE   | FALSE  | FALSE    | FALSE  | FALSE |
| NULL     | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | TRUE   | FALSE    | FALSE  | FALSE |
| array()  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | TRUE     | FALSE  | FALSE |
| "php"    | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE    | TRUE   | FALSE |
| ""       | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE  | FALSE    | FALSE  | TRUE  |
+----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+----------+--------+-------+

参考http://php.net/manual/en/types.comparisons.php

答案 1 :(得分:0)

if(isset($ _ POST [" submit"])&&($ file_check === False)){     $ file_check =' OK&#39 ;; }

使用' ==='在strpos()

中检查TRUE或FALSE