2014-10-07 50 views
0

可能有人请帮我用下面的代码正确使用语法平等的

if ($scope = '9001') $docref = $rs["9001ref"]; 
elseif ($scope = '14001') $docref = $rs["14001ref"]; 
elseif ($scope = '18001') $docref = $rs["18001ref"]; 
elseif ($scope = '9001,14001') $docref = $rs["914001ref"]; 
elseif ($scope = '9001,18001') $docref = $rs["918001ref"]; 
elseif ($scope = '14001,18001') $docref = $rs["1418001ref"]; 
elseif ($scope = '9001,14001,18001') $docref = $rs["91418001ref"]; 

我不确定我是否应该使用=或==

,也不能确定我是否应该使用“”或“”

有人请让我知道,并提供一个简短的解释,所以我知道前进,谢谢。

+0

http://php.net/manual/en/language.operators.comparison。 php – 2014-10-07 07:52:13

+0

这里没有很多mysql在这里 – Strawberry 2014-10-07 07:53:08

+0

RTFM:'='是赋值运算符,'=='用于宽松比较,'==='是类型和值检查 – 2014-10-07 07:59:49

回答

0

在comaprison必须有==

if ($scope = '9001') $docref = $rs["9001ref"]; 
elseif ($scope == '14001') $docref = $rs["14001ref"]; 
elseif ($scope == '18001') $docref = $rs["18001ref"]; 
elseif ($scope == '9001,14001') $docref = $rs["914001ref"]; 
elseif ($scope == '9001,18001') $docref = $rs["918001ref"]; 
elseif ($scope == '14001,18001') $docref = $rs["1418001ref"]; 
elseif ($scope == '9001,14001,18001') $docref = $rs["91418001ref"]; 

对于这种情况更好的解决方案是使用的switchif-elseif代替条件。

2

比较单个=意味着你正在给变量赋值。例如。 $scope = '14001'将分配14001$scope。要比较一下,请使用==(只要值相同)或===(如果值和类型匹配)。
使用'"基本上是代码风格的问题。但是如果你在字符串中使用了一些变量,比"将解析字符串来检查里面是否有变量,而'会忽略字符串中的任何变量。
如:

$scope = '123'; 

echo "My scope is {$scope}"; // will echo "My scope is 123"; 
echo 'My scope is {$scope}'; // will echo "My scope is {$scope}"; 

你也可以使用与$"包裹字符串开头的任何变量:

echo "Variable {$variable}"; 
echo "String {$row['someKey']}"; 
echo "Object {$this->variable}"; 
echo "Object method that returns value {$this->getValue()}";