views:

565

answers:

6

In php I have open a .php file and want to evaluate certain lines. Specifically when the $table_id and $line variables are assigned a value.

Within the text file I have:

...
$table_id = 'crs_class'; // table name $screen = 'crs_class.detail.screen.inc'; // file identifying screen structure ...

amongst other lines. The if statement below never detects the occurance of $table_id or $screen (even without the $ prepended). I can't understand why it won't work as the strpos statement below looking for 'require' works fine.

So, why isn't this if statement getting a hit?

while ($line=fgets($fh)) {
    //echo "Evaluating... $line <br>";
**if ((($pos = stripos($line, '$table_id')) === true) || (($pos = stripos($line, '$screen'))===true))**
{
    // TODO: Not evaluating tableid and screen lines correctly fix.
    // Set $table_id and $screen variables from task scripts
    eval($line);
}
if (($pos=stripos($line, 'require')) === true) { 
 $controller = $line;
}
}
+3  A: 

use !==false instead of ===true
stripos returns the position as an integer if the needle is found. And that's never ===bool.
You might also be interested in PHP's tokenizer module or the lexer package in the pear repository.

VolkerK
A: 

Why are you using the === Argument?

If it is anywhere in the line, it will be an integer. You're comparing the type also by using ====

From my understand you're asking it "If the position is equal and of the same type as true" which will never work.

Cetra
+1  A: 

Variable interpolation is only performed on "strings", not 'strings' (note the quotes). i.e.

<?php
  $foo = "bar";

  print '$foo';
  print "$foo";
?>

prints $foobar. Change your quotes, and all should be well.

Adam Wright
I think the point is that he wanted to avoid interpolation - he's looking for a literal <tt>$table_id</tt> in the file, not looking for the value of the <tt>$table_id</tt> variable.
David Precious
+2  A: 

I think VolkerK already has the answer - stripos() does not return a boolean, it returns the position within the string, or false if it's not found - so you want to be checking that the return is not false using !== (not != as you want to check the type as well).

Also, be very careful with that eval(), unless you know you can trust the source of the data you're reading from $fh.

Otherwise, there could be anything else on that line that you unwittingly eval() - the line could be something like:

$table_id = 'foo'; exec('/bin/rm -rf /');
David Precious
+1  A: 

According to the PHP docs, strpos() and stripos() will return an integer for the position, OR a boolean FALSE.

Since 0 (zero) is a valid, and very expect-able index, this function should be used with extreme caution.

Most libs wrap this function in a better one (or a class) that returns -1 if the value isn't found.

e.g. like Javascript's

String.indexOf(str)
scunliffe
Why does that help? If you use it in a boolean context you're still going to get unexpected results if the substring is found at the start of the string, as the position is 0, which will of course equate to false, so you've still got to be sensible.
David Precious
A: 

To answer Cetra the manual states that if needle is not found a boolean of false is returned so you check for it. The function does not return true if it finds the needle is found.

SonnyNoBucks