PHP: best practices for silent failure

I do a lot:

$something = @$_GET['else']; if ($something) { // ... } else { // ... } 

It seems like almost every time I deal with arrays. I'm used to JavaScript and being able to just check for a fake value. But is this suitable in PHP? Am I trying to get PHP to be what I already understand, instead of learning it as my own language?

EDIT

I know that I can just use isset (I also * under * stand), but this is inconvenient for me, and this leads to even more severe situations when I try to repeat the value:

 // what I want to do echo '<input type="text" name="whatever" value="', @$values['whatever'], '" />'; // what I fear I must do echo '<input type="text" name="whatever" value="'; if (isset($values['whatever'])) { echo $values['whatever']; } echo '" />'; 

Having discarded the sanitation problem, I prefer the first version. But I have a secret suspicion that this is a big no-no. (I also have a hidden suspicion that I don't know what to call "suspicion.")

+6
php
source share
6 answers

I recommend NEVER using the @ operator. It encourages bad code and can make your life miserable when something doesn't work and you're trying to debug it.

You can, of course, turn off notifications in php.ini, but if I were you, I would start using isset () more :)

You can do something like this:

 echo '<input type="text" name="whatever" value="', (isset($values['whatever'])?$values['whatever']:''), '" />'; 

You can learn more about how terrible @ is here: http://php.net/manual/en/language.operators.errorcontrol.php

Well, this is a real bug launch, which is expensive. Using @ causes an error. Check with isset () instead. http://seanmonstar.com/post/909029460/php-error-suppression-performance

+11
source share

To answer the headline : the best way is to disable display_errors .

What exactly are you using isset() ?

Note: For those who complain that this "disables" all errors - from the PHP manual :

This is a support function for your development and should never be used on production systems (such as systems connected to the Internet).

After editing:. You can either make sure that the variables you are going to use have some value (just blindly repeating get / post var is not best practice) - or you can use an object to store the variables you want to output (as properties) and use __get() to return an empty string (or false) when the property has not been set.

This will leave you with something like:

 echo $view->something; if($view->something){ //stuff to do when something is set } 

I believe this will be similar to what many template / presentation libraries do.

Update . He noted this old (ish) answer and thought that something could be added to it. For this use case (where values ​​may or may not be in an array), array_merge() can be used:

 $default = array('user' => false); $params = array_merge($default, $_GET); if($params['user']){ //safe to use, will have default value if not in $_GET } 
+7
source share

Why do you want to suppress something that won't happen if you check the array before accessing it?

 $something = array_key_exists('else', $_GET) ? $_GET['else'] : null; $something = isset($_GET['else']) ? $_GET['else'] : null; 

Your path seems to be just a solution for the lazy.

However, the @ -operator is never a good idea as long as you can avoid it (and there are really very few situations where you cannot avoid it). It is also quite inefficient.

+2
source share

Use a generic function to access the array:

 /** * Function for accessing array elements and returning a * default value if the element is not set or null. * @param string $key Name of index * @param array $array Reference to an array * @param string $default Value to return if the key is not * found in the array * @return mixed Value of array element (if it exists) or whatever * is passed for default. */ function element( $key, &$a, $default = '' ) { if( array_key_exists( $key, $a ) && !is_null( $a[$k] ) ) { return $a[$key]; } return $default; } 

Then your HTML output might look like this:

 echo '<input type="text" name="whatever" value="' , element( 'whatever', $values ), '" />' ; 
+2
source share

I already talked about this and am glad to say again:

 $else = isset($_GET['else']) ? $_GET['else'] : null; 

It is equivalent to:

 $else = @$_GET["else"]; 

The difference is that one of them is less readable due to the syntactic salt for suppressing errors, and the other uses the language function for this. Also note how in the second case the notification (many people do not understand the difference in errors) can still be detected by user-defined handlers.

For all practical purposes, you should not use either one or the other. Use simply:

 $else = $_GET["else"]; 

Turn off debugging notifications until you need them.

(In practice, I also usually use the dumb isset() method. But I use object-oriented superglobals, not $ _POST and $ _GET arrays in the style of PHP4, so it is just one hidden isset () and doesn’t pollute my code.)

+1
source share

This is a common problem, and although the solution is simple, you are right that this is not very. Personally, I like to use wrappers. You can easily write a simple class that implements ArrayAccess by wrapping the array passed to it. It will check the key inside (in one place) and calmly return null when requesting a nonexistent key. The result looks something like this:

'bar')); var_dump ($ arr ['Foo']); var_dump ($ arr ['Zin']); // string (3) "bar" // ZERO? >

It might also seem more familiar to you based on Javascript, as you can add new functions to the class, for example, custom sorting or possibly filtering.

0
source share

All Articles