Jump to content

Archived

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

hostfreak

Curly Bracket question

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 :D

Regards
Huggie

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Cheers
Mark

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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 ;)

Regards
Huggie

Share this post


Link to post
Share on other sites

×

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.