Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 6th, 2007, 08:01 AM
Registered User
 
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aaaa0441
Default What's the use of braces in a string literal?

I am currently reading "Professional PHP5", I encountered the following code and do not understand the use of the curly braces, "{}". Please tell me.

print "<h1>Individual - {$objEntity->__toString()}</h1>";

May the force be with you.
 
Old January 16th, 2007, 07:16 AM
Authorized User
 
Join Date: Oct 2006
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,

  the use of curly braces is to make separate process with that line number..

______________________________________
.::If you quit you loss, If you survive you win::.

.::JHANNY::.

 
Old January 16th, 2007, 08:51 AM
Registered User
 
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aaaa0441
Default

which line number are you refering to?

--------------------------
May the force be with you.
 
Old January 17th, 2007, 12:39 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 479
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to surendran Send a message via Yahoo to surendran
Default

hi aaaa0441,
ex:
case 1:
if($i==5)
echo ("Number 5");


case 2:
if($i==5){
echo ("Number 5");
echo ("Number 5 line 2");
}

when you look both case, in case one we do not have curly braces, so it will print only 1 line,
but when u look case 2, we are using curly braces, so it will print both lines,

surendran
(Anything is Possible)
http://ssuren.spaces.msn.com
 
Old January 18th, 2007, 09:39 AM
Registered User
 
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aaaa0441
Default

Thanks, surendran. But I don't think you got my question.
My question is the use of curly braces within a string literal.

Please test the following code and, if you understand, tell me why the outputs are different?

Code:

class test{
  private $strTest;

  public function __construct($strTest="Just to test it.<br />"){
    $this->strTest=$strTest;
  }

  public function __toString(){
    return $this->strTest;
  }
}

$objTest=new test();
print "Test with curly braces: {$objTest->__toString()}";
print "Test without curly braces: $objTest->__toString()";

--------------------------
May the force be with you.
 
Old January 18th, 2007, 02:27 PM
NC NC is offline
Registered User
 
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by aaaa0441
 I am currently reading "Professional PHP5", I encountered the following code and do not understand the use of the curly braces, "{}". Please tell me.

print "<h1>Individual - {$objEntity->__toString()}</h1>";
This is done so that the interpreter knows exactly where the variable identifier ends.

Consider this situation:

class WierdClass {
  public $aa;
  public $aaa;
  // The rest of definition goes here...
}

$myWeirdObject = new WierdClass();
echo "$myWeirdObject->aaaaa";

So what is the interpreter do, output myWeirdObject->aa and string "aaa" or myWeirdObject->aaa and string "aa"? To avoid confusion, you have the option of using the curly braces to tell the interpreter exactly what to do:

echo "{$myWeirdObject->aa}aaa";

or

echo "{$myWeirdObject->aaa}aa";
 
Old January 18th, 2007, 02:36 PM
NC NC is offline
Registered User
 
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by aaaa0441
 Please test the following code and, if you understand, tell me why the outputs are different?

Code:

class test{
  private $strTest;

  public function __construct($strTest="Just to test it.<br />"){
    $this->strTest=$strTest;
  }

  public function __toString(){
    return $this->strTest;
  }
}

$objTest=new test();
print "Test with curly braces: {$objTest->__toString()}";
print "Test without curly braces: $objTest->__toString()";
In the second case, the interpreter, for lack of better information, treats "$objTest->__toString()" as a variable "$objTest->__toString" followed by a pair of parentheses. Since the test object nas no property called __toString, a NULL value is output, followed by a pair of parentheses.

In the first case, the interpreter is explicitly told that "$objTest->__toString()" is a complete identifier and treats it as such, so the output is the value retured by the method...
 
Old January 20th, 2007, 04:23 AM
Registered User
 
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aaaa0441
Default

Thanks a lot, NC.
Your answer is the clearest and most convincing one.

--------------------------
May the force be with you.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Label vs Literal pinch BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 October 9th, 2007 01:01 PM
What's the use of braces in a string literal? aaaa0441 Beginning PHP 4 January 14th, 2007 08:10 AM
Literal Control bilal589 ASP.NET 1.0 and 1.1 Basics 5 August 19th, 2006 01:42 PM
Using right() to return a literal value Somed00d Access 4 February 8th, 2006 02:56 PM
Where are Curly Braces required? ababb BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 January 20th, 2005 11:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.