0

as you can see from my code below I have set a variable ($query) equal to the data posted from an outside form. Under that I tested the variable by echoing it, so the variable seems to be established correctly.

The problem is that near the bottom I'm trying to create another variable, called $str_to_find, where I want it set to output my original variable, $query. However, when I view the output, nothing shows up at all after the code processes this variable near the bottom of my code. I dont' understand why it wouldn't display output.

<?php
$query = $_POST['query']; 

echo "$query"; 

find_files('.');
function find_files($seed) {
    if(! is_dir($seed)) return false;
    $files = array();
    $dirs = array($seed);
    while(NULL !== ($dir = array_pop($dirs)))
    {
        if($dh = opendir($dir))
        {
            while( false !== ($file = readdir($dh)))
            {
                if($file == '.' || $file == '..') continue;
                $path = $dir . '/' . $file;
                if(is_dir($path)) {
                    $dirs[] = $path;
                }
                else {
                    if(preg_match('/^.*\.(php[\d]?|js|txt)$/i', $path)) {
                        check_files($path);
                    }
                }
            }   
            closedir($dh);
        }
    }
}

function check_files($this_file) {
    $str_to_find = $query;
    if(!($content = file_get_contents($this_file))) {
        echo("<p>Could not check $this_file</p>\n");
    }
    else {
        if(stristr($content, $str_to_find)) {
            echo("<p>$this_file -> contains $str_to_find</p>\n");
        }
    }
    unset($content);
}
?>

UPDATED CODE

<?php
 $query = $_POST['query'];



find_files('.');
function find_files($seed) 

{


if(! is_dir($seed)) return false;
$files = array();
$dirs = array($seed);
while(NULL !== ($dir = array_pop($dirs)))
{
  if($dh = opendir($dir))
    {
      while( false !== ($file = readdir($dh)))
        {
          if($file == '.' || $file == '..') continue;
          $path = $dir . '/' . $file;
          if(is_dir($path)) {    $dirs[] = $path; }
          else { if(preg_match('/^.*\.(php[\d]?|js|txt)$/i', $path)) { check_files($path); }}
        }
      closedir($dh);
    }
}
}

function check_files($this_file) 
{

$query = $_POST['query'];

$str_to_find = $query;
if(!($content = file_get_contents($this_file))) { echo("<p>Could not check $this_file</p>\n"); }
else { if(stristr($content, $str_to_find)) { echo("<p>$this_file -> contains
$str_to_find</p>\n"); }}
unset($content);
}

?>
2
  • 1
    php.net/manual/en/language.variables.scope.php $query is defined outside the function check_files(), so it doesn't exists there: either make it global or, better, pass it as an argument to the function Commented Feb 24, 2012 at 20:51
  • 1
    function check_files($this_file) { global $query; $str_to_find = $query; Commented Feb 24, 2012 at 20:52

4 Answers 4

3

This is an issue of scoping. Your $query variable (and indeed any variables not instantiated directly within the function body) is not available within check_files.

You should pass $query in as a parameter to the function.

function check_files($this_file, $query) {
    // ...
}

Another options exists to make the variables 'global', however this is seldom a sensible idea.

Sign up to request clarification or add additional context in comments.

5 Comments

+1, but not for the global suggestion. If you have to use global, you are most likely doing it wrong.
@simshaun I have suggested it not to be a good idea, however it is still a valid option and I think something to be aware of with respect to variable scope.
Thanks jstephenson, I've tried all of the ideas for this post, and I'm still not getting output...hmmm?
You will need to pass $query into file_files as well (if you haven't already), as of course this suffers the same problem when attempting to pass it on to check_files.
jrstephenson, you can see I posted the code that gave me output. The only problem now is that whenever I type certain variations of characters into the form, it will sometimes show nothing, then for other characters it shows just fine. The best results happen when I use a single digit. This even more absurd because it an input type=text on the original form. Do you have any thoughts on this?
2

The reason it's not working is because $query is out of the function's scope. If you want to use a variable declared outside of a function inside of it, you either need to pass it through as a parameter, or use

function check_files($this_file) {
    global $query;
    $str_to_find = $query;

Although passing it through as a parameter is preferred to using global.

Comments

0

The variable $query is declared outside the function check_files() scope. If you want to access it, put global $query; at the beginning of the function.

Comments

0

You need to declare the the $query global as per the PHP manual, if not, the parser will assume that $query is a local scope variable (local as in "only within this function")

function check_files($this_file) {
global $query;
$str_to_find = $query;
...

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.