Jump to content

[SOLVED] newbie question about PHP


nirvanaforu

Recommended Posts

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

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

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.