ericorx Posted October 18, 2007 Share Posted October 18, 2007 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. $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 Quote Link to comment https://forums.phpfreaks.com/topic/73789-error-using-print-and-a-function/ Share on other sites More sharing options...
wildteen88 Posted October 18, 2007 Share Posted October 18, 2007 You can only use {} around variables within a string not for calling functions/methods Quote Link to comment https://forums.phpfreaks.com/topic/73789-error-using-print-and-a-function/#findComment-372512 Share on other sites More sharing options...
Barand Posted October 18, 2007 Share Posted October 18, 2007 This works for method calls but not function calls (PHP 5.0) <?php class a { function totalWeight() {return '9,999';} } function c() { return 'xxx'; } $b = new a; print "total weight = {$b->totalWeight()} kg <br>"; // works --> total weight = 9,999 kg print "total weight = {c()} kg <br>"; // NO --> total weight = {c()} kg print "total weight = " . c() . " kg <br>"; // works --> total weight = xxx kg ?> EDIT: PHP 4.3.9 gives parse error on the print with method call Quote Link to comment https://forums.phpfreaks.com/topic/73789-error-using-print-and-a-function/#findComment-372613 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.