SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist Fergal's Avatar
    Join Date
    Nov 2003
    Location
    Ireland
    Posts
    500
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Concatenation Question?

    Code:
    $lo = 2;
    $high = 7;
    echo $lo < $high;
    The above code outputs 1.

    However,
    Code:
    echo $lo < $high . " string";
    doesn't concatenate " string" to that 1. Why is that?

    I know there is a better way of doing all of the above, but I'm just trying to understand the concatenation issue.

    Thanks!
    Fergal Crawley (Previous Username: Proudirish.com)
    Business Advice Forum - Webmaster and Business Forum
    < Get a free link & win $5,000
    Forum Coin New World Currency

  2. #2
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Why are you trying to complicate simple thing?

    I'm not sure about this codes below, but try,
    Code:
        echo {$lo < $high}. " string";
    Quality codes are optimized and tested...
    Click here for inspiration..

  3. #3
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    echo (int)( $lo $high ) . " string";
    Be nice to nerds. Chances are you'll end up working for one - Bill Gates
    > photos | admin panel

  4. #4
    SitePoint Member
    Join Date
    Jul 2013
    Location
    Brazil
    Posts
    9
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    i think this happen because, php does the operation first and cast the the 'answer' to a number '1' is true, '0' is false.

    http://www.php.net/manual/en/languag...precedence.php


    A word of caution - the dot operator has the same precedence as + and -, which can yield unexpected results.

    Example:

    <php
    $var = 3;

    echo "Result: " . $var + 3;
    ?>

    The above will print out "3" instead of "Result: 6", since first the string "Result3" is created and this is then added to 3 yielding 3, non-empty non-numeric strings being converted to 0.

    To print "Result: 6", use parantheses to alter precedence:

    <php
    $var = 3;

    echo "Result: " . ($var + 3);

    http://www.php.net/manual/en/languag...ors.string.php

  5. #5
    SitePoint Evangelist Fergal's Avatar
    Join Date
    Nov 2003
    Location
    Ireland
    Posts
    500
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone for your replies and help. Your feedback has helped me to clarify what is happening and why it is not working.
    Fergal Crawley (Previous Username: Proudirish.com)
    Business Advice Forum - Webmaster and Business Forum
    < Get a free link & win $5,000
    Forum Coin New World Currency


Bookmarks

Posting Permissions

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