0

Hi i wonder what i might be doing wrong. I have two conditions that returns true or false.For same reason it doesnt work. 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 = "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

4
  • 1
    What's happening and what do you want to have happen here? Is it not going into the clause you expect or is it going into neither one? Commented Aug 5, 2010 at 20:05
  • Could we get some samples of the values of $currentPage and $quoterequest Commented Aug 5, 2010 at 20:05
  • 1
    And does it say 'this is retail' or 'this is restaurant' - or neither? Commented Aug 5, 2010 at 20:06
  • What does your script print now? or nothing? Commented Aug 5, 2010 at 20:06

4 Answers 4

6

$pageType is never true. If the string is contained, strpos returns an integer. So test for !== false.

<?php  
    $pagetype = strpos($currentPage, 'retail');
    if (isset($quoterequest) && $currentPage !== 'index.php') {
        if ($pagetype !== false) {
            echo "this is retail";
        }
        else {
            echo "this is restaurant";
        }
    }                     
?>
Sign up to request clarification or add additional context in comments.

Comments

1

At first look it should be $pagetype !== false
strpos return false or a numeric value on match

quote from php.net/strpos

Returns the position as an integer. If needle is not found, strpos() will return boolean 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)

Comments

1

no sense in duplicating the conditions each time either. The below code should do what you want.. (taking into account the aforementioned strpos comments).

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

1 Comment

dang I need to type faster... :)
0

PHP has lazy evaluation. If the first term in an if statement evaluates to false, it will stop evaluating the rest of the code. If isset($quoterequest) evaluates to false, nothing else in either of your statements will be examined.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.