Skip to content

Instantly share code, notes, and snippets.

@oropesa
Created October 4, 2014 20:33
Show Gist options
  • Save oropesa/d4c051781c3a58a61b78 to your computer and use it in GitHub Desktop.
Save oropesa/d4c051781c3a58a61b78 to your computer and use it in GitHub Desktop.
Check if the key of the array is valid and return it.
<?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