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

January 6th, 2007, 08:01 AM
|
Registered User
|
|
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

January 16th, 2007, 07:16 AM
|
Authorized User
|
|
Join Date: Oct 2006
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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::.
|

January 16th, 2007, 08:51 AM
|
Registered User
|
|
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
which line number are you refering to?
--------------------------
May the force be with you.
|

January 17th, 2007, 12:39 AM
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 479
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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
|

January 18th, 2007, 09:39 AM
|
Registered User
|
|
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

January 18th, 2007, 02:27 PM
|
Registered User
|
|
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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";
|

January 18th, 2007, 02:36 PM
|
Registered User
|
|
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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...
|

January 20th, 2007, 04:23 AM
|
Registered User
|
|
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks a lot, NC.
Your answer is the clearest and most convincing one.
--------------------------
May the force be with you.
|
|
 |