Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
1  
The easiest fix is to use a real (read: statically typed) programming language. –  ThomasX Dec 15 '11 at 11:51
add comment

2 Answers

up vote 3 down vote accepted

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.

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

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.

share|improve this answer
    
So you're suggesting before using any variable we should call isset? –  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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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