Conditional Statements And Logical Operators in PHP

PHP & MySQL Course: Conditional Statements And Logical Operators in PHP

In the previous article we learnt about the operators of PHP and used those in different ways with variables the lecture 4 part B is now continue.

Lecture 4 (Part-B):Conditional Statements And Logical Operators in PHP

In this article I continue the remaining topics of lecture 4. It was getting time and being so long that’s why I break it in two parts. By which you can understand them easily. Because now we in going to ahead in PHP & MySQL gradually.

  1. Use of Operators in PHP
  2. Use of Conditional statements in PHP

Logical Operators in PHP:

The usage of the Logical Operators in PHP are same as in C++ and Java programming languages. There is no any different methodology to use them in PHP. And there is no any extra Logical operator in PHP. These are same as in C++ and Java or other programming languages. For details about the logical operators see the table 1 for their use. But we use them also in English words also.

Table 1  Logical Operators in PHP

Name of Operators

Symbol

Description

Use in PHP

And Operator

and

Returns true when both are true

$a and $b

Or Operator

or

Returns true when any one is true

$a or $b

Xor Operator

xor

Returns true when any one is true, not both

$a xor $b

And Operator

&&

Returns true when both are true

$a && $b

Or Operator

||

Returns true when any one is true

$a || $b

Not Operator

!

Returns true when variable values is false

!$b

Comparison Operators in PHP:

We use multiple operators in PHP for comparing the data. Because it is very important to compare the data which type of data is coming from the user. There are multi-types of websites. And they need to compare data and then proceed the process. Without comparing the data, it is very difficult to give good facility to the user. And also it will be a difficult job to secure the website from the hackers. See the table 2 for more details.

Table 2  Comparison Operators in PHP & MySQL

Name of Operators

Symbol

Description

Use in PHP

Equal Operator

==

It compares two variables conditions

$a == $b

Identical Operator

===

It compares two variables conditions with stress

$a === $b

Not Equal Operator

!=

It compares the valid values of two variables

$a != $b

Not Equal Operator

<>

It compares the valid values of two variables

$a <> $b

Not Identical Operator

!==

It compares the valid values of two variables with stress

$a !== $b

Greater Than Operator

>

It checks the largest value

$a > $b

Less Than Operator

<

It check the smallest value

$a < $b

Greater Than & Equal to Operator

>=

It compares and checks the largest value

$a >= $b

Less Than & Equal to Operator

<=

It compares and checks the smallest value

$a <= $b

Array Operators in PHP:

There are some operators in PHP which we use to compare the arrays in PHP. Sometimes we need to compare the data of the operators and also sometimes we need to compare their keys. It is a very complicated task, to compare the one array data or key of array with other array or key of array. Check the table 3 for details about the array operators in PHP programming which are being used in web pages.

Table 3 Array Operators in PHP

Name of Operators

Symbol

Description

Use in PHP

Union Operator

+

It takes the Union of two arrays if there is no any duplicate key

$a + $b

Equality Operator

==

It takes the Equality to check the keys are same or not

$a == $b

Identity Operator

===

It checks the Identity of two arrays with their keys.

$a === $b

Inequality Operator

!=

It checks the equality and return true when not equal with each other

$a != $b

Inequality Operator

<>

It checks the equality and return true when not equal with each other

$a <> $b

Non-identity Operator

!==

Its check the Identity of two arrays with their keys

$a !== $b

Conditional Statements in PHP:

In conditional statements we will discuss the following statements.

  1. If(){…} Statement in PHP
  2. If(){…} else{…} Statement in PHP
  3. If(){…} elseif(){…} else{…}Statement in PHP
  4. Switch() {…} Statement in PHP

Description of Conditional Statements in PHP:

We use the first statement “If() {…}” when we are just comparing two variables or checking the Boolean (True or False) conditions of any variable. And we use the 2nd conditional statement to do same as we use If(){…} but it will go to else {…} condition when it is false, or which we want from the If(){…} condition but it not happen so it will do else{…} condition command.

And we use if(){…}elseif(){…}, we want to do multiple comparisons in a single program of PHP. It will work continuously by checking the if(){…} statement with else{…} condition.

And we use switch(){…} conditional statement in PHP for same purpose. But it just once check the operation in switch() statement and then check different conditions in the block. Using of switch is very simple than If(){…}, if(){…}else{…} and if(){…} elseif(){…} conditional statement. Check listing 1,2,3 & 4 for details about Conditional Statements.

Syntax:

If(condition) {statement}

If(condition) {statement} else {statement}

If(condition) {statement} elseif(condition){statement} else {statement}

Switch(operation){case 1, case 2 … deafualt}

Listing 1: If(){…} Statement


<?

$a = 3;

$b = 4;

if($a < $b)

{

echo "$a is less than $b";

}

?>

Listing 2: If(){…} else{…} Statement


<?

$a = 4;

$b = 4;

if($a < $b)

{

echo "$a is less than $b";

}

else

{

echo "$a is equal to $b";

}

?>

Listing 3: If(){…} elseif{…} else Statement


<?

//using if elseif condition statement for check the smallest number

$a = 1;

$b = 2;

$c = 3;

if($a > $b && $a > $c)

{

echo "$a is the largest number";

}

elseif ($b > $a && $b > $c)

{

echo "$b is the largest value";

}

elseif($c > $b && $c > $a)

{

echo "$c is the largest value";

}

else

{

echo "$a ,$b & $c are equal to each other";

}

?>

Listing 4: Switch(){…} Statement


<?

$a = 4;

$b = 3;

switch($a % $b == 1)

{

case True:

echo "<br>The Remainder of $a and $b is 1";

break;

case False:

echo "<br>The Remainder of $a and $b is not 1";

break;

default:

echo "<br>Something gone wrong in Calculation... Sorry!!!";

}

?>

Listing 5: Array Operators with Conditional Statement


<?

$a = array("a" => "C++", "b" => "Java");

$b = array("c" => "PHP", "d" => "MySQL");

$c = $a + $b; // union of $x and $y

if($a == $b)

{

echo "Equal, Keys or Pair values are same...";

}

else

{

echo "Not Equal";

}

echo "<br>";

if($a === $b)

{

echo "Equal, The Identity of keys are True...";

}

else

{

echo "Not Equal";

}

echo "<br>";

if($a <> $b)

{

echo "Its Ture, both are not equal...";

}

else

{

echo "Equal";

}

echo "<br>";

if($a !== $b)

{

echo "Equal, Their Keys Identity is True.";

}

else

{

echo "Not Equal";

}

?>

Summary:

In lecture 4, we learnt about two most important topics first operators. In operators we learnt about different types of operators as arithmetic, assignment, increment and decrement, comparison, logical, and also those operators which used in arrays called array operators.

And we also discussed the conditional statements. We use them in different programs using PHP programming syntax.

admin
 

student of computer Science , having interest and experience in C++,Java,Php,Html,Css,JavaScript, wordpress and android development