V Posted May 7, 2010 Share Posted May 7, 2010 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 https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/ Share on other sites More sharing options...
Alex Posted May 7, 2010 Share Posted May 7, 2010 In your specific example there's no point. Link to comment https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1054794 Share on other sites More sharing options...
Mchl Posted May 7, 2010 Share Posted May 7, 2010 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 https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1054795 Share on other sites More sharing options...
V Posted May 7, 2010 Author Share Posted May 7, 2010 Thanks for the help! @Alex, can you please give an example of a situation where mixed quoting is necessary? Link to comment https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1054797 Share on other sites More sharing options...
Mchl Posted May 7, 2010 Share Posted May 7, 2010 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 https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1054801 Share on other sites More sharing options...
gwolgamott Posted May 7, 2010 Share Posted May 7, 2010 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 https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1054804 Share on other sites More sharing options...
V Posted May 8, 2010 Author Share Posted May 8, 2010 Thank you! That was very helpful. Getting the hang of strings seems to take a while Link to comment https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1054922 Share on other sites More sharing options...
roopurt18 Posted May 8, 2010 Share Posted May 8, 2010 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 https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1054957 Share on other sites More sharing options...
Kryptix Posted May 8, 2010 Share Posted May 8, 2010 Someone tried telling me the other day that using ' was faster than ". Is there any truth in that what-so-ever? Link to comment https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1054965 Share on other sites More sharing options...
jskywalker Posted May 8, 2010 Share Posted May 8, 2010 if your code relies on ' being faster than ", than you are writing bad-code..... i dont think in 'normal' situations one can measure the difference reliably... Link to comment https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1054974 Share on other sites More sharing options...
roopurt18 Posted May 8, 2010 Share Posted May 8, 2010 Single-quoted strings are faster than double-quoted strings. The difference in speed is negligible. Link to comment https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1054983 Share on other sites More sharing options...
jskywalker Posted May 8, 2010 Share Posted May 8, 2010 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 https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1054985 Share on other sites More sharing options...
roopurt18 Posted May 8, 2010 Share Posted May 8, 2010 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 https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1054988 Share on other sites More sharing options...
jskywalker Posted May 8, 2010 Share Posted May 8, 2010 but its good to know that there's a difference in using single, or double quotes... Link to comment https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1054990 Share on other sites More sharing options...
ignace Posted May 8, 2010 Share Posted May 8, 2010 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 https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1054992 Share on other sites More sharing options...
Mchl Posted May 8, 2010 Share Posted May 8, 2010 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 https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1054993 Share on other sites More sharing options...
ignace Posted May 8, 2010 Share Posted May 8, 2010 (bron: http://stackoverflow.com/questions/482202/is-there-a-performance-benefit-single-quote-vs-double-quote-in-php) ^^ gevonden binnen 5 minuten googlen..... Niets vergeten? Link to comment https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1055013 Share on other sites More sharing options...
V Posted May 8, 2010 Author Share Posted May 8, 2010 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 https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1055031 Share on other sites More sharing options...
Daniel0 Posted May 8, 2010 Share Posted May 8, 2010 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 ? Link to comment https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1055042 Share on other sites More sharing options...
V Posted May 8, 2010 Author Share Posted May 8, 2010 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! I should be a string guru by tomorrow Link to comment https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1055044 Share on other sites More sharing options...
Mchl Posted May 8, 2010 Share Posted May 8, 2010 That's great because we have an opening in that position. Link to comment https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1055046 Share on other sites More sharing options...
V Posted May 8, 2010 Author Share Posted May 8, 2010 That's great because we have an opening in that position. ok but I may be overqualified Link to comment https://forums.phpfreaks.com/topic/201045-why-use-mixed-quoting/#findComment-1055128 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.