简体   繁体   English

PHP的if语句?

[英]PHP if statement?

Hi i wonder what i might be doing wrong. 嗨,我想知道我可能做错了什么。 I have two conditions that returns true or false.For same reason it doesnt work. 我有两个条件返回true或false。出于相同的原因,它不起作用。 I would appreciate if someone point out what i might be doing wrong. 如果有人指出我可能做错了,我将不胜感激。

<?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";
  }                               
?>

EDIT- Sorry i edit it but didnt show up for some reason. 编辑-对不起,我编辑了它,但是由于某种原因没有出现。 Basically script looks into urls for term "retail" if it finds it, it should return "this is retail" if not "this is restaurant" 基本上,脚本会查找“零售”一词的网址,如果找不到,则应返回“这是零售”,而不是“这是餐馆”

quoterequest is a just a variable like so quoterequest是一个像这样的变量

$quoterequest = "washington"

and the currentpage is driven from 并且当前页面是从

<?php $currentPage = basename($_SERVER['SCRIPT_NAME']);

Just to be clear. 只是要清楚。 Url is structure like so 网址是这样的结构

www.example.com/retail-store.php www.example.com/store.php www.example.com/retail-store.php www.example.com/store.php

$pageType is never true. $pageType永远不会为真。 If the string is contained, strpos returns an integer. 如果包含字符串,则strpos返回一个整数。 So test for !== false . 因此,测试!== false

<?php  
    $pagetype = strpos($currentPage, 'retail');
    if (isset($quoterequest) && $currentPage !== 'index.php') {
        if ($pagetype !== false) {
            echo "this is retail";
        }
        else {
            echo "this is restaurant";
        }
    }                     
?>

At first look it should be $pagetype !== false 乍一看应该是$ pagetype!== false
strpos return false or a numeric value on match strpos返回false或匹配时的数值

quote from php.net/strpos 引用来自php.net/strpos

Returns the position as an integer. 以整数形式返回位置。 If needle is not found, strpos() will return boolean FALSE. 如果未找到needle,strpos()将返回布尔值FALSE。

so, to check if the value is not found you should use if(strpos(...)===false) and to check if it's found you should use if(strpos(...)!==false) 因此,要检查是否未找到该值,应使用if(strpos(...)=== false),并应检查是否应找到if(strpos(...)!== false)

no sense in duplicating the conditions each time either. 每次都复制条件也没有意义。 The below code should do what you want.. (taking into account the aforementioned strpos comments). 下面的代码应该做您想要的..(考虑到上述strpos注释)。

$pagetype = strpos($currentPage, 'retail');
if($currentPage !== 'index.php' && isset($quoterequest)){
   if ($pagetype === false){
     echo "restaurant";
   }else{
     echo "retail"; 
   }
}

PHP has lazy evaluation. PHP的评估很懒。 If the first term in an if statement evaluates to false, it will stop evaluating the rest of the code. 如果if语句中的第一项评估为false,它将停止评估其余代码。 If isset($quoterequest) evaluates to false, nothing else in either of your statements will be examined. 如果isset($quoterequest)计算结果为false,则不会检查任何isset($quoterequest)语句中的其他内容。

$>
<?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!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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