PHP Examples

Note: I understand the indentation is very off, but try to bear with it as text processors have horrible indentation styling than environments.

Hello World

<!DOCTYPE html>
<html>
<head>
	<link type="text/css" rel="stylesheet" href="style.css"/>
		<title>Hello World</title>
	</head>
	<body>
<p>Hello World!</p>
	</body>
</html>

Hello World Alternative

<!DOCTYPE html>
<html>
	<head>
<link type="text/css" rel="stylesheet" href="style.css"/>
<title>Hello World Alternative</title>
	</head>
	<body>
<p>
<?php
echo "Hello World!" ;
?>
</p>
	</body>
</html>

Math

Addition:

<!DOCTYPE html>
<html>
	<head>
<link type="text/css" rel="stylesheet" href="style.css"/>
<title>Addition</title>
	</head>
	<body>
<p>
<?php
echo 2 + 2;
?>
</p>
	</body>
</html>

Subtraction:

<!DOCTYPE html>
<html>
	<head>
<link type="text/css" rel="stylesheet" href="style.css"/>
<title>Subtraction</title>
	</head>
	<body>
<p>
<?php
echo 2 - 2;
?>
</p>
	</body>
</html>

Multiplication:

<!DOCTYPE html>
<html>
	<head>
<link type="text/css" rel="stylesheet" href="style.css"/>
<title>Multiplication</title>
	</head>
	<body>
<p>
<?php
echo 2 * 2;
?>
</p>
	</body>
</html>

Division:

<!DOCTYPE html>
<html>
	<head>
<link type="text/css" rel="stylesheet" href="style.css"/>
<title>Division</title>
	</head>
	<body>
<p>
<?php
echo 2 / 2;
?>
</p>
	</body>
</html>

Modulo:

<!DOCTYPE html>
<html>
	<head>
<link type="text/css" rel="stylesheet" href="style.css"/>
<title>Modulo</title>
	</head>
	<body>
<p>
<?php
echo 2 % 2;
?>
</p>
	</body>
</html>

Variables

<!DOCTYPE html>
<html>
	<head>
<link type="text/css" rel="stylesheet" href="style.css"/>
<title>Variable</title>
	</head>
	<body>
<p>
<?php
$StringVariable = "Hello World!";
$IntVariable = 0;
echo $StringVariable;
echo $IntVariable;
?>
</p>
	</body>
</html>

Variable Math

Addition:

<!DOCTYPE html>
<html>
	<head>
<link type="text/css" rel="stylesheet" href="style.css"/>
<title>Variable Math</title>
	</head>
	<body>
<p>
<?php
$myFirstVariable = 2 + 2;
echo $myFirstVariable;
?>
</p>
	</body>
</html>

Subtraction:

<!DOCTYPE html>
<html>
	<head>
<link type="text/css" rel="stylesheet" href="style.css"/>
<title>Variable Math</title>
	</head>
	<body>
<p>
<?php
$myFirstVariable = 2 - 2;
echo $myFirstVariable;
?>
</p>
	</body>
</html>

Multiplication:

<!DOCTYPE html>
<html>
	<head>
<link type="text/css" rel="stylesheet" href="style.css"/>
<title>Variable Math</title>
	</head>
	<body>
<p>
<?php
$myFirstVariable = 2 * 2;
echo $myFirstVariable;
?>
</p>
	</body>
</html>

Division:

<!DOCTYPE html>
<html>
	<head>
<link type="text/css" rel="stylesheet" href="style.css"/>
<title>Variable Math</title>
	</head>
	<body>
<p>
<?php
$myFirstVariable = 2 / 2;
echo $myFirstVariable;
?>
</p>
	</body>
</html>

Modulo:

<!DOCTYPE html>
<html>
	<head>
<link type="text/css" rel="stylesheet" href="style.css"/>
<title>Variable Math</title>
	</head>
	<body>
<p>
<?php
$myFirstVariable = 2 % 2;
echo $myFirstVariable;
?>
</p>
	</body>
</html>

Comment

<!DOCTYPE html>
<html>
	<head>
	<link type="text/css" rel="stylesheet" href="style.css"/>
	<title>Variable Math</title>
	</head>
	<body>
	<p>
		<?php
		// This is a comment, the language will always ignore anything after this
		/* This is a multiline comment,
		anything after the slash and multiplication sign will be ignored,
		until a multiplication sign and a slash is typed */
		?>
	</p>
</body>
</html>

If/Else Age Verification

<!DOCTYPE html>
<html>
	<head>
	<link type="text/css" rel="stylesheet" href="style.css"/>
	<title>Variable Math</title>
	</head>
	<body>
	<p>
		<?php
      		$age = 17;
       		if ($age >= 18) {
			echo "You may enter!";
		} else {
			echo "Not there yet buddy!";
       		}
		?>
	</p>
</body>
</html>

Sales Discount

<!DOCTYPE html>
<html>
	<head>
		<link type="text/css" rel="stylesheet" href="style.css"/>
		<title>Variable Math</title>
	</head>
	<body>
		<p>
			<?php
	$items = 6;
	if ($items > 5) {
		echo "You get a 10% discount!";
	} elseif ($items == 1) {
		echo "Sorry, no discount!";
	} elseif ($items <= 0) {
		echo "Select an item please!";
	} else {
		echo "You get a 5% discount!";
	}
?>
<p>
</body>
</html>

Programming


QR Code
QR Code php_examples (generated for current page)