Jump to content

Why use mixed quoting?


V

Recommended Posts

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?

Link to comment
Share on other sites

It allows for longer MTBF for Shift keys.

 

No real reason, just a inconsistence of whoever did that.

(Or maybe they read, that single quotes are faster than double...)

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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....

Link to comment
Share on other sites

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!)

Link to comment
Share on other sites

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.....

 

Link to comment
Share on other sites

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.

This is where I'd disagree.  The performance hit is negligible.

Link to comment
Share on other sites

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"]

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

http://php.net/string ?

 

I overlooked that one. Thanks! :shy: I should be a string guru by tomorrow :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.