Jump to content

Why use mixed quoting?


V

Recommended Posts

There's no such situation, when it's necessary. There are however cases, when it's convenient.

 

$var = "<div style='$foo'>".'The style of this div is stored in $foo variable</div>';

 

A lousy example, but couldn't find anything better ;) (Which in itself means something)

There is not real reason in that particular example you gave & not really useful or matter one way or another.

However you could use it for something semi-useful such as embedding code within code.. for example something like this: (Of course this snippet code be done some other way, but for demonstrations purposes...)

 

    	echo(' <td><a class="four" href="'.$path.'/'.$file.'" target="_blank">'.$form_num.'</a></td> ');
//will print out the html for something like this:   
//<td><a class="four" href="inner/form_4.01.pdf" target="_blank"> Form Number 4.01</a></td>

 

This is convenient if you are using a format for your html you want to keep consistent or vice-versa for your php.... Just a tool that's nice to have but not necessary, like that power-drill you use to hang pictures....

Thank you! That was very helpful. Getting the hang of strings seems to take a while :)

Coming from a C background and knowing that you really mean strings in PHP, that's a pretty funny statement!

 

(I'm not poking fun at you; I'm just saying if you really know what was happening under the hood, then you'd be really, really afraid of strings!)

Yes. It is slightly faster to use single quotes.

 

This is because when you use double quotes PHP has to parse to check if there are variables in there.

 

So, if you do:

 

$bar = 'world';

$foo = "hello $bar";

$baz = 'hello $bar';

$foo would contain "hello world" while $baz could contain "hello $bar"

 

Whenever you are not using variables inside a string it's good practice to just use single quotes so PHP doesn't have to parse the string.

 

(bron: http://stackoverflow.com/questions/482202/is-there-a-performance-benefit-single-quote-vs-double-quote-in-php)

^^ gevonden binnen 5 minuten googlen.....

 

I'm still trying to figure out string concatenation. I see some codes that use mixed quoting for example,

 

 $str_2 = 'This '."is a".' test message';

 

Is there any point to that?

 

" (double quotes) are able to parse $variables and newlines ' (single quotes) don't. Personally I use double quotes to notify the reader that it contains $variables or newlines otherwise single quotes, I find it more readable (and prettier, if that makes any sense in programming).

 

$_SERVER['REMOTE_ADDR']
$_SERVER["REMOTE_ADDR"]

Microoptimisations like using single quotes instead of double, seldom result in any significant performance improvement. Especially weighted against time needed to implement them. Concentrate on what is REALLY slowing down your application, because that's where you're most likely to find most improvement.

 

 

See this blog post, for some examples:

http://www.brandonsavage.net/micro-optimizations-that-dont-matter/

Thank you! That was very helpful. Getting the hang of strings seems to take a while :)

Coming from a C background and knowing that you really mean strings in PHP, that's a pretty funny statement!

 

(I'm not poking fun at you; I'm just saying if you really know what was happening under the hood, then you'd be really, really afraid of strings!)

 

lol, I'm foreign to the terms. PHP really seems like applying math formulas. It's so gratifying when I create my own little function and it works. :)

 

BTW, does anyone know a really good tutorial on php strings? I googled some but couldn't find any that explain the whole concept in more detail and provides a lot of examples.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.