CodeIgniter Forums
When it comes to echoing or setting do you prefer ' or " - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: When it comes to echoing or setting do you prefer ' or " (/showthread.php?tid=5480)

Pages: 1 2


When it comes to echoing or setting do you prefer ' or " - El Forum - 01-23-2008

[eluser]Lone[/eluser]
As most of us on here are advanced php developers Im really curious to know what preferred way of echoing/setting variables/text people have. Do you prefer to use a single quote or double quote or a mixture of the two - and why?


Situation 1 - Echoing html/text

Personally, I use a mixture of the two when it comes to outputting html such as a repetitive table row eg.

Code:
function output_row($title,$value) {
  echo "<tr>\n";
  echo "\t".'<td class="title">'.$title.'</td>'."\n";
  echo "\t".'<td>'.$value.'</td>'."\n";
  echo "</tr>\n";
}

Sure its a lil bit more code, but if I use " then I have to use \ for setting tag variables like class and when there is a few it makes it hard to read. I hate unformatted html as well hence the \n and \t

The other alternative (which I use sometimes but fear the {} for some reason) I have seen is below:

Code:
function output_row($title,$value) {
  echo "<tr>\n";
  echo "\t<td class=\"title\">($title)</td>\n";
  echo "\t<td>{$value}</td>\n";
  echo "</tr>\n";
}
EDIT: () is meant to be {}


Situation 2 - Setting variables

I get into the habit now of setting variables using single quotes every now and again - but uncertain if there is a downside to this compared to "?

Code:
$text = 'some text';


As I said im more curious just to see what habits other people have formed and if there is any performance/memory/convention advantages or using one over the other?


When it comes to echoing or setting do you prefer ' or " - El Forum - 01-23-2008

[eluser]ekeretex[/eluser]
I use single quotes for strings and double quotes if I need to substitute variables (although I still have to use single quotes and join strings when I add html to mix due to the double strings in attributes).


When it comes to echoing or setting do you prefer ' or " - El Forum - 01-23-2008

[eluser]cinewbie81[/eluser]
Single quote for me anydays cause i hate to put the '\' in my code


When it comes to echoing or setting do you prefer ' or " - El Forum - 01-23-2008

[eluser]ejangi[/eluser]
This:
Code:
$string = 'Hello world';
$text = 'My text string: '.$string.', is '.count($string).' characters long';

is much faster than:
Code:
$string = "Hello world";
$count = count($string);
$text = "My text string: {$string}, is {$count} characters long";

for PHP to parse, regardless of how much you have to type.

[EDIT:] The performance gain of single quotes is somewhere between 150% and 200% compared to a double-quoted string containing variables, but in saying that the overall performance hit to your application will be almost unnoticeable, so in truth it's not big enough a issue to go a change all your current applications to use single quotes or anything...

I ALWAYS use single quotes, except on the very rare occasion that I need to add newlines etc... Even then, I'd prefer to have that kind of thing in a view where it belongs. Big Grin


When it comes to echoing or setting do you prefer ' or " - El Forum - 01-23-2008

[eluser]Craig A Rodway[/eluser]
Single quotes for strings where I don't need variable access, and double-quoted for strings where I need to access variables (but only with curly braces - this is quicker than just placing variable references in strings).

Code:
$name = 'Craig';
echo "Hello world, my name is {$name}!";



When it comes to echoing or setting do you prefer ' or " - El Forum - 01-23-2008

[eluser]Seppo[/eluser]
count($string) ? Tongue

I usually use single quotes, plus if I am echoing I don't use "." but ",", 'cause it's faster

Code:
$string = 'Hello world';
$text = 'My text string: ',$string,', is ',strlen($string),' characters long';



When it comes to echoing or setting do you prefer ' or " - El Forum - 01-23-2008

[eluser]Sarre[/eluser]
[quote author="Craig Rodway" date="1201107424"]Single quotes for strings where I don't need variable access, and double-quoted for strings where I need to access variables (but only with curly braces - this is quicker than just placing variable references in strings).

Code:
$name = 'Craig';
echo "Hello world, my name is {$name}!";
[/quote]
Erm? Why exactly with curly braces? I never use those...
Is there a big speed difference?


When it comes to echoing or setting do you prefer ' or " - El Forum - 01-23-2008

[eluser]ejangi[/eluser]
@Sarre - as far as I'm aware the curly brackets don't gain anything in terms of performance, but it's easier to spot variables when browsing code. A lot of Advanced PHP books recommend this technique for better maintainability.


When it comes to echoing or setting do you prefer ' or " - El Forum - 01-23-2008

[eluser]Craig A Rodway[/eluser]
Apparently not, but curly braces support arrays too. More here.


When it comes to echoing or setting do you prefer ' or " - El Forum - 01-23-2008

[eluser]Sarre[/eluser]
Doesn't that link show that curly braces work slower?
Double-quoted string, with variable, simple 4.737
Double-quoted string, with variable, curly 4.838