your image

PHP Adding Two Numbers Program - javatpoint

javapoint
Related Topic
:- PHP information technology

Adding Two Numbers

There are three methods to add two numbers:

  • Adding in simple code in PHP
  • Adding in form in PHP
  • Adding without using arithmetic operator (+).

Adding in Simple Code

Addition of two numbers 15 and 30 is shown here.

Example:

  1. <?php  
  2. $x=15;  
  3. $y=30;  
  4. $z=$x+$y;  
  5. echo "Sum: ",$z;  
  6. ?>  

Output:

 

How to find Nth Highest Salary in SQL

Adding in Form

Two numbers can be added by passing input value in the form.

Example:

  1. <html>  
  2. <body>  
  3. <form method="post">  
  4. Enter First Number:  
  5. <input type="number" name="number1" /><br><br>  
  6. Enter Second Number:  
  7. <input type="number" name="number2" /><br><br>  
  8. <input  type="submit" name="submit" value="Add">  
  9. </form>  
  10. <?php  
  11.     if(isset($_POST['submit']))  
  12.     {  
  13.         $number1 = $_POST['number1'];  
  14.         $number2 = $_POST['number2'];  
  15.         $sum =  $number1+$number2;     
  16. echo "The sum of $number1 and $number2 is: ".$sum;   
  17. }  
  18. ?>  
  19. </body>  
  20. </html>  

Output:

Adding in Simple Code

Two numbers can be added by passing input value in the form but without using (+) operator.

  1. <body>  
  2. <form>  
  3. Enter First Number:  
  4. <input type="number" name="number1" /><br><br>  
  5. Enter Second Number:  
  6. <input type="number" name="number2" /><br><br>  
  7. <input  type="submit" name="submit" value="Add">  
  8. </form>  
  9.  </body>  
  10. <?php      
  11. @$number1=$_GET['number1'];   
  12. @$number2=$_GET['number2'];   
  13. for ($i=1; $i<=$number2; $i++)     
  14. {      
  15.  $number1++;      
  16. }      
  17. echo "Sum of $number1 and $number2 is=".$number2;  
  18. ?>  

Output:

Comments