嗨,我想知道我可能做错了什么。我有两个返回true或false.For的条件,这与它不工作的原因相同。如果有人指出我可能做错了什么,我将不胜感激。
<?php
$pagetype = strpos($currentPage, 'retail');
if(isset($quoterequest) && $pagetype === True && $currentPage !== 'index.php'){
echo "this is retail" ;
}elseif(isset($quoterequest) && $pagetype === False && $currentPage !== 'index.php'){
echo "this is restaurant";
}
?>编辑-对不起,我编辑了它,但由于某些原因没有出现。基本上脚本在urls中查找术语“零售”,如果它找到了,它应该返回“这是零售”,如果不是“这是餐厅”
quoterequest只是一个变量,如下所示
$quoterequest = "washington"当前页面是从
<?php $currentPage = basename($_SERVER['SCRIPT_NAME']);只是想说清楚。Url的结构如下
Www.example.com/retail store.php www.example.com/store.php
发布于 2010-08-06 04:06:28
$pageType从来都不是真的。如果包含该字符串,则strpos返回一个整数。所以测试一下!== false吧。
<?php
$pagetype = strpos($currentPage, 'retail');
if (isset($quoterequest) && $currentPage !== 'index.php') {
if ($pagetype !== false) {
echo "this is retail";
}
else {
echo "this is restaurant";
}
}
?>发布于 2010-08-06 04:15:46
每次重复这些条件也没有意义。下面的代码应该可以执行您想要的操作。(考虑到前面提到的strpos评论)。
$pagetype = strpos($currentPage, 'retail');
if($currentPage !== 'index.php' && isset($quoterequest)){
if ($pagetype === false){
echo "restaurant";
}else{
echo "retail";
}
}发布于 2010-08-06 04:20:36
PHP有懒惰的评估。如果if语句中的第一项计算结果为false,它将停止计算其余代码。如果isset($quoterequest)的计算结果为false,则不会检查这两个语句中的任何其他内容。
$>
<?php
function untrue() {
return FALSE;
}
function truth() {
echo "called " . __FUNCTION__ . "\n!";
return TRUE;
}
if ( untrue() && truth() ){
echo "Strict.\n";
} else {
echo "Lazy!\n";
}
$> php test.php
Lazy!https://stackoverflow.com/questions/3418708
复制相似问题