default variable value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lurisia

    default variable value

    Does php provide a built in way to use a variable's current value if
    that variable is already set but to use a default value otherwise.
    That is, a more compact syntax for:

    $some_var = isset("$some_va r") ? $some_var : "default value";

    Thanks,
    lurisia
  • Chung Leong

    #2
    Re: default variable value

    For class variables, yes. For function parameters, yes. In the global scope,
    no, as it makes little sense.

    If you're talking about GET and PUT variables, you can write a little
    function that does that. I use a function I call "StickyVariable " in a lot
    of situations where state values are required:

    function StickyVariable( $name, $default = null) {
    if(isset($_REQU EST[$name])) {
    $GLOBALS[$name] = $_SESSION[$name] = $_REQUEST[$name];
    }
    else if(isset($_SESS ION[$name])) {
    $GLOBALS[$name] = $_SESSION[$name];
    }
    else {
    $GLOBALS[$name] = $default;
    }
    }

    ....

    StickyVariable( "sort_type" , SORT_BY_DATE);
    StickyVariable( "sort_dir", SORT_ASCENING);

    Uzytkownik "lurisia" <hello@hello.co m> napisal w wiadomosci
    news:depq109j0u 14c02tdu52mdl5j eugjn8k8i@4ax.c om...[color=blue]
    > Does php provide a built in way to use a variable's current value if
    > that variable is already set but to use a default value otherwise.
    > That is, a more compact syntax for:
    >
    > $some_var = isset("$some_va r") ? $some_var : "default value";
    >
    > Thanks,
    > lurisia[/color]


    Comment

    Working...