Why a full stop, “.” and not a plus symbol, “+”, for string concatenation in PHP?

前端 未结 12 1604
暖寄归人
暖寄归人 2020-12-09 01:54

Why did the designers of PHP decide to use a full stop / period / \".\" as the string concatenation operator rather than the more usual plus symbol \"+\" ?

Is there

相关标签:
12条回答
  • 2020-12-09 02:04

    This doesn't answer the question, just wanted to share something.

    From PHP Manual: String Operators, someone posted this which I find rather interesting. Notice how the space plays a part in the output.

    Excerpt:
    If you attempt to add numbers with a concatenation operator, your result will be the result of those numbers as strings.

    <?php
    
    echo "thr"."ee";           //prints the string "three"
    echo "twe" . "lve";        //prints the string "twelve"
    echo 1 . 2;                //prints the string "12"
    echo 1.2;                  //prints the number 1.2
    echo 1+2;                  //prints the number 3
    
    ?>
    
    0 讨论(0)
  • 2020-12-09 02:10

    I am not a PHP expert, but, how else do you do differentiate that last two lines?

    $first  = 100;
    $second = 20;
    $stringresult     = $first . $second; // "10020"
    $arithmeticresult = $first + $second; // 120
    
    0 讨论(0)
  • 2020-12-09 02:11

    Here is a bit of historical context.

    • The PHP language started out as a set of Perl scripts.
    • As such, PHP gets most of it's syntax from Perl.
    • Perl, and by extension PHP, has untyped variables.

           "5"  ==   5
      "5" + 5   ==  10
      "5" . 5   ==  55
      
    • To be able to tell the difference between addition and concatenation, they had to be two different operators.
    • Perl copied the method access operator from C ->.
    • This was before many of the more modern programming languages started to use . for method access.
    • Concatenation is one of the more common operations, and should use fewer characters. According to Huffman coding.
    • . was one of the few characters available for this use. The only other one that would make sense to use is ~, which is probably why that is now the Perl 6 concatenation operator.
    0 讨论(0)
  • 2020-12-09 02:12

    PHP's syntax is influenced by Perl, and . is the string concatenation operator in Perl.

    In a weakly typed language there are advantages to having a different string concatenation and numeric addition operators: which one you use will influence which type the language coerces the variables to.

    As it happens, Perl 6 will use a tilde ~ instead of a dot . for string concatenation, because . will be used for object member access. So it seems the designers of Perl now think it was a bad choice.

    Perhaps, in Perl and PHP's early, non-Object-Oriented days, it seemed like as good a choice as any. Maybe the designers of both languages never envisaged them becoming strong OO languages.

    As for whether PHP will one day ditch its -> member access syntax for ., who knows?

    0 讨论(0)
  • 2020-12-09 02:13

    I guess it is so you can concatenate numbers with strings?

    $i=100;
    $str="hello";
    $str2 = $str.$i
    

    Since you don't declare variable types, with a + it could give a result of 100 instead of "hello100."

    0 讨论(0)
  • 2020-12-09 02:14

    Douglas Crockford thinks that + for Concatenation is a Bad Idea:

    JavaScript has its share of design errors, such as the overloading of + to mean both addition and concatenation with type coercion

    0 讨论(0)
提交回复
热议问题