PHP, Why doesn't anyone use the ?: operator? -


I can not discuss anything about it.

In JavaScript to check if something exists, and use default if it is not:

  var myvariable = mysetting || 3   

What I believe in PHP can be done as such:

  $ myvariable = $ mysetting ?: 3;   

Am I completely closed on this? I understand the implications of not using isset () and not_empty () and all of these, but if I just want to know that the variable exists and the truth is otherwise the default Uses - It should work. I think there are hidden things about this in this syntax?

This code will still notify if it does not work Notice: Undefined Variable: mysetting In C: \ wamp \ www \ x.php line , which can be visible to the user on the basis of PHP settings, apart from this, it will work if the notice is pressed, the final result is correct.

So, to move around it, you can either use the isset , which is not actually a function, but a language creation, and especially It is designed to check:

  $ myvariable = isset ($ mysetting)? $ Mysetting: 3; You can press the notice using   

or you can use the @ operator:

  $ myvariable = @ $ mysetting ?: 3 ;   

For this specific case, it is probably acceptable, but the use of @ in general is harassed by many people privately, I have some more I use the letter and feel less 'dirty', but it is a matter of opinion.

There is another reason why people should not use it, it is relatively new (PHP 5.3). Everyone can know about this new syntax or can not remain comfortable with it. They have been used for years for isset , and old habits die.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -