[PHP][HELP]Not sure what's wrong with this.[HELP][PHP]

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Hello, Devbest.

I'm working on a little script, but I seem to have ran into a little problem.
First, let me paste my code.

PHP:
            $reqs = $this->get_reqs;
            if(isset($_GET))
            {
                $get = $_GET;
                $num = count($reqs);
                $i = 0;
                foreach($reqs as $k => $v)
                {
                    if($i != $num)
                    {
                        if(array_key_exists($k, $get))
                        {
                            $func = "\$this->" . $reqs[$k] . "();";
                            eval($func);
                        }
                        else
                        {
                            $i++;
                        }
                    }
                    else
                    {
                        echo "no get found.";
                    }
                }
            }
            else
            {
                echo "No get found.";
            }

On line 28 & 22 I echo "No get found" if a) [line 28] $_GET is not set or b) [line 22] the key wasn't found in the array.
Now, both of these errors are shown if I set $i (set on line 5) to 1, however this causes problems when $num (set on line 4) is set to 0.
When I echo $num & $i I get "11", which means $i is equal to $num, however, line 22 should be called when $i == $num.
So why isn't it echoing?
 
Last edited:

Heaplink

Developer & Designer
Nov 9, 2011
510
173
$reqs is not declared to any object or array. I guess you wan't to switch $get with $reqs. Also since $_GET is already a short name, I don't see the idea in putting it in a variable.

Also never use eval, it can be very evil. So when you wan't to assign a function with the var name you can just do something like

PHP:
$funcName = "myfunc";
$this->{$funcName}();

Also you don't need to check if the key $k exists. It does if its iterated in the foreach loop anyway so that condition is not doing anything useful.

This is what you mostly want:
PHP:
foreach($_GET as $k => $v) {
    if(is_string($v)) { // Make sure it's a string, since we can't have an array as function.. duh
        $this->{$v}(); // You could pass arguments here too
    }
}
 
Last edited:

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
I apologise, $reqs = $this->get_reqs; <-- I had missed this part.
Currently, $this->get_reqs = array("page" => "loadPage");
 

Heaplink

Developer & Designer
Nov 9, 2011
510
173
Btw if you're trying to do routes anyway you should use something like which is a very simple router for PHP
 

Users who are viewing this thread

Top