首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >正确使用equals语法

正确使用equals语法
EN

Stack Overflow用户
提问于 2014-10-07 15:51:27
回答 2查看 23关注 0票数 0

有没有人能帮我写一下下面的代码

代码语言:javascript
运行
复制
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"];

我不确定我应该使用=还是==

我也不确定我应该用‘’还是“”

谁能让我知道,并提供一个简短的解释,以便我知道下一步,谢谢。

EN

回答 2

Stack Overflow用户

发布于 2014-10-07 16:01:28

相比之下,单个=意味着您正在为变量赋值。例如,$scope = '14001'会将14001赋值给$scope。要比较某些内容,请使用== (如果值相同)或=== (如果值和类型匹配)。

"相比,使用'基本上是代码风格问题。但是如果你在字符串中使用一些变量,那么"将解析字符串以检查其中是否有任何变量,而'将忽略字符串中的任何变量。

例如:

代码语言:javascript
运行
复制
$scope = '123';

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

您还可以在"包装字符串中使用任何以$开头的变量:

代码语言:javascript
运行
复制
echo "Variable {$variable}";
echo "String {$row['someKey']}";
echo "Object {$this->variable}";
echo "Object method that returns value {$this->getValue()}";
票数 2
EN

Stack Overflow用户

发布于 2014-10-07 15:54:25

在监狱里一定要有==

代码语言:javascript
运行
复制
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"];

对于这种情况,更好的解决方案是使用switch而不是if-elseif条件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26231103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档