nirvanaforu Posted December 13, 2007 Share Posted December 13, 2007 Hi there, I have a really simple question: how coud I print or echo the result of some arithmetric equations? what's the correct syntax? For example: <? echo "2+3";?> will output the string '2+3' but if I have two variables $a and $b, which $a=2, $b=3 now <? echo "$a+$b"?> will output "2+3" how could I get 5 except using another variable to store the sum first? Thanks Quote Link to comment Share on other sites More sharing options...
revraz Posted December 13, 2007 Share Posted December 13, 2007 You are treating them as strings, you need to treat them as numbers. Quote Link to comment Share on other sites More sharing options...
nirvanaforu Posted December 13, 2007 Author Share Posted December 13, 2007 Hi revraz: sure, I know that echo $a+$b without the double quote will work. However, if the string and numbers are mixed if there is some operator to that. For example: <? $a=1; $b=2; echo "the sum is ". $a+$b . "\n"; ?> unfortunately, the '.' operator can't connect string with the result of numbers? I don't want to use two echos to ouput this sentence. Do you know what I'm talking about? Thanks Quote Link to comment Share on other sites More sharing options...
Xyphon Posted December 13, 2007 Share Posted December 13, 2007 Never start wiht <?, <?PHP is more secure, and blah blah. Just use it, and it can mess up in like, PHP 5, and such. Quote Link to comment Share on other sites More sharing options...
nirvanaforu Posted December 13, 2007 Author Share Posted December 13, 2007 yes, I'm using <?php ............?> actually. Thanks Quote Link to comment Share on other sites More sharing options...
revraz Posted December 13, 2007 Share Posted December 13, 2007 <?php $a=1; $b=2; echo "the sum is " . ($a + $b); ?> Quote Link to comment Share on other sites More sharing options...
Xyphon Posted December 13, 2007 Share Posted December 13, 2007 Also, use <?PHP, it might mess up when you have a big site if you dont O.O Quote Link to comment Share on other sites More sharing options...
revraz Posted December 13, 2007 Share Posted December 13, 2007 Are you just spamming for posts? Quote Link to comment Share on other sites More sharing options...
CMC Posted December 13, 2007 Share Posted December 13, 2007 Set the sum as a variable, like so. That will work for you <?php $a=1; $b=2; $sum = $a + $b; echo "the sum is: ".$sum; ?> [/code] Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 13, 2007 Share Posted December 13, 2007 echo actually accepts a coma-separated list of operands, which will evaluate a term like $a + $b (tested) - <?php $a=1; $b=2; echo "the sum is ",$a + $b,"\n"; ?> BTW: the short open php tag <? has nothing to do with php5. It was a lazy way short cut that has caused more problems then it was worth and results in code that is not portable between different server configurations and should not be used. Quote Link to comment Share on other sites More sharing options...
nirvanaforu Posted December 13, 2007 Author Share Posted December 13, 2007 Thanks, man. That's what I'm looking for, coma , haha however, as I just tested, print won't accept coma-separated lists. That's the only differences I know about print and echo now.. echo actually accepts a coma-separated list of operands, which will evaluate a term like $a + $b (tested) - <?php $a=1; $b=2; echo "the sum is ",$a + $b,"\n"; ?> BTW: the short open php tag <? has nothing to do with php5. It was a lazy way short cut that has caused more problems then it was worth and results in code that is not portable between different server configurations and should not be used. Quote Link to comment Share on other sites More sharing options...
Xyphon Posted December 13, 2007 Share Posted December 13, 2007 Are you just spamming for posts? No. He said <?php is what he uses, <?PHP is more secure. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 13, 2007 Share Posted December 13, 2007 <?php and <?PHP are the exact same thing. Quote Link to comment 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.