php 如果 $variable = 0 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13261495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:03:34  来源:igfitidea点击:

If $variable = 0 not working

phpif-statement

提问by GiantDuck

if($_POST['user_admin'] = 0){ $acct_type = "a standard"; }
elseif($_POST['user_admin'] = 1){ $acct_type = "an administrator"; }
echo $acct_type;
echo $_POST['user_admin'];

Whether $_POST['user_admin']is 0 or 1, $acct_typestill returns "an administrator" Why?

不管$_POST['user_admin']是0还是1,$acct_type还是返回“管理员”为什么?

回答by Tim Dearborn

You need to use "==" when comparing variables.

比较变量时需要使用“==”。

if($_POST['user_admin'] == 0){ $acct_type = "a standard"; }
elseif($_POST['user_admin'] == 1){ $acct_type = "an administrator"; }
echo $acct_type;
echo $_POST['user_admin'];

回答by Ian

It should be

它应该是

if $variable == 0

回答by admoghal

you are assigning value with = you should use $variable == 0 to compare the value

你用 = 赋值你应该使用 $variable == 0 来比较值

回答by janenz00

You are on the first of 10 common PHP mistakes to avoid:-)

您是要避免10 个常见 PHP 错误中的第一个:-)

   $_POST['user_admin'] = 0 
   $_POST['user_admin'] = 1

are both assignments. PHP evaluates whether the final assigned expression is true or false after assigning the value to $_POST['user_admin'] . So, the first one will evaluate to false since the assigned value is 0, and the second one will evaluate to true since the assigned value is 1.

都是任务。PHP 在将值分配给 $_POST['user_admin'] 后评估最终分配的表达式是真还是假。因此,第一个将评估为 false,因为分配的值为 0,第二个将评估为 true,因为分配的值为 1。

As everyone pointed out, you have to use "==" instead of "=" for conditional statements.

正如每个人所指出的,对于条件语句,您必须使用“==”而不是“=”。