Monday, 12 January 2015

PHP Concatenation – Comma (,) vs Dot (.)

Comma isn’t a concatenator. it use for printing or rather echoing a list of variable, string and numbers similar to how it is used in English. While dot joins two strings together to make one longer string.
Take note: comma only work when using the echo language construct and you can’t return a comma delimited variable.
Examples
Let’s see some code example.
  • ?
    1
    2
    3
    4
    <?php
    $a = 'i am';
    $b = ' boy';
    echo $a, $b;
    ?
    1
    2
    3
    4
    5
    <?php
    $a = 'i am';
    $b = ' boy';
    $c = $a . $b;
    echo $c;
    ?
    1
    2
    3
    4
    <?php
    $a = 'i am';
    $b = ' boy';
    echo $a . $b;
    Result: i am boy
  • ?
    1
    2
    3
    4
    <?php
    $a = 'i am';
    $b = ' boy';
    print($a, $b);
    ?
    1
    2
    3
    4
    <?php
    $a = 'i am';
    $b = ' boy';
    $c = $a, $b;
    ?
    1
    2
    3
    4
    <?php
    $a = 'i am';
    $b = ' boy';
    return $a , $b;
    Result: Parse error: syntax error, unexpected ‘,’ in C:\xampp\htdocs\index.php on line 4
It is worth noting that when using echo, delimiting string with comma is faster than dot.

0 comments:

Post a Comment