faqts : Computers : Programming : Languages : PHP : Common Problems : Errors

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

96 of 104 people (92%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

Getting "Warning: Undefined variable: foo in D:\path\dir\file.php on line n" for all unposted variables. This error occurs even for if($foo) {

Feb 3rd, 2001 01:17
Philip Olson, Mike Boucher,


This has to do with error reporting, read about this a bit here :
    http://www.php.net/manual/en/function.error-reporting.php 
And for a definition of types : 
    http://www.php.net/manual/en/phpdevel-errors.php
You may have E_ALL or E_NOTICE on within php.ini  BTW, consider doing 
this instead :
    if (empty($submit))  {
    - or -
    if (!empty($submit)) {
As using empty (or isset) for this task will not result in such errors.
    http://www.php.net/manual/en/function.empty.php
    http://www.php.net/manual/en/function.isset.php
As well as string comarison functions, such as :
    http://www.php.net/manual/en/function.strcmp.php
Essentially, when error reporting is turned up high enough, errors will 
result from undefined variables.  For example, this will also result 
with an error rather then return true :
    if ($var == '') {
As stated above, using empty or isset is preferred for this task.