pnj Posted August 4, 2007 Share Posted August 4, 2007 I often find myself writing this to avoid a php warning: $x = (isset ($_GET['foo']) ? $_GET['foo'] : null); Because php triggers a warning on the following if foo is not in the http get: $x = $_GET['foo']; I'd like to put it in a function, something like function getval($val) { return (isset ($val) ? $val : null; } But of course this doesn't because the call to getval() triggers the warning. I could use eval() and pass a string, but that also seems inelegant. In C, I would use a #define macro. Is there any such thing in php? thanks Link to comment https://forums.phpfreaks.com/topic/63336-php-macros/ Share on other sites More sharing options...
dbo Posted August 4, 2007 Share Posted August 4, 2007 Is this what you want? function get_val($val) { if( isset($_GET[$val]) ) return $_GET[$val]; return ""; } Link to comment https://forums.phpfreaks.com/topic/63336-php-macros/#findComment-315669 Share on other sites More sharing options...
pnj Posted August 5, 2007 Author Share Posted August 5, 2007 fair enough, this is a good first step. but i was hoping for something more generic, that would work for a get, a post, or even a variable defined or not defined by myself. basically, a C-style macro in php, which from the silence on this post, I'm guessing doesn't exist... -pnj Link to comment https://forums.phpfreaks.com/topic/63336-php-macros/#findComment-315879 Share on other sites More sharing options...
dbo Posted August 5, 2007 Share Posted August 5, 2007 Well this is a step closer... function get_it($type, $val) { if( $type == "get" ) { if( isset($_GET[$val]) ) return $_GET[$val]; return ""; } else if( $type == "post" ) { if( isset($_POST[$val]) ) return $_POST[$val]; return ""; } else if( $type == "session" ) { if( isset($_SESSION[$val]) ) return $_SESSION[$val]; return ""; } else if( $type == "globals" ) { if( isset($GLOBALS[$val]) ) return $GLOBALS[$val]; return ""; } return ""; } to call it you'd just do: $var = get_it("post", "username"); Link to comment https://forums.phpfreaks.com/topic/63336-php-macros/#findComment-315884 Share on other sites More sharing options...
lightningstrike Posted August 5, 2007 Share Posted August 5, 2007 function get_val($val){ GLOBAL $$val; return (isset($$val)) ? $$val : ""; } $var = get_val("_POST['varname']"); perhaps this? (untested); Link to comment https://forums.phpfreaks.com/topic/63336-php-macros/#findComment-315887 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.