PHPsu
MANUAL ZH  |  EN
     


Current Position :| index>Beginners> Adding up in PHP

Adding up in PHP

FROM: AUTHOR: TIME:2008-08-09 HITS:

OK, let's do some adding up. To add up in PHP, the plus symbol (+) is used. (If you still have the code open from the previous page, try changing the full stop to a plus symbol. Run the code, and see what happens.)

do you kown phpsu.com?

To add up the contents of variables, you just separate each variable name with a plus symbol. Try this new script: phpsu.com

<?php

phpsu提供的php教程

$first_number = 10;
$second_number = 20;
$sum_total = $first_number + $second_number;

phpsu.com is a free phpscool

$direct_text = 'The two variables added together = ';

do you kown phpsu.com?

print ($direct_text . $sum_total);

phpsu

?>

phpsu提供的php教程

In the above script, we've added a second number, and assigned a value to it:

do you kown phpsu.com?

$second_number = 20;

phpsu.com

A third variable is then declared, which we've called $sum_total. To the right of the equals sign, we've added up the contents of the first variable and the contents of the second variable:

http://www.phpsu.com

$sum_total = $first_number + $second_number;

phpsu

PHP knows what is inside of the variables called $first_number and $second_number, because we've just told it in the two line above! It sees the plus symbol, then adds the two values together. It puts the answer to the addition in the variable to the left of the equals sign (=), the one we've called $sum_total. phpsu提供的php教程

To print out the answer, we've used concatenation: phpsu.com is a free phpscool

print ($direct_text . $sum_total);

do you kown phpsu.com?

This script is a little more complicated than the ones you've been doing. If you're a bit puzzled, just remember what it is we're doing: adding the contents of one variable to the contents of another. The important line is this one:

http://www.phpsu.com

$sum_total = $first_number + $second_number;

do you kown phpsu.com?

The addition to the right of the equals sign gets calculated first ($first_number + $second_number). The total of the addition is then stored in the variable to the left of the equals sign ($sum_total =). phpsu.com

You can, of course, add up more than two numbers. Try this exercise. phpsu.com


Exercise
Add a third variable to your code. Assign a value of 30 to your new variable. Put the sum total of all three variables into the variable called $sum_total. Use concatenation to display the results. (In other words, add up 10, 20, and 30!)

phpsu is a phpschool


You don't have to use variable names to add up. You can do this:

phpsu.com is a free phpscool

print (10 + 20 + 30); www.phpsu.com

Or even this: www.phpsu.com

$number = 10; phpsu提供的php教程

print ($number + 30);

phpsu is a phpschool

But the point is the same - use the plus symbol (+) to add up.

phpsu提供的php教程

 

phpsu

In the few next parts, you'll learn how to Subtract, Divide and Multiply.

http://www.phpsu.com


TITLE:Adding up in PHP
Copyright 2008 The PHPsu All rights reserved. This mirror generously provided by: .Hp Inc.
Last updated: Sat Aug 9 14:10:47 GMT-8 2008