please explain or refer: $string = isset($xyz) ? $xyz : "something else";

Discussion in 'Javascript' started by juglesh, May 10, 2005.

  1. juglesh

    juglesh Guest

    "$string = isset($xyz) ? $xyz : "something else";"

    Hello, someone gave code like this in another thread. I understand (by
    inference) what it does, but have not found any documentation on this
    type of syntax.

    Any one have links to this shortuct(?) syntax and other types of
    syntax?

    thanks
    j
     
    juglesh, May 10, 2005
    #1
    1. Advertising

  2. juglesh

    juglesh Guest

    ahh, sorry, wrong group

    all apologies
    j
     
    juglesh, May 10, 2005
    #2
    1. Advertising

  3. Re: please explain or refer: $string = isset($xyz) ? $xyz :"something else";

    "juglesh" <> writes:

    > "$string = isset($xyz) ? $xyz : "something else";"
    >
    > Hello, someone gave code like this in another thread. I understand (by
    > inference) what it does, but have not found any documentation on this
    > type of syntax.
    >
    > Any one have links to this shortuct(?) syntax and other types of
    > syntax?


    Well, the definitive guide would be ECMA 262, 3rd edition:
    <URL:http://www.ecma-international.org/publications/standards/Ecma-262.htm>

    For less ... detailed ... descriptions, you can use:
    JScript:
    <URL:http://msdn.microsoft.com/library/en-us/script56/html/js56jslrfjscriptlanguagereference.asp>
    JavaScript 1.1:
    <URL:http://wp.netscape.com/eng/mozilla/3.0/handbook/javascript/>
    JavaScript 1.5:
    <URL:http://www.croczilla.com/~alex/reference/javascript_ref/>

    In all these, the "...?...:..." syntax is described under "operators"
    (beacuse that is what it is :)

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, May 10, 2005
    #3
  4. Re: please explain or refer: $string = isset($xyz) ? $xyz : "somethingelse";

    juglesh wrote:

    > "$string = isset($xyz) ? $xyz : "something else";"
    >
    > Hello, someone gave code like this in another thread. I understand (by
    > inference) what it does, but have not found any documentation on this
    > type of syntax.
    >
    > Any one have links to this shortuct(?) syntax and other types of
    > syntax?
    >
    > thanks
    > j
    >


    javascript version of PHP's isset would be:

    typeof(xyz)!='undefined'

    Therefore:

    string = typeof(xyz)!='undefined' ? xyz : 'something else';

    --
    Justin Koivisto -
    http://koivi.com
     
    Justin Koivisto, May 10, 2005
    #4
  5. Justin Koivisto wrote:
    <snip>
    > javascript version of PHP's isset would be:
    >
    > typeof(xyz)!='undefined'
    >
    > Therefore:
    >
    > string = typeof(xyz)!='undefined' ? xyz : 'something else';


    Except that in javascript - typeof - is an operator so its operand does
    not need to be parenthesised. Though doing so is harmless, it is just
    potentially misleading as to the nature of - typeof - because the
    results resemble a function call.

    Richard.
     
    Richard Cornford, May 10, 2005
    #5
  6. Justin Koivisto wrote:

    > javascript version of PHP's isset would be:
    >
    > typeof(xyz)!='undefined'


    No, because PHP's isset()

    ,-<http://php.net/isset>
    | Returns TRUE if var exists; FALSE otherwise.

    "Exists" has to be read as "has been defined, its value is not NULL and
    unset() was not applied on it.":

    ,-<ibid.>
    | If a variable has been unset with unset(), it will no longer be set.
    | isset() will return FALSE if testing a variable that has been set to
    | NULL.

    But in JS, there are fundamental differences:

    1. An already instantiated variable may have/become the
    value of `undefined':

    var x;
    alert(typeof x); // `undefined'
    alert(x); // `undefined' or nothing

    var y = y;
    alert(typeof y); // `undefined'
    alert(y); // `undefined'

    2. An already instantiated variable may be undefined by
    having the `delete' operator applied on it, provided
    that it was not declared (`var' keyword was not used):

    z = 2;
    alert(typeof z); // `number'
    delete z;
    alert(typeof z); // `undefined'
    alert(z); // ReferenceError

    3. A named function argument may be not supplied. In
    that case, its value is, by definition :), `undefined':

    function foo(a, b)
    {
    alert([typeof a, typeof b]); // `number,undefined'
    }
    foo(42);

    4. Although `null' in JS is a special value of the internal Null type,

    ,-<ECMAScript 3>
    | 4.3.11 Null Value
    | The null value is a primitive value that represents
    | the null, empty, or non-existent reference.
    |
    | 4.3.12 Null Type
    | The type Null has exactly one value, called null.

    which it has in common with PHP (where NULL is the sole value of
    the NULL type), it yields

    var x = null;
    alert(typeof x); // `object'

    This is most certainly an attribution to the fact that a reference
    always refers to an object or not.


    Conclusion:

    Besides risking a ReferenceError (and hopefully catching it with exception
    handling), there is no way in JS to determine whether a variable/property
    was defined or not; it is only possible to determine whether it yields
    `undefined' (the sole value of the internal Undefined type) or `null', or
    not.


    PointedEars
    --
    When the power of love overcomes the love
    of power, the world will know peace.
    -- Jimi Hendrix
     
    Thomas 'PointedEars' Lahn, May 15, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christof Krueger
    Replies:
    1
    Views:
    621
    Christof Krueger
    Dec 19, 2003
  2. Guest
    Replies:
    4
    Views:
    514
    Guest
    Oct 13, 2004
  3. Olivier Noblanc ATOUSOFT

    find isset() php function equivalent in python

    Olivier Noblanc ATOUSOFT, Feb 1, 2005, in forum: Python
    Replies:
    3
    Views:
    909
    Ola Natvig
    Feb 1, 2005
  4. Patrick Fitzsimmons

    Does a function like isset() exist in Python?

    Patrick Fitzsimmons, Jun 23, 2005, in forum: Python
    Replies:
    6
    Views:
    1,334
    Roy Smith
    Jun 23, 2005
  5. Replies:
    4
    Views:
    245
    Tad McClellan
    Jun 1, 2007
Loading...

Share This Page