Wednesday 26 February 2014

Logical Operators in php

logical operators
This tutorial is for Logical Operators. You already have seen examples of Assignment Operators In Php,Arithmetic operators In Php,Comparison operators In Php .We had assign operators and compared them but what if we want to use little bit of logic??? For example, to check few variables once and compared it with different values, best way to explain it is:-

Code for comparison operators(Screenshot1):-

Code for Comparison Operators
Code for Comparison Operators Screenshot1

output (Screenshot2) :-

Logical Operators In PHP
Screenshot2
In this example we have already seen comparing number1 and its true. Now what if you want to compare more variables,for that you have to take more “if statements”.Logical operators does work easy. Logical Operators are AND (&&), OR(||),NOT etc.In thistutorial we will look at OR, we can write in both way OR/|| and AND/&&.Lets see example for Logical operators to find upper bound and lower bound.

Code for AND logical operators (Screenshot3):-

Logical Operators
Code for AND logical Operators Screenshot3



Ouput (Screenshot4):-

Logical Operators In php
Screenshot4
In this example I have created variable upper value is 1000 and lower value is 500.I want to test whether specific number applied would be between values of lower and upper.So I want my variable number would be 1000 or 500 or something in between them.It can’t be 499 less then 500 and also it can’t be greater then 1000 like 1001.So my value of number is 654,but how can we check it if number is greater then or equal to lower && (AND) number is less then or equal to upper.So, It has to be greater then lower and less to from upper.The answer is ok because number is 654.

Code for AND logical operators (Screenshot5):-

Logical Operators
Screenshot5

output (Screenshot6):-

Logical Operators
Screenshot6
In this example number value we had changed to 999.999 and its echoing “OK” because its also less then 1000.

Code for AND logical operators (Screenshot7):-

Code for AND logical Operators
Screenshot7

Output Screenshot8

Logical Operators
Screenshot8
In this example number is greater then upper value it evaluates false to if statement and it will echo out number which must be between lower value and upper value.

Code for AND logical operators (Screenshot9):-

Logical Operators
Screenshot9

Output Screenshot10

Logical Operators In php
Screenshot10

In this its equal values, therefore it is ok.

Code for OR logical operators (Screenshot11):-

Logical Operators in php
Screenshot11

Output (Screenshot12):-

Screenshot12
In this example we have variables such as number , canbe1 and canbe2. Now I want to compare number which is equal to either canbe1 or canbe2.If statement evaluates false not ok because number is not equal to value of canbe1 nor canbe2.

Code for OR logical operators (Screenshot13):-

Code for OR Logical Operators
Screenshot13

Output (Screenshot14):-

Logical Operators
Screenshot14

In this example it will echo out “OK” if statement evaluates true.


Code for OR logical operators (Screenshot15):-

Logical Operators In php
Screenshot15

Output (Screenshot16):-

Logical Operators in php
Screenshot16
In this example it will echo out “OK” if statement evaluates true.


No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates