views:

100

answers:

4

This might be a silly question but it struck me, and here i ask.

<?php
  $x="Hi";
  $y=" There";
  $z = $x.$y;
  $a = "$x$y";
  echo "$z"."<br />"."$a";
?>

$z uses the traditional concatenation operator provided by php and concatenates, conversely $a doesn't,

My questions:

  1. by not using the concatenation operator, does it effect the performance?

  2. If it doesn't why at all have the concatenation operator.

  3. Why have 2 modes of implementation when one does the work?

+3  A: 
  1. Only slightly, since PHP has to parse the entire string looking for variables, while with concatenation, it just slaps the two variables together. So there's a tiny performance hit, but it's not noticeable for most things.

  2. It's a lot easier to concatenate variables like $_SERVER['DOCUMENT_ROOT'] using the concatenation operator (with quotes, you have to surround the variable in brackets or remove the single quotes in the array index; plus it just makes the string look all ugly). Plus, the concatenation operator allows more flexibility for formatting. For example, you can break up a long string literal onto multiple lines and then concatenate the different parts of it:

    $blah = "This is a really really long string. I don't even know how " .
        "long it is, but it's really long. Like, longer than an eel " .
        "or even a boa constrictor. Wow.";
    

    You can also use the concatenation operator to directly include return values from functions in a string literal (you can't include a function call in a double-quoted string), like this:

    $blah = "This has a " . fn_call() . " result, which can't go in the quotes.";
    
  3. I'm not sure I entirely understand what you're asking here, but I can say that PHP borrows a lot of things from Perl, and one of Perl's mantras is "There's more than one way to do it."

htw
While the multiple lines thing is true, you can also just use the newline and it will work fine. For example if you knocked the `" . \n "` off your first line, it will still work (however with double quotes if you explicitly put a `\n` it will be interpreted as newline).
alex
+1 just for use of eels and snakes in PHP. Snakes are usually only ever found in the Python language.
Pyronaut
You **can** include array values in strings without brackets like this: `"$_SERVER[DOCUMENT_ROOT]"`. Only works for one-dimensional array indexes though.
deceze
@alex: The only problem with that is the indentation is actually included in the string. If all you're doing is echoing it to HTML, then that's fine, but it might cause some unexpected results in other cases…@deceze: Good point, I'll make an edit to reflect that.@Pyronaut: Hah, and what's even better is that boas and pythons are closely related, even though I wasn't even aware of that when I wrote that example string.
htw
+1  A: 

In some cases your write less with:

$someLongVarName ="Hi";
$someLongVarName .=" there";

VS

$someLongVarName ="Hi";
$someLongVarName = "$someLongVarName there";
Babiker
+2  A: 

a. Yes. PHP has to parse the string for variables.

b. Because of lines like: echo 'Your Ip address is' . get_ip() . '.';

For reasons A and B.

Aaron Harun
A: 

Addressing your last question:

Every language has multiple was of doing the same thing. Flexibility is important in every language since any given method may be better the another from situation to situation. The only thing that you should worry about in regards to this is to be consistent in your own code.

Justin Johnson