Thursday, February 5, 2015

Swap a Two Numbers in PHP

In this article I would like to share with you a simple program that will ask the user to enter two numbers and then our program will swap the arrangement of numbers using PHP as our programming language. One of the strength of PHP it has many built in data structure function that makes programming much easier compared with traditional programming language that requires you to wrote your own function or methods just to achieve the same results. I hope you will find my work useful in your learning in PHP.

If you have some questions please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com. People here in the Philippines who wish to contact me can reach me at my mobile number 09173084360.

Thank you very much and Happy Productive Programming.


Sample Output of Our Program

Program Listing

<html>
<head>
</head>
<body>
<?php include 'template-parts/header.php' ?><br>
<div class="container home">
<?php
    $value1 = $_POST['num1'];
    $value2 = $_POST['num2'];
    if(isset($_POST['solve']))
    {
    $result1 = "<font size='4' color='blue' > Original Arrangement  :=> "
              .$value2. " and ".$value1.".</font> <br><br>";
list($value2, $value1) = array($value1, $value2);
$result2 = "<font size='4' color='blue' > After Swapping        :=> "
            .$value2. " and ".$value1.".</font> <br>";
    }
if (isset($_POST['clear']))
{
$value1=""; $value2=""; 
$result1=""; $result2="";
}
?>
<form action="index.php" method="post">
<font size="6" color="blue">
<label> Enter First Number: </label>
<input type="text" placeholder="first number" autofocus name="num1" value="<?php echo $value1; ?>" />
<br />
<label> Enter Second Number: </label>
<input type="text" placeholder="second number"  name="num2" value="<?php echo $value2; ?>"  />
<br />
 </font>
<input type="submit" name="solve" value="Swap"
 title="Click here to swap values." class="btn btn-info">
<input type="submit" name="clear" value="Clear"
 title="Click here to clear text box." class="btn btn-info">
</form>
<?php
 echo "<br><br>";
 echo $result1;
 echo $result2;
 ?>
</div>
</body>
</html>



No comments:

Post a Comment