PHP Condition Example



IF Condition Example.
<?php
	$denom = 50;
	$num = 200;
	if($denom != 0 && $num/$denom > 2) {
		print('More than twice as much!');
	}
?>
IF Else Condition Example.
<?php
	$num1 = 200;
	$num2 = 100;
	if($num2 - $num1) {
	 if($num2 > $num1) {
		$diff = $num1 - $num2;
		print("The difference is $diff");
		echo '<br/>'; 
	 } else {
		$diff = $num2 - $num1;
		print("The difference is $diff");
		echo '<br/>'; 
	 }
	else { 
		print("There is no difference.");
	}
?>
Else if condition example
<?php
	if($num == 5) {
		echo 'Five Fish';
	} 
	else if($num == 4) {
		echo 'Four Birds';
	}
	else if($num == 3) {
		echo 'Three monkeyes'
	}
	else if($num == 2) {
		echo 'Two Ducks';
	} 
	else {
		echo 'A Lion';
	}
?>
Switch Example
<?php
	switch($num)
	{
		case 5: 
			echo 'Five Fish';
			break;
		case 4:
			echo 'Four Birds';
			break;
		case 3:
			echo 'Three monkeyes'
			break;
		case 2:
			echo 'Two Ducks';
			break;
		default: 
			echo 'A Lion';
	}
?>
Ternary Operator Example
<?php
	$a = 100;
        echo ($a > 1000)? 'Greater then thousands': 'Less than thousands';
?>
Ternary Operator Example2
<?php
	$a = 100;
        echo ($a % 2 == 0)? 'Even Number': 'Odd Number';
?>
Nested if else Example
<?php
            $a = 100;
            if(is_int($a)) {
                if($a > 1000) {
                    echo 'Greater then thousands';
                }
                else if($a == 1000) {
                    echo 'Number is equal to thousands';
                }
                else {
                    echo 'Less than thousands';
                }
            }
            else {
                echo 'Please enter a number';
            }
        ?>
        


Read more articles


General Knowledge



Learn Popular Language