0

I have been using php for webapp development and constantly i come across variable not defined or index not defined error/warning.

Usally we do check it by isset(variable) or empty() . But the problem I is that I have to write code for checking my entire variables(which can be empty) null or not by functions isset(variable) or empty() ,this makes code ugly.

Can anyone suggest any aother method rather than checking variables before using it??

1
  • 2
    My suggestion is to always make sure a variable is assigned before it can be used - after this you only need to check for "isset" when reading from arrays, such as $POST, and can consider everything else a programming error. Quickly and uniformly extract $POST/GET data into variables (or another appropriate type), and use parameters/arguments to minimize global state. Commented Apr 12, 2014 at 17:29

2 Answers 2

1

It's best to always define your variables before use.

I normally declare my variables with a default value. So you always know its going to be defined.

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

Comments

0

You should always define variables in php. But if you really want to do things wrong, you can always disable E_NOTICE in PHP:

error_reporting(E_ALL & ~E_NOTICE);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.