简体   繁体   中英

Correct use of equals syntax

could someone please help me with the following code

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"];

I am unsure if I should be using = or ==

and also unsure if I should be using ' ' or " "

Could someone please let me know and provide a brief explanation so I know going forward, thank you.

In comparison single = means you are assigning some value to variable. Eg $scope = '14001' will assign 14001 to $scope . To compare something, use == (just if values are same) or === (if values and types match).
Using ' versus " is basically code style matter. But if you are using some variables in string, than " will parse string to check if there is any variable inside, while ' will ignore any variable inside string.
Eg:

$scope = '123';

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

Also you can use any variable that starts with $ in " wrapped string:

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

In comaprison there has to be ==

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"];

For this case better solution is to use switch instead of if-elseif conditions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM