hostfreak Posted October 12, 2006 Share Posted October 12, 2006 I am curious, a lot of times I see something like (just an example):[code] $display = <<<HTML <form name="login" method="post" action="{$_SERVER['PHP_SELF']}?action={$_GET['action']}"> <table id="login_table"> <tr> <td>Test:</td> <td><input type="text" name="username" value="{$_POST['test']}"></td> </tr> <tr> <td></td> <td align="center"><input type="submit" name="submit" value="Submit"></td> </tr> </table> </form>HTML; echo $display;[/code]The {} around the code is what I am curious about. From my own testing, I think it more less "echo's" the code? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/23741-curly-bracket-question/ Share on other sites More sharing options...
HuggieBear Posted October 12, 2006 Share Posted October 12, 2006 Hostfreak, The curly braces are used for variable parsing...When a string is enclosed in either double quotes or within the [url=http://uk.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc]heredoc syntax[/url], the variables are parsed within it.The curly braces are used in 'complex' parsing of variables. The link above will take you to the correct page in the manual, then scroll down slightly until you see the title 'Variable Parsing'.It's an excellent read :DRegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23741-curly-bracket-question/#findComment-107828 Share on other sites More sharing options...
hostfreak Posted October 12, 2006 Author Share Posted October 12, 2006 Ah, thanks HuggieBear. I wanted to check out the manual before I started the thread, but I wasn't sure what to refer to it as. Thanks man. Quote Link to comment https://forums.phpfreaks.com/topic/23741-curly-bracket-question/#findComment-107841 Share on other sites More sharing options...
HuggieBear Posted October 12, 2006 Share Posted October 12, 2006 No problem, when I first looked I couldn't find it either so I asked here too... See [url=http://www.phpfreaks.com/forums/index.php/topic,108074.msg434411.html#msg434411]this post[/url].Always a good idea to search the forums first too :)Huggie Quote Link to comment https://forums.phpfreaks.com/topic/23741-curly-bracket-question/#findComment-107843 Share on other sites More sharing options...
Daniel0 Posted October 12, 2006 Share Posted October 12, 2006 I always put them in curly brackets. I think that [code]"Bla bla {$variable} bla bla again"[/code] looks nicer than [code]"Bla bla $variable bla bla again"[/code]It also makes it easier to spot variables. Quote Link to comment https://forums.phpfreaks.com/topic/23741-curly-bracket-question/#findComment-107848 Share on other sites More sharing options...
hostfreak Posted October 12, 2006 Author Share Posted October 12, 2006 I really do need to start using the search on the forums. Reading that was almost like Deja vu heh. Once again thanks man.edit- Yeah, I agree with you Daniel. It does look nicer. Now that I know what they are for, I'll probably end up doing them that way. Quote Link to comment https://forums.phpfreaks.com/topic/23741-curly-bracket-question/#findComment-107850 Share on other sites More sharing options...
redbullmarky Posted October 12, 2006 Share Posted October 12, 2006 it is worth noting that (depending on the situation) it's actually better not to include variables inside strings in this way. if you do, the way daniel suggested is better - as he stated, it's easier to read.so whilst this is valid (and i use it from time to time):[code]<?php$animal = "cat";$mystring = "the $animal sat on the mat";$mystring = "the {$animal} sat on the mat";?>[/code]it's much quicker performance wise and (IMO) easier to debug if you use:[code]<?php$animal = 'cat';$mystring = 'the ' . $animal . ' sat on the mat';$mystring = 'the ' . $animal . ' sat on the mat';?>[/code]note the use of single quotes instead of double. double quotes will have PHP do extra parsing. sure, the performance hit is nothing to cry about - but worth noting, nonetheless.as for HEREDOC, i personally find it a bit clumsy for keeping things tidy and organised - but again, that is my own preference, not the norm.CheersMark Quote Link to comment https://forums.phpfreaks.com/topic/23741-curly-bracket-question/#findComment-107866 Share on other sites More sharing options...
hostfreak Posted October 12, 2006 Author Share Posted October 12, 2006 Good advice redbullmarky, when the situation allows, I like your mentioned way as well. Looks nice and easy to debug as you mentioned. Both are nice, but the slightest bit of performance hit gives the way you mentioned the edge. Therefore I'll probably end up doing it that way. Ha, and to think, right after I got done going through a while script and doing it the other way, I learn a new way. Ah well. Thanks guys for all the advice. Quote Link to comment https://forums.phpfreaks.com/topic/23741-curly-bracket-question/#findComment-107941 Share on other sites More sharing options...
roopurt18 Posted October 12, 2006 Share Posted October 12, 2006 I think the performance impact between single and double quoted strings is probably minimal at best and probably not worth worrying about in most cases. I do agree that leaving variables outside the strings makes them easier to find though. Quote Link to comment https://forums.phpfreaks.com/topic/23741-curly-bracket-question/#findComment-107964 Share on other sites More sharing options...
Barand Posted October 12, 2006 Share Posted October 12, 2006 Depends on the tools you use. My editor (PHPEd) would display it as$mystring = "the [b]$animal[/b] sat on the mat";so it's easy to spot anyway. Quote Link to comment https://forums.phpfreaks.com/topic/23741-curly-bracket-question/#findComment-108064 Share on other sites More sharing options...
HuggieBear Posted October 13, 2006 Share Posted October 13, 2006 [quote author=Barand link=topic=111280.msg451111#msg451111 date=1160684536]Depends on the tools you use. My editor (PHPEd) would display it as$mystring = "the [b]$animal[/b] sat on the mat";[/quote]My editor (Notepad) isn't that clever I'm afraid ;)RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23741-curly-bracket-question/#findComment-108240 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.