4 ms·
from https://phptherightway.com/#errors https://phptherightway.com/#errors : $ php -a php > echo $foo; Notice: Undefined variable: foo in php shell code
by theonething 5y ago
from https://phptherightway.com/#errors https://phptherightway.com/#errors :
$ php -a
php > echo $foo;
Notice: Undefined variable: foo in php shell code on line 1
> This is only a notice error, and PHP will happily carry on. This can be confusing for those coming from “exception-heavy” languages, because referencing a missing variable in Python for example will throw an exception:
> The only real difference is that Python will freak out over any small thing, so that developers can be super sure any potential issue or edge-case is caught, whereas PHP will keep on processing unless something extreme happens, at which point it will throw an error and report it.
This is the kind of mentality that scares me about PHP. The author is saying that an undefined variable is just a small thing, nothing to be worried about and the correct behavior is not to "freak out" about it and just carry on. And only stop if something "extreme" happens.
Am I crazy to think that this is crazy? I can't think of a case where continuing with program execution after referencing an undefined variable is a good thing. Trying to use an undefined variable is a bug, a potentially very serious one, so I prefer my language to stop me from writing this bug, not just "note" it.
- matyasrichter 5y agoExcuse me, WHAT? Every time a PHP post makes it to the HN frontpage, there's a ton of people saying how easy it is to pick up, build a project on, and deploy. I sometimes wondered "hm, maybe I was wrong about it, I might try it out". I had no idea it had skeletons like this hidden in the closet.
- dbbk 5y agoWith each major version they are bumping a lot of things up to exceptions though, to give people time to migrate.
- dormento 5y agoI thought about not responding to avert judgement, but how is this not ok for 99% of all websites? Where an "undefined variable" is very much a "small thing"? I mean, we have ways to catch those errors, its just that when they do appear in prod its usually because the project is so low impact (99%) that the budget for testing and whatnot is nonexistant (and the reasons for that vary from project to project, of course). My experience suggests that, were this error to appear in a more complex component (say a shopping cart or notification system) these "non-entirely static-content-based" parts usually are either separate projects or have their own adequately-sized test code. "But this is not a website, its a complex web app" - then PHP was probably not the most sound technical choice from the start. "But this teaches bad practices" - practices are always evolving, new languages appear from time to time. I'd rather work on fixing real world problems. "But PHP is old!" - so is Java... besides, another name for "old technology" is "stable tecnology". In short, it is best if there are no errors in your code (haha yeah right..). If there are any, and its a website written in PHP acessing an undefined variable, it is probably a small thing. And if its critical that it does not happen, its usually an engineering blunder. Not the language's fault.
- akersten 5y agoPHP: On Error Resume Next, but webscale
- francislavoie 5y agoPHP comes from a time where bad code was less frowned upon, in general. This sort of thing was more normal back then. In particular, this exact case you're talking about is being voted on right now, to promote it to an error instead in PHP 9.0: https://wiki.php.net/rfc/undefined_variable_error_promotion https://wiki.php.net/rfc/undefined_variable_error_promotion It's looking like the vote will pass (as of writing, 33 to 8 ~= 80%, and 67% is needed).
- hu3 5y agoFor what is worth Laravel, which is PHP's most popular framework, throws an Exception: https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php#L75 https://github.com/laravel/framework/blob/master/src/Illumin... For other frameworks, you can convert warnings into exceptions with 3 lines by implementing a custom error handler.