Created
October 4, 2014 20:33
-
-
Save oropesa/d4c051781c3a58a61b78 to your computer and use it in GitHub Desktop.
Check if the key of the array is valid and return it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function isset_get( $array, $key, $default = null ) { | |
return isset( $array[ $key ] ) ? $array[ $key ] : $default; | |
} | |
/* | |
* In Wordpress, the most common PHP warning is when it uses an array index without check if that index is valid. | |
* So, you have to ask if the array index is valid: | |
* if( isset( $array['index'] ) ) | |
* And then you can use it: | |
* $value = $array['index'] | |
* | |
* With this function you can check and get the value of the array and custom the default value | |
* $value = isset_get( $array, 'index' ); | |
* $value = isset_get( $array, 'index', false ); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment