Uses curly braces in variables. Good practice in php

Some developers use curly braces in their PHP code, and some simply combine them.

For example, these 2 lines of code are fully valid in PHP

echo "<h1>{$row['title']}</h1>"; echo "<h1>" . $row['title'] . "</h1>"; 

The result of the code is the same at the end, but which is considered good coding practice?

Thanks J

+7
source share
7 answers

Using curly bracket syntax is a bit slower. Consider the following test:

 <?php $array = array('key'=>'val'); $start1 = microtime(TRUE); for ($i = 0; $i < 100000; $i++) { $str = "<tag>{$array['key']}</tag>"; } $end1 = microtime(TRUE); $result1 = $end1 - $start1; $start2 = microtime(TRUE); for ($j = 0; $j < 100000; $j++) { $str = "<tag>".$array['key']."</tag>"; } $end2 = microtime(TRUE); $result2 = $end2 - $start2; $start3 = microtime(TRUE); for ($k = 0; $k < 100000; $k++) { $str = '<tag>'.$array['key'].'</tag>'; } $end3 = microtime(TRUE); $result3 = $end3 - $start3; echo "1: $result1\n2: $result2\n3: $result3\n"; ?> 

On my PHP / 5.2.19-win32 system, the first test (with curly braces) is a bit slower (~ 7%). However, the difference is so small that you don’t have to worry, and I would say that you like it most.

A little opposite intuitively, the second test is consistently faster than the third (~ 2%) - double quotes are faster than single quotes - and I expected that it would be the other way around.

+12
source

It is useless to ask for best practice, since you did not write which style you prefer. If this helps you read your lines with braces, use them; they work. There are several ways to solve the problem, just to name a few:

 echo "<h1>{$row['title']}</h1>"; echo "<h1>" . $row['title'] . "</h1>"; echo "<h1>", $row['title'], "</h1>"; printf("<h1>%s</h1>", $row['title']); echo sprintf("<h1>%s</h1>", $row['title']); ?><h1><?php echo $row['title']; ?></h1><?php # thx, Jaime :) ... 

Choose what is readable for you. The best practices that you learn during class. Language is a tool to suit your needs.

+5
source

I always found echo '<h1>' . $row['title'] . '</h1>'; '<h1>' . $row['title'] . '</h1>'; easier to read.

+4
source

In fact, using single quotes for strings is more efficient in PHP.

So, I would say no, using curly braces around variables in a string is not good practice.

Using the following syntax is more efficient and readable (imho):

 echo '<h1>' . $row['title'] . '</h1>'; 
+4
source

I have never used braces around variables on my own. It seems to me that a very long time is being imposed on me!

+3
source

I don’t see anything wrong with that, unless you are avoiding custom HTML that you can forget to do anyway.

Some may say that single quotes are more effective, but all the tests that I see were done many years ago on this old equipment, which does not mean their reliability. Tests on current hardware show that single quotes in a 100,000 sample are only 7 milliseconds faster than curly braces, which cannot be an argument .

Use what you think is more readable, because developer time these days is much more expensive than computer time. Let the servers do their jobs.

+1
source

If you do not already have an agreed standard, I recommend using an existing one, such as Zend or Pear . For example, regarding your question, the Zend guide says the following:

Variable replacement is allowed using any of these forms:

 $greeting = "Hello $name, welcome back!"; $greeting = "Hello {$name}, welcome back!"; 
+1
source

All Articles