your image

PHP Subtracting Two Numbers Program - javatpoint

javapoint
Related Topic
:- PHP information technology

Subtracting Two Numbers

There are three methods to subtract two numbers:

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

Subtraction in Simple Code

Subtraction of two numbers 30 and 15 is shown.

Example:

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

Output:

 

C++ vs Java

Subtraction in Form

By inserting values in the form two numbers can be subtracted.

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="Subtract">  
  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 difference of $number1 and $number2 is: ".$sum;  
  17.     }  
  18. ?>  
  19. </body>  
  20. </html>  

Output:

Subtraction in Form without (-) Operator

By inserting values in the form two numbers can be subtracted but without using (-) operator.

Example:

  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="Subtract">  
  8. </form>  
  9.  </body>  
  10. <?php  
  11.       
  12. @$number1=$_GET['number1'];   
  13. @$number2=$_GET['number2'];  
  14. for ($i=1; $i<=$number2; $i++)     
  15. {      
  16.  $number1--;      
  17. }      
  18. echo "Difference=".$number1;  
  19. ?>  

Output:

Comments