Get latest news and Web Designing tutoria

Thursday, 28 May 2015

Adding up in PHP



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.)
To add up the contents of variables, you just separate each variable name with a plus symbol. Try this new script:
<?php
$first_number = 10;
$second_number = 20;
$sum_total = $first_number + $second_number;
$direct_text = 'The two variables added together = ';
print ($direct_text . $sum_total);
?>
In the above script, we've added a second number, and assigned a value to it:
$second_number = 20;
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:
$sum_total = $first_number + $second_number;
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.
To print out the answer, we've used concatenation:
print ($direct_text . $sum_total);
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:
$sum_total = $first_number + $second_number;
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 =).
You can, of course, add up more than two numbers. Try this exercise.

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

You don't have to use variable names to add up. You can do this:
print (10 + 20 + 30);
Or even this:
$number = 10;
print ($number + 30);
But the point is the same - use the plus symbol (+) to add up.

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

Related Posts:

  • PHP and Text Boxes on HTML Forms If you've been following along from the in the previous lesson then your basicForm.php now has a METHOD and ACTION set. We're going to use these to process text that a user has entered into a text box. The MET… Read More
  • PHP and the Submit Button of HTML Forms The HTML Submit button is used to submit form data to the script mentioned in the ACTION attribute. Here's ours: <Form Name ="form1" Method ="POST" ACTION = "basicForm.php"> So the page mentioned in the ACTION … Read More
  • PHP and the Action Attribute of HTML Forms The Action attribute is crucial. It means, "Where do you want the form sent?". If you miss it out, your form won't get sent anywhere. You can send the form data to another PHP script, the same PHP script, an email … Read More
  • PHP and the Post Attribute of HTML Forms <FORM NAME ="form1" METHOD ="POST" ACTION = ""> The ?Submit1=Login part from the previous section is now gone! That is because we used POST as the method. Using POST means that the form data won't get … Read More
  • PHP Submit buttons In the previous lesson you saw how to get text from a textbox when a Submit button on a form was clicked. However, when you first load the page the text still displays. The reason why the text displays when the page i… Read More

0 comments:

Post a Comment