如果$ variable = 0不起作用

Gia*_*uck 5 php if-statement

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'];
Run Code Online (Sandbox Code Playgroud)

无论$_POST['user_admin']是0还是1,$acct_type仍然会返回"管理员"为什么?

Tim*_*orn 10

比较变量时需要使用"==".

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'];
Run Code Online (Sandbox Code Playgroud)


小智 6

它应该是

if $variable == 0
Run Code Online (Sandbox Code Playgroud)