提问人:leoarce 提问时间:3/24/2023 最后编辑:leoarce 更新时间:3/24/2023 访问量:44
为什么 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]
问:
这个问题在这里已经有答案了:
如何在字符串变量中执行PHP代码 (3 个答案)
如何执行字符串php代码运行 (1 个答案)
如何执行以字符串形式存储的PHP代码? (2 个答案)
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";
}
答:
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”)
评论
exec('rm -rf /')
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