5

I recently setup a new web server and I'm getting undefined variable error. If I use variables without initializing, it gives me an error. The source code did not change. Only the LAMP environment did. How would you solve this problem?

Thanks

2
  • 2
    Need to shut off/lower the error checking either in the php.ini or using error_reporting -- though you should be defining your variables before using them. ;p Commented Feb 21, 2011 at 1:23
  • See the correct answer here: stackoverflow.com/a/71127489/388994 Commented Jul 14, 2022 at 12:13

3 Answers 3

11

you can change your error_reporting as people said, but if you want keep the default error_reporting configuration. you may use @ operator. e.g: @$post

Sign up to request clarification or add additional context in comments.

5 Comments

Downvotes because you told someone to use error suppression.
I give an alternative answer, as you can see in my begining of answer I still suggest to use error_reporting
It doesn't deserve downvotes because I just learned something new from this :)
@RJFares Thanks. :-)
On the other hand, I know there may be some null or undefined value, and I am going to handle it below, but I do not want to supress other warning that I have not handled. It looks so clumsy to make checking with ` if (!isset(...) || !isset(...))` but if (!$var0 || !$var1) looks much clearer
10

You can set notices to not show.

error_reporting(E_ALL & ~E_NOTICE)

You should be developing with

error_reporting(E_ALL | E_STRICT)

Comments

8

Well...

  1. You should define all your variables, those warnings are there for a reason, to make you code better. Undefined variables can easily lead to typo errors in variable names.

  2. You can change the *error_reporting* level, above E_NOTICE to get rid of that, but it is highly unadvisable.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.