Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Operators and "dots"

  1. #1
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Operators and "dots"

    Hello all ,

    As some of you most likely know I am a complete begginer to PHP.

    I need some help understanding how operators and the dots for the echo works.

    Here is an example of how I have read of doing it -

    <?php
    $txt1 = "Hello";
    $txt2 = "Fool";
    echo $txt1." ".$txt2;
    ?>

    But I read about operators in JavaScript and completely understand it, can you replace the dots above with + like this -

    <?php
    $txt1 = "Hello";
    $txt2 = "Fool";
    echo $txt1 + " " + $txt2;
    ?>

    Thanks !

  2. #2
    Join Date
    Feb 2007
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    No, that won't work. The dot in php (when dealing with strings) does the same thing as the plus in JavaScript.
    "Rock and roll ain't noise pollution." - AC/DC

    http://www.blake-foster.com

  3. #3
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    No, the + operator in PHP would try to parse the character as an operator not an assigment to a string.
    It's like saying (mathematically)
    Hello + Fool = ?

    That's where PHP messes up, so fortunately they have a way around it . The genius' at PHP decided to use a different character, and as Blake said, the "." sign.
    - Mike

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No. Javascript overloads the + operator to mean both addition (of numbers) and concatenation (of strings). However, PHP has two separate operators, + for addition and . for concatenation. The two are not interchangeable.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    No. Javascript overloads the + operator to mean both addition (of numbers) and concatenation (of strings).
    Which I think is more confusing in the long run, and less effecient. Having to use String() all the time to convert numbers to strings is just a nussiance (spelling?).
    - Mike

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Hmm? One very rarely has to convert numbers to strings in JS, and certainly not for the purposes of concatenation.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    In a script I used a while back I used setAttribute, and IE didn't like numbers in the function:

    setAttribute("src",num+".gif");

    so I did

    setAttribute("src",String(num)+".gif";
    - Mike

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That should make no difference at all... num should be type-coerced into a string.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Well, it didn't work before String() and did after.
    - Mike

  10. #10
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    LOL operator....operator, here is my order ,

    But anyway mburt here is an example of + in JavaScript -

    <script type="text/javascript">
    var txt1 = "Hello"
    var txt2 = 123
    document.write(txt1+" "+txt2)
    </script>

    Anyway i once read that the default language for php in vbscript and i also read that javascript can replace that - please tell me how
    (i may be mistaking this with another server language)

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
  •