0

I am learning php for the first time. In the below code, I have seen echo as in method 2 is more common in various code snippets. But method 1 also prints the same correct answer. My question is why should I use method 2.

<?php 
    
    $company[0]= " tweer" ;   
     
    $regtype[0]= "Limited";
     
    echo  "$company[0]  $regtype[0]<br />"; // method 1
     
    echo $company[0]." ".$regtype[0]."<br />"; //method 2
?>

Output:

tweer Limited
tweer Limited

I understand . is used for concatenation but I can get the same result without it as well.

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
gpuguy
  • 4,607
  • 17
  • 67
  • 125
  • You can choose the either way, it’s just different ways to do the same. – OMi Shah Jun 20 '21 at 05:52
  • I prefer method 1. That's called "string interpolation". About 1000 years ago the zend guidelines recommended method 2 in all situations I think. It depends on your preference and the conventions used in your code base. If you are calling global functions or referencing constants you'll need method 2 but if you are only using variables and instance properties, use method 1. – chiliNUT Jun 20 '21 at 05:54
  • Method 1 will only work with double quotes so some linters will try to refactor your code with single quotes and you won't know why your code suddenly won't work. – Mojo Allmighty Jun 20 '21 at 05:56
  • 1
    This is very similar to https://stackoverflow.com/questions/4676417/should-i-use-curly-brackets-or-concatenate-variables-within-strings – Nigel Ren Jun 20 '21 at 06:13

1 Answers1

1

There tends to be multiple ways to achieve the same outcome when coding. Version 2, using the concatenation operator ('.') Is best [ref a]

echo $company[0]." ".$regtype[0]."<br />"; 

Rationale:

  1. Using the concatenation operator makes the intentention clear and code more expressive.

  2. Version 1 relies on PHPs string interpolation [B]. Many other languages do not have an equivalent. So using the concatenation makes it easier to transfer between languages. It also makes it easier for non PHP programmers to read the code.

  3. Version 2 exposes the variables in a more transparent way to string functions like trim [ref C]

  4. String interpolation only works for heredocs syntax and double quotes [ref D]. This is easy to forget and cause syntax errors.

Note:

On a side note. I have labelled your examples versions rather than methods to stop any confusion with the term method in OOP [ref E] and also as synonym for function (see Ref F]

References

A. https://www.php.net/manual/en/language.operators.string.php

B. https://phppot.com/php/variable-interpolation-in-php/

C. https://www.php.net/manual/en/ref.strings.php

D. https://phppot.com/php/variable-interpolation-in-php/. Also mentioned in comments to the question by @mojo allmighty

E. https://en.m.wikipedia.org/wiki/Method_(computer_programming)#:~:text=A%20method%20in%20object%2Doriented,any%20of%20its%20various%20consumers.

F. https://www.php.net/manual/en/functions.user-defined.php

Lew Perren
  • 1,209
  • 1
  • 10
  • 14