Discussion:
undefined $_GET index.
atar
2013-12-25 00:48:36 UTC
Permalink
Hi there!!
<?php
var_dump($_GET['r']);
?>
and save it to a file named: test.php.
Afterwards, I've launch the PHP binary on my Debian Linux machine with the
"php -S 127.0.0.1:8000 test.php"
in order to use the built-in PHP web server.
"http://127.0.0.1:8000/test.php?r=something"
"[Tue Dec 24 23:56:31 2013] PHP Notice: Undefined index: r in
/home/user/Desktop/new_raf_jewl/test.php on line 3"
My question is simply why? what's wrong with my above PHP snippet? and the
strange thing here is that the PHP binary replies to the browser with the
" string(9) "something" "
so what's going here? is the $_GET['r'] index is defined or not?

NOTE: I'm using the php-cli (Command Line Interface) version of the PHP
binary.

Regards,

atar.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph Michael Becker
2013-12-24 23:11:05 UTC
Permalink
Post by atar
<?php
var_dump($_GET['r']);
?>
and save it to a file named: test.php.
Afterwards, I've launch the PHP binary on my Debian Linux machine with
"php -S 127.0.0.1:8000 test.php"
in order to use the built-in PHP web server.
"http://127.0.0.1:8000/test.php?r=something"
"[Tue Dec 24 23:56:31 2013] PHP Notice: Undefined index: r in
/home/user/Desktop/new_raf_jewl/test.php on line 3"
My question is simply why? what's wrong with my above PHP snippet? and
the strange thing here is that the PHP binary replies to the browser
" string(9) "something" "
so what's going here? is the $_GET['r'] index is defined or not?
NOTE: I'm using the php-cli (Command Line Interface) version of the PHP
binary.
Um, the command line for starting the built-in webserver seems wrong.
According to the documentation[1], test.php is used as router script,
what is probably not intended. Just try to start the server without the
script argument:

php -S 127.0.0.1:8000

[1]
<http://www.php.net/manual/en/features.commandline.webserver.php#example-383>
--
Christoph M. Becker
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
atar
2013-12-25 02:51:01 UTC
Permalink
At Christoph Michael Becker:

Thanks you for your advice! it solved the problem.
But I didn't succeed to understand HOW the action of dropping the script
argument from the command line was worked? why does PHP complain about the
undefined $_GET['r'] variable when the test.php script is used as router?

Regards,

atar.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
2013-12-25 00:55:50 UTC
Permalink
Post by atar
Thanks you for your advice! it solved the problem.
But I didn't succeed to understand HOW the action of dropping the script
argument from the command line was worked? why does PHP complain about the
undefined $_GET['r'] variable when the test.php script is used as router?
Regards,
atar.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
In PHP, when you try to access a variable and that variable doesn't exist,
it throws a notice. In your case the router.php was executed with no r GET
param, causing the notice.

You can duplicate that by accessing: http://127.0.0.1:8000/test.php (notice
the r GET param is not set).

As a good practice you should always use isset() before trying to access a
param that might not be set:

if (isset($_GET['r'])) {
// use it
}
tamouse pontiki
2013-12-25 02:18:53 UTC
Permalink
Post by Aziz Saleh
Post by atar
Thanks you for your advice! it solved the problem.
But I didn't succeed to understand HOW the action of dropping the script
argument from the command line was worked? why does PHP complain about
the
Post by atar
undefined $_GET['r'] variable when the test.php script is used as router?
Regards,
atar.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
In PHP, when you try to access a variable and that variable doesn't exist,
it throws a notice. In your case the router.php was executed with no r GET
param, causing the notice.
You can duplicate that by accessing: http://127.0.0.1:8000/test.php(notice
the r GET param is not set).
As a good practice you should always use isset() before trying to access a
if (isset($_GET['r'])) {
// use it
}
Oops, Aziz scooped me on this one!
atar
2013-12-25 09:00:43 UTC
Permalink
Post by Aziz Saleh
Post by atar
Thanks you for your advice! it solved the problem.
But I didn't succeed to understand HOW the action of dropping the script
argument from the command line was worked? why does PHP complain about the
undefined $_GET['r'] variable when the test.php script is used as router?
Regards,
atar.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
In PHP, when you try to access a variable and that variable doesn't exist,
it throws a notice. In your case the router.php was executed with no r GET
param, causing the notice.
You can duplicate that by accessing: http://127.0.0.1:8000/test.php (notice
the r GET param is not set).
As a good practice you should always use isset() before trying to access a
if (isset($_GET['r'])) {
// use it
}
But why when I use the 'test.php' script as a router, the $_GET['r'] index
isn't get defined? that's what wonder me.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
2013-12-25 19:52:15 UTC
Permalink
Please refer to the reference Christopher posted on server routers. The
router is executed before your script is executed. The same way if you were
executing: php test.php.

Basically your code was executing twice, once as a router without the r
param and once via the browser with the r param. The router was the one
throwing the error.
Post by atar
Post by Aziz Saleh
Post by atar
Thanks you for your advice! it solved the problem.
But I didn't succeed to understand HOW the action of dropping the script
argument from the command line was worked? why does PHP complain about the
undefined $_GET['r'] variable when the test.php script is used as router?
Regards,
atar.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
In PHP, when you try to access a variable and that variable doesn't
exist,
it throws a notice. In your case the router.php was executed with no r GET
param, causing the notice.
You can duplicate that by accessing: http://127.0.0.1:8000/test.php(notice
the r GET param is not set).
As a good practice you should always use isset() before trying to access a
if (isset($_GET['r'])) {
// use it
}
But why when I use the 'test.php' script as a router, the $_GET['r'] index
isn't get defined? that's what wonder me.
tamouse pontiki
2013-12-25 02:18:12 UTC
Permalink
Post by atar
Thanks you for your advice! it solved the problem.
But I didn't succeed to understand HOW the action of dropping the script
argument from the command line was worked? why does PHP complain about the
undefined $_GET['r'] variable when the test.php script is used as router?
When you ran it the first time, from the command line, there was no r
parameter (or query string)
Post by atar
Regards,
atar.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
atar
2013-12-25 22:30:09 UTC
Permalink
Thanks you for your explanation!! it help clarify me the technical process.
Post by Aziz Saleh
Please refer to the reference Christopher posted on server routers. The
router is executed before your script is executed. The same way if you
were
executing: php test.php.
Basically your code was executing twice, once as a router without the r
param and once via the browser with the r param. The router was the one
throwing the error.
Post by Aziz Saleh
Post by atar
Thanks you for your advice! it solved the problem.
But I didn't succeed to understand HOW the action of dropping the
script
argument from the command line was worked? why does PHP complain about
the
undefined $_GET['r'] variable when the test.php script is used as
router?
Regards,
atar.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
In PHP, when you try to access a variable and that variable doesn't
exist,
it throws a notice. In your case the router.php was executed with no r
GET
param, causing the notice.
http://127.0.0.1:8000/test.php(notice
the r GET param is not set).
As a good practice you should always use isset() before trying to
access a
if (isset($_GET['r'])) {
// use it
}
But why when I use the 'test.php' script as a router, the $_GET['r']
index
isn't get defined? that's what wonder me.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Loading...