tags:

views:

108

answers:

7

I may be doing it wrong but take a peek. If I hardcode the logic, it works but not if I try to use it as a variable.

if($range <= 50) {
    $operator = "<=";
} else {
    $operator = ">=";
}

foreach($cursor as $s) {
    $data = round($this->distance($zip_lat, $zip_lon, $s["lat"],$s["lon"]), 2);

    if ($data .$operator. $range) {
        $zipcodes[] = "$s[zipcode]";   
    }
}               

I mean, I could add the if/else inside the foreach but wasn't sure if it adds any "overhead."

+5  A: 

try:

if ($range <= 50 ? $data <= $range : $data >= $range) {

}

or use an eval()

jspcal
perfect, that seems to work. So lets say I added the additional "if/else" from the beginning of my code into the foreach loop, would that add "overhead?"
luckytaxi
@luckytaxi, don't worry about the overhead of an additional IF in PHP, the whole thing is bloat anyway and wont make 0.00001% difference to your running time!
Aiden Bell
Why are you so worried about "overhead" ? Is this a known bottleneck (seriously, i think you are optimising prematurely)?
ChristopheD
yep you could use an if/else as well, no diff in terms of speed
jspcal
Not really. If you read from a file or do a db lookup anywhere in your code, it will take 1000's of times longer than adding the if. Point being don't worry about such a minuscule optimization unless you are running this block in 100,000 iterations.
Byron Whitlock
@ChristopheD, calm down, I'm just asking a question. Thanks for playing.
luckytaxi
@Byron - possibly. It's pulling from a "table" with 42,000 rows.
luckytaxi
A: 

You appear to be missing a closing }

graphicdivine
hah, yea just caught that. thanks. i do have it in my code though.
luckytaxi
A: 

I think you are going to need to do this:

foreach ( $cursor as $s ) {
    $data = round($this->distance($zip_lat, $zip_lon, $s["lat"],$s["lon"]), 2);
    if ( $range <= 50 ) {
      if ( $data <= $range ) {
          $zipcodes[] = "$s[zipcode]";   
      }
    } else {
      if ( $data >= $range ) {
          $zipcodes[] = "$s[zipcode]";   
      }
    }
}   
OneNerd
+4  A: 
 if ($data .$operator. $range) 

Is always true, because it is a string ,not a null.

You can find out problem using this simple code:

$data="0";
$operator=">=";
$range="1";

if ($data .$operator. $range) {
       echo   $data .$operator. $range . " is true !";   
}                   
amir beygi
I disagree with the 'always' part, consider: `$data = '0'; $operator = ''; $range = ''; if ($data .$operator. $range){ echo 'this statement is always true'; };`
ChristopheD
A: 

You cannot have a variable representing an evaluation operator. You would have to switch your code to something like:

foreach ($cursor as $s) {
    $data = round($this->distance($zip_lat, $zip_lon, $s["lat"],$s["lon"]), 2);
    if ($range <= 50 && $data <= $range) {
        $zipcodes[] = $s['zipcode'];
    } else if ($data >= $range) {
        $zipcodes[] = $s['zipcode'];
    }
}
cballou
+1  A: 

The 'dots' only do regular 'string' concatenation - you can't expect them (injected as strings) to behave like regular, 'real' operators.

Think about it: if $data = 'data1' and $range = 50 your if statement becomes:

if ('data1<=50') which will probably just evaluate to true or false, just as if ('yournamehere') or if('randommumbojumbo')

ChristopheD
right, but since in my case both $data and $range are numeric figures, I thought I could throw the "operator" into the mix.
luckytaxi
A: 

I very much suspect that the if is simply evaluating the string $date.$operator.$range (which will always return true), as all you're doing is concatenating the operator and operands together.

As such, you may need to eval (duck and cover people, duck and cover) the contents of the if.

middaparka