Jump to: navigation, search

Php else

From w3cyberlearnings

Contents

else condition

else is for the condition that the if condition is not meet.

Syntax if

if(expr)
 statement
else
 statement

Example 1

<?php

$a = 10;
$b = 30;

if($a > $b) {
	echo '$a is bigger than $b';
} 
else {
	echo '$b is bigger than $a';
}
?>

Output

$b is bigger than $a

Example 2

<?php

$pass = 1;

if ($pass) {
	echo 'you are done';
} else {
	echo 'you are not done';
}
?>

Output

you are done

Example 3

  • This will generate the INPUT form for user to input two numbers. When user click on the Check Result,

the system will check the user input result and display result accordingly back to the user.

<?php
if ($_GET) {
	if ($_GET['number1'] > $_GET['number2']) {
		echo 'Input 1 is larger than Input 2';
	} else {
		echo 'Input 2 is larger than Input 1';
	}
}
?>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title></title>
	</head>
	<body>
		<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
			Input 1<input type="text" name="number1"/><br/>
			Input 2<input type="text" name="number2"/><br/>
			<input type="submit" name="submit" value="Check Result"/>
		</form>
	</body>
</html>

Output

The output is depending on the user input.

Related Links


Template:ALL

Navigation
Web
SQL
MISC
References