Jump to content

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.
Link to comment
https://forums.phpfreaks.com/topic/23741-curly-bracket-question/
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
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
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.
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
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.
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 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
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.