I've being using this trick for years instead of the ugly
isset($array['key']) ? $array['key'] : null;
thinking I was simply ignoring those stupid "Undefined index" notices.
Now I figure out I was also hiding "Undefined variable" notices when $array was not defined and a ton of potential errors when $array is an object implementing ArrayAccess.
Yes, the main issue with using @ is that it conceals side effects and sweeps all errors under the rug -- including the ones that you may actually want to handle.
Although there may be some scenarios where it's permissible, such as @unlinking temporary files when upload validation fails. It only happens in exceptional cases, the outcome is not immediately crucial, and you don't have any dependent operations.
All of this is moot when you do the following though:
- Set error_reporting to -1 (report ALL errors)
- Register a custom error handler that converts all errors to exceptions
This renders the @ operator useless (it'll throw an exception regardless). I personally prefer this since it enforces error handling discipline early on.
That's an interesting idea on the custom error handler, I wasn't aware that those trigger regardless of @s.
I guess a lot of the trouble with @ is that it affects everything that happens temporally-within the expression, but people often use it as if it only affects everything syntactically-within it... for instance, recently I came across something like:
... which would also hide any warnings that occur in the body of returns_a_list(), despite that being neither intended nor wanted.
It's a shame the only @-free solutions to many popular uses of @ (like that one) are so hideously ugly. Also that I suspect that changing @ so it only affected syntactically-contained errors would be... impractical for the PHP team, for a number of reasons.
It'd be better if you told us why you are using Smarty in the first place. I suspect a lot of places use it simply because they've heard a lot of other places use it.
My company also uses Smarty, but we're planning to move away from it. (Currently evaluating Mustache.php)
Smarty's parser/lexer is horribly slow. The 3.1.x branch has caused problems for us, such as corrupted compiles and broken nested blocks [1]. As evidenced by the article, the developers don't seem to know what they're doing. You should see the workaround for people with custom error handlers -- registering another error handler on top that checks the source of the error against registered template and cache directory paths!
sounds about right. I don't think anyone would choose Smarty for actual technical reasons at this point. I personally think plain PHP makes a far superior templating system. Smarty is slow, doesn't "protect" designers from any complexity because it's complex itself and is yet another thing to learn.
Otherwise PHP itself is a template language. That is why you start it with <? tag.
Just keep short tags on [it's off by default on new installations but ON by default on all hosting sites] and you got a better template engine than smarty.
PHP does not support HTML escaping and is therefor not secure by default. At least twig escapes HTML by default (I am not up to date with PHP so the others might too).
You do not want to type <?php echo htmlspecialchars($var, ENT_QUOTES) ?> every time you want to output data. (Yes, I know it could probably be written shorter but my PHP is rusty. My point still remains though, you have to remember to type it every time.)
I think my solution back in the day was just to include the template files by running a function, something like showTemplate($templateName, $templateVars). The function takes an array as an arg so only these values are available to the template context (apart from the many global vars of course).
You can then run this entire array through htmlentities or htmlspecialchars before doing include().
this is exactly how many modern frameworks do it. your response body is assembled and cached as your application executes, along with any variables it needs, and before the template is rendered the variables are sanitized.
I think it's good this way, it forces the developer to think about the implications of escaped vs unescaped output. The way I see it, having htmlspecialchars/htmlentities applied automatically by a template engine is a close relative to the magic quotes; it abstracts something esential for the developer to know.
There should be a big fat warning in front of this very bad advice, and I particularly dislike the last paragraph about intercepting error handlers to cover up your ugly hack.Some newcomer is going to run with this idea without understanding the many, many problems it can introduce. Until 5.4 the @ operator caused dramatic slowdowns on everything it touched because PHP had to jump through hoops to suppress the errors. It's funny to see someone advocating using it for performance reasons now.
If you need a mutex, you should use a mutex.
That example is amongst the worst synthetic benchmarks I have seen in a while. Also it comes from someone who is associated with piwik. A PHP project that makes wordpress look tidy, well engineered and speedy.
The more deeply nested the callstack is when you supress, the bigger the performance hit (slight generalisation and slightly from memory).
I spent 10-15 minutes poking around php internals newslist for an explanation of the php 5.4 changelog "improved performance of the (@) silence operator" without luck. It's just listed as a zend engine improvement without specifications.
Someone posted here a clear example of why (in earlier php5) it generates far worse opcode:
I'm being offered a "Serbo-Croatian" translation and the site's captcha is broken.
Might be unwise to judge authority on the subject just from that, however I decided not to read the article.
You are correct. It is wrong -very very wrong- to view programming advice as suspect merely because the content is offered in a language you don't speak.
projects like this are why coffeescript needs to support macros. There are so many coffeescript dialects out there that make minor changes to the language to accomodate one or two extra features, e.g. IcedCoffee. I don't want to have to use a separate, presumably less well supported language for this kind of stuff, just let me plug it in!
> The golden rule of CoffeeScript is: "It's just JavaScript"[0]
Macros would be deviating from core JS too much to fit with CoffeeScript's goal. Just happens that the compiler is nicely hackable to allow for these projects.
Not to say I object to the idea of an expanded CoffeeScript, just that it should be a fork.
Yours is such a wilfully ignorant post, I'm not sure where to begin, or even if it's worth replying...
How do you explain the countless genocides in countries where guns are or were freely available? What about the Soviet dissidents that were exterminated before 1929? Would widespread availability of guns have prevented the holocaust - of course not!
It is the people that have the power to prevent genocide, not guns.
Your comparison of gun related deaths to natural deaths is completely nonsensical.
How about this, the USA has both one of the highest murder rates, and some of the most relaxed gun laws in the first world. I am 3.5x as likely to get murdered in the USA compared to the UK. Around 66% of murders in the USA involve a firearm compared to 7% in the UK.
I am 33x as likely to be shot to death in the USA compared to the UK.
Don't you think that someone with easy access to a gun is more likely to commit murder in the heat of an argument compared to someone who doesn't?
Do you really believe that easy access to guns is a good thing?
Do you really believe that having access to guns will prevent a dictatorship in the USA? I don't. I think that would ensure a bloody civil war. As someone else on here pointed out recently, non violent mass protest is far more likely to effect change because a striking workforce can cripple an economy rather quickly.
A striking work force isn't going to work when you are easily thrown into forced labor camps because you have been neutered in your ability to defend yourself.
Please answer all my questions rather than just sniping at one of them.
How are your small arms going to defend against tanks, apaches, drones and the rest of your government's military might? Your reasoning is at least 60 years out of date. The prevalence of guns in america is a cancer on the american people.
Guns are tools that kill. They have no other purpose. How anyone can defend the concept of gun ownership in a civilised society is genuinely beyond me.
> Guns are tools that kill. They have no other purpose.
The other† purpose is to keep countries and people in check. The mere ability to immediately use firearms is enough to stop a lot of possible troublemakers.
A country keeps armed forces: military and border guard to keep external actors in check, police to keep unruly citizens in check. A private person keeps arms to keep in check potential burglars, assailants etc., and any other actor who could come in force, of which there are some historical examples discussed in other posts.
† aside of the sporting, hunting and collector activities.
I have no problem with the military and police having access to weapons. They are (or should be) highly trained, Psych tested and they work within a regulated, highly structured, hierarchical organisation. All of this serves to keep the wanton violence (almost completely) in check.
The issue arises when every schmoe off the street can go in and purchase a fully automatic, purpose built people killing machine.
I also don't buy the people vs. the government scenario you allude to. Could you really picture a reality where a group of citizens could overthrow the US government just because they had some AK47s?
In the event of run-away government, a far more rational and realistic scenario would be for mass protests that essentially halt the economy over night. There are countless examples of almost completely bloodless revolutions happening in exactly this manner (Polish Solidarnosc and more recently Egypt during the Arab Spring immediately come to mind). The alternative is a bloody coup which usually ends in civil war or military dictatorship.
levers as in how to operate a specific kind of lever/switch or do you mean "teaching" the abstract concept of levers? e.g. recognising that this thing can be used as a lever? The latter sounds much harder