Error using print {} and a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ericrox
Forum Newbie
Posts: 2
Joined: Thu Oct 18, 2007 7:14 am

Error using print {} and a function

Post 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]
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
ericrox
Forum Newbie
Posts: 2
Joined: Thu Oct 18, 2007 7:14 am

Thanks

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