Jump to content

Curly Bracket question


hostfreak

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
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
Link to comment
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
Link to comment
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.
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.