0

If I try to use a variable which doesnt even exist in PHP, I dont get any errors or notices. In the example below the return value is "null". I'd like to see some form of notification if that happens so that I can find bugs in my code more easily.

echo $doesNotExist -> something; //returns null

The error reporting in my php.ini is the following

error_reporting = E_ALL

What do I have to do to see notifications when accessing varibles that don't exist?

To clarifiy this:

I know that I can check if a variable exists by using isset(). That's not what I want though. I want to get a notification in case I accidentaly try to use a variable that does not exists. For example if I misspell the name of a variable.

display_errors = On is in my php.ini

cmbuckley
  • 40,217
  • 9
  • 77
  • 91
marius2k12
  • 1,041
  • 1
  • 18
  • 32

3 Answers3

0

As well as error_reporting, you may need to turn on display_errors, which should be off in production environments.

Production environments can use log_errors instead, which outputs the errors to the file named in the error_log directive.

cmbuckley
  • 40,217
  • 9
  • 77
  • 91
  • Hum. What's the output of `phpinfo()`? It should tell you which INI files are being parsed as well as the values of the config options. – cmbuckley Sep 20 '13 at 13:03
  • error_reporting 32767 / display_errors On / log_errors On / error_log C:\xampp\php\logs\php_error_log – marius2k12 Sep 20 '13 at 14:03
  • Do you get the errors in the log? The only other thing that I can think of is that output buffering may be enabled, and causing the error not to be shown? – cmbuckley Sep 20 '13 at 20:40
  • I once had a problem with php not logging errors all the time and what I did to fix it was deleted the log file and let php create a new one. Perhaps you could try that. – starshine531 Oct 02 '13 at 11:49
-1

I generally use thw following;

if(isset($var) && !empty($var)) {
    //Do something with $var
} else {
    //Define $var
}

As said in a previous answer you also need to turn error reporting on in the php.ini

Phyore
  • 41
  • 10
-1

what you do is perfectly valid in PHP. It just creates and instantiates that variable at runtime, and doesn't throw an error like other languages.

even isset would not work too well. Say you set an object variable with a wrong name. If you do isset on that name, it will be true. If you do isset on the correct name, it will be false.

Example:

class test{
    public $name1;
}

$test = new test;
$test->name2 = 'wrong';

var_dump($test);

prints: object(test)#1 (2) { ["name1"]=> NULL ["name2"]=> string(5) "wrong" }

Aris
  • 4,643
  • 1
  • 41
  • 38
  • Simply not true — PHP does not create anything when accessing an undefined variable, and it *does* give an error. – cmbuckley Sep 20 '13 at 22:21
  • @cbuckley you are wrong. you can try the example code I added. – Aris Sep 21 '13 at 05:45
  • The OP was not talking about the member variable not existing, and was not talking about setting a member variable either. They were talking about the object variable not existing. [Like this](http://codepad.org/pfiwCiD8). – cmbuckley Sep 21 '13 at 12:21