Page 1 of 1

Error using print {} and a function

Posted: Thu Oct 18, 2007 7:33 am
by ericrox
scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have some code that doesn't seem to work and i'm not sure why.  I tried three different ways of coding it and example 1 and 2 work but the third doesn't.
I get a unexpected ( error from Example 3. 

Is it just that I have () enclosed in {} enclosed in a print string?  Is there away around this.. It just seems shorter than concatenating.

I have a UnitCounter class with a function called totalWeight that does is a quick calculation of units * weight and return that value.  I have assigned weights and unit numbers to the vars.

Code: Select all

$b = new UnitCounter;

//Example 1:  This works
print "total weight = " . $b->totalWeight() . "kg"; 

//Example 2:  This works
$a = $b->totalWeight();
print "total weight = {$a} kg"; 

//Example 3:  This doesn't work.. why?
print "total weight = {$b->totalWeight()} kg";

Thanks,
e


scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Oct 18, 2007 7:58 am
by s.dot
This user comment from the manual might pertain to something related to the problem. (great choice of wording? eh? :P)

http://us3.php.net/manual/nl/language.t ... .php#76176

Thanks

Posted: Thu Oct 18, 2007 8:32 am
by ericrox
Hey thanks. Yeah the post looks much nicer.

It looks like the guy has similar issue.

I guess mine is a bit different since I'm using a function from a class. But it seems the same problem occurs with the unexpected ( error.

I guess you have to either use a variable or concatenate. If you use brackets.

e