Newbie - Notice: Undefined variable

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Help! ive been happily coding away for hours only to get a 'Notice:
Undefined variable' error. I switched off the error reporting in
php.ini then created a helloworld.php file to check i wasnt going
crazy, but i am!

my helloworld.php file looks like this :

echo ("Hello $word !");

when i enter "http://localhost/helloworld.php?word=world" in the
address bar i still only get "Hello !" !!!

what in the world ?

Re: Newbie - Notice: Undefined variable

Quoted text here. Click to load it

 Are you working from an old book or tutorial? Because $word will be undefined
in the example you gave, unless you explicitly turn on a deprecated
configuration option.

Andy Hassall :: :: :: disk and FTP usage analysis tool

Re: Newbie - Notice: Undefined variable

Try this instead:
$word = "PHP Student";
echo ("Hello $word !");


Re: Newbie - Notice: Undefined variable

Quoted text here. Click to load it

You can't refer to HTML form variables in this way unless you explicitly  
tell PHP to give you them. There are two popular ways to get at form  

1. Built-in variable sets
Historically, you have been able to refer to form variables using  
$HTTP_POST_VARS (for POST forms) or $HTTP_GET_VARS (for GET requests like  
the one you described). So your variable would be referred to as  
$HTTP_GET_VARS["word"]. Since 4.1.0 there's a shorthand - you can use  
$_GET["word"]. Incidentally, if you use a mix of GET and POST forms, you can  
get at stuff as $_REQUEST["word"].

2. import_request_variables
PHP can be told to import form variables and make them PHP variables in  
their own right. This is the approach I use mostly. If, at the start of your  
script, you say:


... it will take all GET and POST (the "gp") variables and make them into  
PHP variables in their own right, prefixing them (in this case) with "form_"  
so that they don't inadvertently trample on variables in your script with  
the same name. So in your example you'd refer to $form_word.



Site Timeline