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 Link to comment https://forums.phpfreaks.com/topic/81438-solved-newbie-question-about-php/ 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. Link to comment https://forums.phpfreaks.com/topic/81438-solved-newbie-question-about-php/#findComment-413404 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 Link to comment https://forums.phpfreaks.com/topic/81438-solved-newbie-question-about-php/#findComment-413415 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. Link to comment https://forums.phpfreaks.com/topic/81438-solved-newbie-question-about-php/#findComment-413420 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 Link to comment https://forums.phpfreaks.com/topic/81438-solved-newbie-question-about-php/#findComment-413422 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); ?> Link to comment https://forums.phpfreaks.com/topic/81438-solved-newbie-question-about-php/#findComment-413424 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 Link to comment https://forums.phpfreaks.com/topic/81438-solved-newbie-question-about-php/#findComment-413426 Share on other sites More sharing options...
revraz Posted December 13, 2007 Share Posted December 13, 2007 Are you just spamming for posts? Link to comment https://forums.phpfreaks.com/topic/81438-solved-newbie-question-about-php/#findComment-413429 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] Link to comment https://forums.phpfreaks.com/topic/81438-solved-newbie-question-about-php/#findComment-413432 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. Link to comment https://forums.phpfreaks.com/topic/81438-solved-newbie-question-about-php/#findComment-413436 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. Link to comment https://forums.phpfreaks.com/topic/81438-solved-newbie-question-about-php/#findComment-413471 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. Link to comment https://forums.phpfreaks.com/topic/81438-solved-newbie-question-about-php/#findComment-413475 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. Link to comment https://forums.phpfreaks.com/topic/81438-solved-newbie-question-about-php/#findComment-413503 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.