Jump to content


Photo

Curly Bracket question


  • Please log in to reply
10 replies to this topic

#1 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 12 October 2006 - 08:59 AM

I am curious, a lot of times I see something like (just an example):
  $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;
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.

#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 12 October 2006 - 10:47 AM

Hostfreak, The curly braces are used for variable parsing...

When a string is enclosed in either double quotes or within the heredoc syntax, 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
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 12 October 2006 - 11:31 AM

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.

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 12 October 2006 - 11:35 AM

No problem, when I first looked I couldn't find it either so I asked here too... See this post.

Always a good idea to search the forums first too :)

Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 12 October 2006 - 11:46 AM

I always put them in curly brackets. I think that
"Bla bla {$variable} bla bla again"
looks nicer than
"Bla bla $variable bla bla again"

It also makes it easier to spot variables.

#6 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 12 October 2006 - 11:47 AM

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.

#7 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 12 October 2006 - 12:22 PM

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):

<?php
$animal = "cat";

$mystring = "the $animal sat on the mat";
$mystring = "the {$animal} sat on the mat";
?>

it's much quicker performance wise and (IMO) easier to debug if you use:

<?php
$animal = 'cat';

$mystring = 'the ' . $animal . ' sat on the mat';

$mystring = 'the ' .
            $animal .
            ' sat on the mat';
?>

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
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#8 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 12 October 2006 - 03:09 PM

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.

#9 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 12 October 2006 - 04:34 PM

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.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 12 October 2006 - 08:22 PM

Depends on the tools you use. My editor (PHPEd) would display it as

$mystring = "the $animal sat on the mat";

so it's easy to spot anyway.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 13 October 2006 - 09:57 AM

Depends on the tools you use. My editor (PHPEd) would display it as

$mystring = "the $animal sat on the mat";


My editor (Notepad) isn't that clever I'm afraid ;)

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users