- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
PHP Code Sample in Coding if else-if Statement
PHP code, PHP code example on if, else-if statement. A conditional statement on PHP Programming. Given sample code output.
This hub contains php code example of if else-if statement. Like the php code examples on php conditional statements, this php file is also connected with a html file.
Note: The name of html file is form3.html and the php file is elseifstatement.php. If you wish to try this code better save it with this file name too. If you do not know how to run an html file on your browser please take a look on this hub that offers steps on how to do it. And also, if you do not know how to run a php file, read this hub too to know how it is done.
So, now, let start!
form3.html Sample Code
<html> <form method = "post" action = "elseifstatement.php"> <table> <tr> <td>Enter Your Name:</td> <td><input type = "text" name = "name" /></td> </tr> <tr> <td>Enter Year of Birth:</td> <td><input type = "text" name = "year" /></td> </tr> <tr> <td>Enter Month of Birth:</td> <td><input type = "text" name = "month" /></td> </tr> <tr> <td>Enter Date of Birth:</td> <td><input type = "text" name = "date" /></td> </tr> <tr> <td></td> <td><input type = "submit" name = "submit" value="SUBMIT"/></td> </tr> </table> </form> </html>
form3.html Sample Output
PHP Code Example on elseifstatement.php
<?php $name = $_POST['name']; $year = $_POST['year']; $month = $_POST['month']; $date = $_POST['date']; if($_POST['submit']) { echo "Your Name is: ". $name."<br/>"; echo "Your birthday is on: ". $year."-".$month."-".$date."<br /><br />"; echo "Today's date is: ".date("Y-m-d")."<br /><br />"; if(date("d") == $date && date("m") == $month) { echo "OMG! Today is your birthday! <br />" ; echo "I wish you a happy birthday ".$name; } else if(date("m") < $month) { $age = (date("Y") - $year) - 1; echo "Your current Age is: ".$age." yrs. Old."; } else if(date("m") > $month) { $age = (date("Y") - $year); echo "Your current Age is: ".$age." yrs. Old"; } else if(date("m") == $month && date("d") > $date) { $age = (date("Y") - $year) - 1; echo "Your current Age is: ".$age." yrs. Old."; } else { $age = (date("Y") - $year); echo "Your current Age is: ".$age." yrs. Old."; } } ?>
elseifstatement.php Sample Output
A Beginners Guide on Running this Code
To provide more clarification for beginners, just follow the steps below to be able to test the codes above.
1. Install XAAMP and Notepad++.
2. Open the XAAMP and run the apache and mysql
3. Copy and Paste the code on form3.html on notepad++ and save it to where you install your XAAMP and open the Xaamp folder on that drive, open htdocs folder and save the file there with the same file name I have given.
4. Save the elseifstatement.php like you did above.
5. Go to your browser and type this on the address bar: localhost/form3.html
Other Sample PHP Codes and Tutorials
- How to become a PHP Developer: Learn and Earn
- Steps on How to Program in PHP: A Beginner's Guide on PHP Programming-Part 1
- How to run PHP Code or PHP Scripts and See the Result on Your Browser
- PHP Sample Codes On Function:How to make Function in PHP
- HTML Basics: Learn The Basics of HTML and Create Your Own Webpage on Your Browser
- PHP Code Example on Associative Array and Multidimensional Array
- PHP Code Example on Foreach Loop and For Loop
- PHP Code Example on Do-while Loop
- PHP Code Example on Numeric Array
- PHP Code Example on While Loop
- PHP Code Example on Switch Statement
- PHP Code Example on if else-if Statement
- PHP Code Example for if-else Statement
- PHP code Example for if statement