12. Logical Operator

Logical Operators:

&& and a && b true if a and b are true, otherwise false
|| or a || b true if a or b are true, otherwise false
xor xor a xor b true if a or b — but not both — are true, otherwise false
! not !a true if a is false; false if a is true
Example-1

<?php
	$x=5;
	if($x>5 || $x<5){
		echo “five”;
	}
	else {
		echo “nothing”;
	}
?>
Example-2

<?php
	$x=5;
	if($x>5 || $x<5){
		echo “five”;
	}
	elseif($x==5 && $x==6) {
		echo “This is true”;
	}
	else {
		echo “nothing”;
	}
?>
Example-3

<?php
	$x=5;
	if($x!=6){   // isn’t 6
		echo “Five”;
	}
	else {
		echo “nothing”;
	}
?>
Example-4

<?php
	$x=35;
	if($x>0 && $x<=12){
		echo "You’re Child";
	}
	elseif($x>12 && $x<=16){
		echo "You’re Young";
	}
	elseif($x>16 && $x<=40){
		echo "You’re Adult";
	}
	else{
		echo "You’re Old";
	}
?>	

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s