0

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?

asked Dec 14, 2011 at 16:15
2
  • 1
    The easiest fix is to use a real (read: statically typed) programming language. Commented Dec 15, 2011 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. Commented Jul 24, 2014 at 8:48

2 Answers 2

4

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.

Matthieu
4,5992 gold badges37 silver badges38 bronze badges
answered Dec 14, 2011 at 16:21
1
  • I know I can google it but a link would be nice :-) Commented Dec 14, 2011 at 18:02
0

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.

answered Dec 19, 2011 at 17:28
2
  • So you're suggesting before using any variable we should call isset? Commented Dec 19, 2011 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. Commented Jan 3, 2012 at 23:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.