Last active
August 17, 2016 21:04
-
-
Save ThierryA/f6ff25e66e9e93b7c893 to your computer and use it in GitHub Desktop.
PHP: simply get array values.
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 | |
/** | |
* Get value from $_GET or defined $haystack. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $needle Name of the searched key. | |
* @param string $haystack Optional. Associative array. If false, $_GET is set to be the $haystack. | |
* @param mixed $default Optional. Value returned if the searched key isn't found. | |
* | |
* @return string Value if found, $default otherwise. | |
*/ | |
function beans_get( $needle, $haystack = false, $default = null ) { | |
if ( $haystack === false ) { | |
$haystack = $_GET; | |
} | |
$haystack = (array) $haystack; | |
if ( isset( $haystack[$needle] ) ) { | |
return $haystack[$needle]; | |
} | |
return $default; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment