Simone
Simone

Reputation: 21272

Weird behaviour of _GET variables

Actually this is my code:

echo "<pre>";
// get current vars
$p1 = isset($_GET['p1']) ? trim($_GET['p1']) : false;
$p2 = isset($_GET['p2']) ? trim($_GET['p2']) : false;
$p3 = isset($_GET['p3']) ? trim($_GET['p3']) : false;
$f1 = isset($_GET['f1']) ? trim($_GET['f1']) : false;
$f2 = isset($_GET['f2']) ? trim($_GET['f2']) : false;
$f3 = isset($_GET['f3']) ? trim($_GET['f3']) : false;
//Debug
echo "P1 from _GET: ".var_dump($p1);
echo "P2 from _GET: ".var_dump($p2);
echo "P3 from _GET: ".var_dump($p3);
echo "F1 from _GET: ".var_dump($f1);
echo "F2 from _GET: ".var_dump($f2);
echo "F3 from _GET: ".var_dump($f3);

When I went to http://localhost/test/index.php?p2=foo&f3=bar, I was expecting this result:

P1 from _GET: bool(false)
P2 from _GET: string(3) "foo"
P3 from _GET: bool(false)
F1 from _GET: bool(false)
F2 from _GET: bool(false)
F3 from _GET: string(3) "bar"

However, this is what I get:

P1 from _GET: string(3) "foo"
P2 from _GET: bool(false)
P3 from _GET: bool(false)
F1 from _GET: bool(false)
F2 from _GET: string(3) "bar"
F3 from _GET: bool(false)

Can you explain why?

Thanks for your time.

Upvotes: 2

Views: 198

Answers (1)

ceejayoz
ceejayoz

Reputation: 180024

var_dump is outputting before your echo statement actually executes.

echo "P1 from _GET: ".var_dump($p1);

should become:

echo "P1 from _GET: ";
var_dump($p1);

As a side note, when this gets executed, you actually (tested it myself) get the following, which should make it more obvious what's going on:

bool(false)
P1 from _GET: string(3) "foo"
P2 from _GET: bool(false)
P3 from _GET: bool(false)
F1 from _GET: string(3) "bar"
F2 from _GET: bool(false)
F3 from _GET:

Upvotes: 8

Related Questions