为什么 PHP if 语句在完全拼写时起作用,而当部分拼写是变量时则不行?[复制]

why does php if statement work when it's completely spelled out, but not when part of the spelling is a variable? [duplicate]

提问人:leoarce 提问时间:3/24/2023 最后编辑:leoarce 更新时间:3/24/2023 访问量:44

问:

这个问题在这里已经有答案了:
8个月前关闭。

社群在 8 个月前审查了是否重新打开这个问题,并关闭了这个问题:

原始关闭原因未解决

这工作原理:

if ( strtolower($make)=="toyota" && ( strpos(strtolower($model), "scion") !== false || strpos(strtolower($model), "scoin") !== false || strtolower($model) == "xa" || strtolower($model) == "xb" || strtolower($model) == "tc" || strtolower($model) == "xd" || strtolower($model) == "iq" || strtolower($model) == "fr-s" || strtolower($model) == "ia" || strtolower($model) == "im" ) ){
    //turn make into scion
    $make = "scion";
}

这是行不通的

$db_scion_models = "";
$scion_models_result = mysqli_query($conn,"SELECT DISTINCT vehicle_model_name FROM mod_vehicle_models WHERE mod_vehicle_make_id = '41' "); //41=scion
while ($scion_model_row = mysqli_fetch_assoc($scion_models_result)) {
    $db_scion_models .= '|| strtolower($model) == "'.$scion_model_row['vehicle_model_name'].'" ';
}
if (!empty($db_scion_models)) {
    $db_scion_models = 'strpos(strtolower($model), "scion") !== false || strpos(strtolower($model), "scoin") !== false '.$db_scion_models;
} else {
    $db_scion_models = 'strpos(strtolower($model), "scion") !== false || strpos(strtolower($model), "scoin") !== false';
}
if ( strtolower($make)=="toyota" && ( $db_scion_models ) ){
    //turn make into scion
    $make = "scion";
}

if 语句具有变量来尝试使其动态化。将发生以下情况:

these convert to scion properly:
toyota FR-S
toyota iA
toyota iM
toyota tC

these convert to scion but shouldn't:
toyota 4runner
toyota avalon
toyota camry
toyota corolla
toyota highlander
toyota prius

if 语句以静态方式拼写。将发生以下情况:

these convert to scion properly:
toyota FR-S
toyota iA
toyota iM
toyota tC

these don't convert to scion:
toyota 4runner
toyota avalon
toyota camry
toyota corolla
toyota highlander
toyota prius

我希望动态和静态的工作方式相同。我做错了什么?

编辑。。。

所以这样做吗?

$db_scion_models_array = array();
$scion_models_result = mysqli_query($conn,"SELECT DISTINCT vehicle_model_name FROM mod_vehicle_models WHERE mod_vehicle_make_id = '41' "); //41=scion
while ($scion_model_row = mysqli_fetch_assoc($scion_models_result)) {
    $db_scion_models_array[]=$scion_model_row['vehicle_model_name'];
}
if ( strtolower($make)=="toyota" && in_array(strtolower($model), $db_scion_models_array, true) ) {
    //turn make into scion
    $make = "scion";
}
php mysql 字符串 if-statement 变量

评论

3赞 ADyson 3/24/2023
因为字符串不是代码
0赞 user3783243 3/24/2023
...执行字符串将是一种非常危险的做法。考虑一下,如果用户写入了一个数据库字段,然后你获取了该字段,服务器被删除了(假设服务器在其上设置了较差的权限)exec('rm -rf /')
1赞 ADyson 3/24/2023
如果你有一个要检查的事物的动态列表,那么把它们都放在一个数组中,并使用 in_array() 来搜索匹配项
1赞 user3783243 3/24/2023
@leoarce 该评论已被删除,因为我在您尝试将字符串作为代码执行后才意识到。别这样。有一种方法可以做到这一点,但正如手册所述,这是非常危险的,不应该这样做。 注意:您已经仔细验证了没有其他选择The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct
1赞 leoarce 3/24/2023
所有这些指向已经回答的问题的链接都不是答案,特别是因为每个人都抱怨 eval 方法。我只是以阵列方式做到了。

答:

1赞 Quentin 3/24/2023 #1

字符串中的PHP源代码与评估PHP源代码的结果不同。

比较代码的简化版本:

$a = "a";
$b = "b";

$result = '$a === "a"';
$result = $result . '($b === "b")';
echo $result;

这给出了 1.

...使用PHP作为PHP而不是PHP字符串的方法:

$a = "a";
$b = "b";

$result = $a === "a";
$result = $result && ($b === "b");
echo $result;

给出$a === “a”($b === “b”)