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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.