www.webdeveloper.com
Results 1 to 8 of 8

Thread: What does the variable actually contain?

  1. #1
    Join Date
    Sep 2008
    Posts
    95

    What does the variable actually contain?

    Hello,

    I have no idea why these scripts echo "ex" instead of "zero". Seems straight forward but it's got me stumped. Any ideas?

    PHP Code:
    $ws=0;
    echo 
    $ws.','.($ws=='x'?'ex':'zero');
    exit; 
    I would expect to see:
    0,zero

    But instead, it shows:
    0,ex

    Or...
    PHP Code:
    $ws=0;
    $ws=($ws=='x'?'ex':'zero');
    echo 
    $ws;
    exit; 
    I would expect it to echo "zero", but it echos "ex"

    Any ideas why this is? Incidentally, it doesn't matter what string I use (or even if I use $ws==0), it consistently displays "ex".

    I appreciate any ideas.

    Thanks.

  2. #2
    Join Date
    Sep 2008
    Posts
    95
    At the moment, I'm the only one posting in this forum anyways so I'm not trying to bump this thread...but I discovered something about my question above that might be of interest in helping to answer it...

    PHP Code:
    $ws=(int)0;
    $ws=($ws=='x'?'ex':'zero');
    echo 
    $ws;exit; 
    PHP Code:
    $ws=0;
    $ws=in_array($ws, array('x'))?'ex':'zero';
    echo 
    $ws;exit; 
    The above 2 code blocks result in "ex". but why??? very confused! I'm sure it's easy once explained.

    PHP Code:
    $ws=(string)0;
    $ws=($ws=='x'?'ex':'zero');
    echo 
    $ws;exit; 
    The above code displays "zero". Alright '0' does not equal 'x'.

    But why does (int)0 equal 'x' in the first script? I didn't think I'd have to type cast an integer when not using quotes and explicitly stating the value. So I'm confused as to the behavior of the first 4 code blocks I've posted here.

    Any ideas would be helpful because it's holding back some validation I'm trying to run in my scripts.

    Thank you for your help.

  3. #3
    Join Date
    Nov 2012
    Location
    Jakarta
    Posts
    42
    The code
    Code:
    ($ws=='x'?'ex':'zero')
    is a shorthand code.

    In the longer form, it is :
    Code:
    if ($ws == 'x') {
    $ws = 'ex'
    }
    else
    {
    $ws = 'zero'
    }

  4. #4
    Join Date
    Sep 2008
    Posts
    95
    Hello firesnaker,

    Thank you for your reply. I understand the shorthand.

    My question, though, was why does 0=='x' evaluate to TRUE?

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,611
    Because of PHP's loose typing, where it's casting 'x' to integer so that it can compare both expressions as the same type, and (int) 'x' will evaluate to 0. If you don't want that loose typing, then use the "===" (is identical to) operator, which checks for both same type and value, instead of the "==" operator.
    PHP Code:
    <?php
    $result 
    = (== 'x');
    var_dump($result);      // true
    echo "<br />\n";
    $result = (=== 'x');
    var_dump($result);      // false
    echo "<br />\n";
    $result = (=== '0');
    var_dump($result);      // false
    echo "<br />\n";
    $result = (== '1x');
    var_dump($result);      // false ('1x' will cast to 1)
    "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
    Sep 2008
    Posts
    95
    Thanks for the explanation, NogDog!

    Rhetorical questions:
    Why does it cast 'x' to an integer (resulting in true) instead of casting 0 to a string (resulting in false)? For example, ('x'=='y') evaluates to false. So it's not casting each of those to int.

    Actual question:
    If I have two numbers I am comparing, for example, and I don't know if they are floats, ints, etc., is it common practice to type cast them in the expression as in below?

    PHP Code:
    if((float)$a==(float)$b){
       
    //...

    The above code may not evaluate the same as
    PHP Code:
    if($a===$b){
       
    //...

    because who knows what $a or $b will be cast to?
    Last edited by speghettiCode; 01-12-2013 at 07:16 PM. Reason: Qualify the question

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,611
    For info on all the type-juggling possibilities, see http://php.net/manual/en/types.comparisons.php

    In your first example, anything that can be considered a zero (0, 0.0, "0", NULL, FALSE, "") will be cast to float as 0.0, so if $a is FALSE and $b is "", then they would both be cast to 0.0 and the comparison would be true -- which may or may not be what you want to happen. If you want to make sure you are comparing numbers, you might want to test each value with is_numeric() first, and if either test fails throw an exception or whatever other error-handling you want to do.

    Or you could insist that those variables be floats (or whatever), and test them accordingly with is_float(), etc; But I don't think there's a one size fits all solution, since many times loose typing and type-juggling works fine.

    If it becomes really critical, you could even create classes for specific types, which you could then pass into function parameters with type-hinting.
    PHP Code:
    <?php

    abstract class Type
    {
        private 
    $value;
        public function 
    __construct($value=null)
        {
            if(
    $value !== null) {
                
    $this->set($value);
            }
        }
        public abstract function 
    set($value);
        public  function 
    get()
        {
            return 
    $this->value;
        }
    }

    class 
    FloatType extends Type
    {
        public function 
    set($value)
        {
            if( ! 
    is_numeric($value)) {
                throw new 
    Exception ("'$value' is not numeric");
            }
            
    $this->value = (float)$value;
        }
    }

    function 
    foo(FloatType $bar)
    {
        
    $result $bar->value " is " . (is_float($bar->value) ? '' 'not ') . "a float";
        return 
    $result;
    }

    // actually do something now:
    $test 1.234;
    echo 
    foo(new FloatType($test));
    echo 
    "<br />\n";
    $test2 '12nope';
    echo 
    foo(new FloatType($test2)); // throws exception
    That's likely overkill for most applications, but something along that lines might be quite useful when you really need to keep your variable types in order.
    "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

  8. #8
    Join Date
    Sep 2008
    Posts
    95
    That link is now bookmarked! Thank you for the thorough explanation and class example. I really appreciate it.

Thread Information

Users Browsing this Thread

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

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