ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

PHP Code Sample in Coding if else-if Statement

Updated on February 22, 2013

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

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article