If I copy a piece of code from somewhere that looks like so

$blah = array(1,2,3,4);
foreach ($blah as $i)
       echo ($i);

and rename the variables but forget to do it correctly like so

$apple = array(1,2,3,4);
foreach ($blah as $i) <--- notice $blah instead of $apple
       echo ($i);

Then my NetBeans IDE doesn't complain and I get an error at runtime when I run this. Is there a way to catch errors like this without running the code? Which IDE does it? Which plugin? Or the whole PHP development world is living without this?

  • 1
    The easiest fix is to use a real (read: statically typed) programming language. – ThomasX Dec 15 '11 at 11:51
  • @ThomasX This has nothing to do with static typing. What is horribly lacking in PHP, Python etc. is a way to mark newly introduced variables. In Perl this is my, in LaTeX this is \newcommand versus \renewcommand: nothing to do with static typing. More on this here. – Alexander Gelbukh Jul 24 '14 at 8:48

PhpStorm from jetBrains does it.

PhpStorm is a lightweight and smart PHP IDE focused on developer productivity that deeply understands your code, provides smart code completion, quick navigation and on-the-fly error checking. It is always ready to help you shape your code, run unit-tests or provide visual debugging.

| improve this answer | |
  • I know I can google it but a link would be nice :-) – Luc M Dec 14 '11 at 18:02

To add to the advice above : PHP Manual - (PHP 4, PHP 5)

isset — Determine if a variable is set and is not NULL

bool isset ( mixed $var [, mixed $... ] )

Determine if a variable is set and is not NULL.

If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL. Also note that a NULL byte ("\0") is not equivalent to the PHP NULL constant.

If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered.

| improve this answer | |
  • So you're suggesting before using any variable we should call isset? – Muhammad Hasan Khan Dec 19 '11 at 18:24
  • No, I am suggesting that if you use built in PHP constructs then good programming will help catch these types of errors both before and during run-time. If you are in the habit of checking then you might catch the fact that you are referencing the wrong name or a variable that has not been set. A good IDE will warn you at best. – SoftwareCarpenter Jan 3 '12 at 23:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.