Jump to content

Recommended Posts

 

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.