ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Sample PHP code Programs for if statement

Updated on April 9, 2013

PHP Code Example, PHP Scripts Sample, PHP Conditional Statements, PHP if Statement code sample

Do you wisth to see more php code examples? This hub contains php code example in if statement which is connected to a html file. Do you know how to run an html file in your browser? if not see this hub that illustrate on how to run an html file in your browser. See how the codes works! If you do not know how to run a PHP code on your browser please read this hub too.Also, to facilitate better understanding, in every PHP code examples provided, there will be a sample output after the code.



Note: Remember the php file is connected to form.html. They connected with an action to the form that indicated its file name. Save the two group of codes given on drive C (where you installed your Xaamp)-> XAAMP folder->htdocs folder. To avoid confusion below is the suggested file name for each of them.

ifstatement.php – for if statement PHP code example

form.html - for the html file

To run the file just type on your browser this: localhost/form.html and see how it works!



Sample code for form.html

<html>
	
	<form method = "post" action = "ifstatement.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>

Sample Output of the Code Above

PHP Code Example for ifstatement.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;
		}
	}
	
	
?>

Sample Output for ifstatement.php

See it for yourselves and run it on your own browser.For more clarifications, if you are a beginner, just follow the steps below.

Granted that you already installed XAAMP, openned the Xaamp and run apache and mysql and also installed notepad++ editor.

1. Copy and paste the form.html to notepad++ and save to the drive where your XAAMP is installed, look for Xaamp folder, click it and look for htdocs folder and click it and save it there.

2. Copy and paste the ifstatement .php to notepad++ and do the previous step.

3. Go to your browser and type this: localhost/html.php

4. You should be directed to the blank form of form.html, enter to the form and hit enter. That is how it runs.

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      solardoomz 4 years ago

      Nice and useful article

    • aisha91 profile image
      Author

      Rasna Aisha 4 years ago from Manila, Philippines

      Thank you soladoomz for the feedback, hope you find my other hubs useful too! :)

    • profile image

      jaydeep 2 years ago

      Nice tutorial

    Click to Rate This Article