www.webdeveloper.com
Results 1 to 10 of 10

Thread: [RESOLVED] PHP version of "undefined || literal"

  1. #1
    Join Date
    Dec 2008
    Posts
    488

    resolved [RESOLVED] PHP version of "undefined || literal"

    In JavaScript, when I'm not sure if a variable is defined, I can say
    Code:
    alert(myVar || "Something else")
    If the variable is undefined, it will obviously return "Something else".

    In PHP, I have been using isset() and the ternary operator (?
    PHP Code:
    echo isset($myVar) ? myVar 'Something else'
    This seems kind of redundant, and I was hoping there was something similar in PHP.

  2. #2
    Join Date
    Dec 2008
    Posts
    488
    I guess maybe what I should be looking at is how to declare undefined in PHP. I forgot that I usually insert "var undefined;" in my JS template to make this work. I guess I need to do the same in PHP... but declaring variables is a little different.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,226
    Offhand I don't know of a terser way than the tenary operator for this sort of thing.

    As far as "defining" a variable as undefined, you can set it to null, which isset() will treat as not set.
    PHP Code:
    var_dump(isset($var)); // false
    $var null;
    var_dump(isset($var)); // false
    $var false;
    var_dump(isset($var)); // true 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Dec 2008
    Posts
    488
    Well, I'm not looking to define a variable as undefined, I'm trying to set undefined to null by declaring it.

    In JavaScript,
    Code:
    var undefined;
    takes a reserved word, undefined, and defines it, so that whenever a variable is undefined and gets called, it will return "undefined" instead of throwing an undefined variable error. This way, you can use the "||" operator instead of using the whole typeof() thing. It's not a strict standards compliant method, but it works and makes for easier error handling. Even jQuery uses it.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,226
    Nope, nothing like that in PHP that I've ever seen.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Nov 2008
    Posts
    2,477
    what happens when myVar is false? Or some other correct value which evaluates false?

  7. #7
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    The PHP's Boolean operators always return a Boolean value... as opposed to other languages that return the value of the last evaluated expression.

    But you may do something tricky like:

    PHP Code:
    $echo=$myVar or $echo="something else";
    echo 
    "$echo"
    It will work with a notable exception: when $myVar=0. In this case the assignment $echo=$myVar is evaluated as Boolean false.

    Or you may simplify the ternary operator like this:
    PHP Code:
    echo ($myVar?$myVar:"something else"); 
    But, again, $myVar can have any value except 0, because 0 is evaluated as Boolean false.

  8. #8
    Join Date
    Nov 2008
    Posts
    2,477
    Quote Originally Posted by Kor View Post
    But, again, $myVar can have any value except 0...
    or "", or "0", or an empty array, or null etc. This technique seems dodgy enough in javascript! Just use isset() to see if a variable has been defined.

  9. #9
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Mindzai View Post
    or "", or "0", or an empty array, or null etc. This technique seems dodgy enough in javascript! Just use isset() to see if a variable has been defined.
    I agree. So, jamesbcox1980, the answer is no. As I said, php always return a Boolean value on Boolean operators.

  10. #10
    Join Date
    Dec 2008
    Posts
    488
    Ok, thanks guys

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles