Playing With Conditional statement in PHP

Md. Abdullah Al Mamun.

Follow us: Click Here

Our YouTube Channel: FriendzTube

Today I will discuss about the conditional statement in PHP. Like other language PHP allows you to make decision based on the some condition.

There are several conditional statements in PHP that you can use to make decisions:

  • The if statement
  • The if else statement
  • The if elseif else statement
  • The switch case statement
  •  Ternary operator

Let us demonstrate some example:

If condition

if ($x == 2) {

print '$x is equal to 2';

} else if ($x == 1) {

print '$x is equal to 1';} else {
print '$x does not equal 2 or 1';

If, elseif, else condition,

if ($x == 2) {

print '$x is equal to 2';

} else if ($x == 1) {

print '$x is equal to 1';}

Switch case <?php
$var = "yes";
switch ($var) {
case "yes":
print '$var is equal to yes';
break;case "no":
print '$var is equal to no';
Ternary operator <?php
$x = 1;
($x==1)?(echo 'x is 1'):(echo 'x is not 1');

Few days ago I posted on facebook two picture those are bellows,

The output Of this code is : "Hello I am PHP".

The Output of this code is :"Hello condition 1 is true".

In first program, we notice that the initial value of x is 1. then by ++$x oparetor it adds 1 with x. then x will 2. So the first condition is true. After executing the code inside the If condition x will 3 but else condition would not true.

According to the Conditional Statement If one condition is true then the other condition is false.
Then in the second program both if condition and else if  condition is true.

But a question is arise which condition is allowed both of them.

The answer is very easy. It will be first true condition always. 

I  think everybody understand this.
If you have any question, Please comment bellow.

Follow us: Click Here

Our YouTube Channel: FriendzTube

Level 0

আমি মোঃ আব্দুল্লাহ আল মামুন। বিশ্বের সর্ববৃহৎ বিজ্ঞান ও প্রযুক্তির সৌশল নেটওয়ার্ক - টেকটিউনস এ আমি 3 বছর 7 মাস যাবৎ যুক্ত আছি। টেকটিউনস আমি এ পর্যন্ত 4 টি টিউন ও 0 টি টিউমেন্ট করেছি। টেকটিউনসে আমার 1 ফলোয়ার আছে এবং আমি টেকটিউনসে 0 টিউনারকে ফলো করি।


আরও টিউনস

টিউনারের আরও টিউনস