Conditional Statement

Following are the  conditional statement in PHP.
1 if
2 if else
3 nested if else

Syntax  1:

if(condition){
statement;
}

Note : In the above case if condition true then statement will execute otherwise execution will skip this part.

Example :
<?php
$a =100;
if($a>=100){ // If condition true
echo "Value of a=$a";
}
?>
Output : Value of a=100;

<?php
$a =100;
if($a<100){ // In this case condition fail thats why execution skip this block
echo "Value of a=$a";
}
?>
Output : Nothing

Syntax 2:

if(condition){
statement1;
}else{
statement2;
}
Note: In the above case if condition is true then if statement will execute otherwise else statement will execute.

Example 1 :
<?php

$a=100;
if($a!=100){
echo "Satatement1 : value of a=$a";
}else{
echo "Satatement2 : value of a=$a";
}
?>

Output : Statement2 : value of a = 100

Example 2 :
<?php

$a=99;
if($a!=100){
echo "Satatement1 : value of a=$a";
}else{
echo "Satatement2 : value of a=$a";
}
?>

Output : Statement1 : value of a = 99

Syntax 3 :

if(condition){

Statement1;

}elseif(condition){

Statement2;

}elseif(condition){

statement3;

}else{

statement4

}

Note : The above statement is nested statement so there is no limit of hierarchy so according to requirement we can either increase or decrease it .In this hierarchy if you have only conditional statement then there is no need to use default statement 'else'. In this case "else" statement is optional.

Example :

<?php

$marks = 40;
if($marks>=60){

echo "First division";

}elseif($marks<60 && $marks>=40){

echo "Second division";

}elseif($marks<40 &&  $marks>=30){

echo "Third division";

}else{

echo "Fail";

}

?>

Output : Second division

Note : So finally if you have a multiple conditions then you can manage your requirement using nested 'if else statement'.

 

16 thoughts on “Conditional Statement”

  1. Heya i’m for the first time here. I found this
    board and I find It truly useful & it helped me out much.
    I hope to give something back and aid others like you helped me.

  2. Hi terrific blog! Does running a blog such as this require
    a lot of work? I have virtually no expertise in coding however I had been hoping to start my own blog soon. Anyhow, should you have any recommendations or tips for new blog owners please share.
    I understand this is off topic nevertheless
    I simply had to ask. Many thanks!

  3. I believe everything posted made a great deal of sense. But, think on this, what if you added a little information? I am not saying your content is
    not solid., but what if you added a title that makes people want more?

    I mean Conditional Statement | PHP, JAVA,
    Spring Boot MYSQL, Javascript, Linux is kinda plain. You could peek at Yahoo’s home page and watch how they create news headlines to get people to open the links.
    You might add a video or a picture or two to get readers interested about everything’ve got to say.
    Just my opinion, it would make your website a little
    livelier.

  4. I think this is among the most important information for me.

    And i am glad reading your article. But want to remark on some general things, The web site style
    is ideal, the articles is really nice : D. Good job, cheers

  5. Good post. I study something tougher on different blogs everyday. It should at all times be stimulating to learn content from other writers and apply a little bit one thing from their store. I抎 prefer to make use of some with the content on my weblog whether or not you don抰 mind. Natually I抣l offer you a hyperlink on your web blog. Thanks for sharing.

  6. I抦 impressed, I have to say. Actually rarely do I encounter a weblog that抯 both educative and entertaining, and let me inform you, you could have hit the nail on the head. Your concept is outstanding; the difficulty is something that not enough individuals are speaking intelligently about. I’m very glad that I stumbled throughout this in my seek for something regarding this.

  7. A powerful share, I just given this onto a colleague who was doing a bit of analysis on this. And he actually bought me breakfast as a result of I discovered it for him.. smile. So let me reword that: Thnx for the deal with! However yeah Thnkx for spending the time to discuss this, I really feel strongly about it and love studying extra on this topic. If doable, as you change into expertise, would you mind updating your blog with more details? It is highly useful for me. Big thumb up for this weblog post!

  8. Hello there! Do you use Twitter? I’d like
    to follow you if that would be ok. I’m definitely enjoying your blog and look forward to new posts.

  9. I really like your blog.. very nice colors & theme.

    Did you design this website yourself or did you hire someone to do it for you?
    Plz respond as I’m looking to construct my own blog and would like
    to find out where u got this from. thanks a lot

  10. Good post. I learn something totally new and challenging on websites I stumbleupon everyday.
    It will always be helpful to read articles from other authors and use a little something
    from their websites.

  11. Thanks a lot for providing individuals with an extraordinarily remarkable opportunity to read from this blog. It is always very good and jam-packed with a lot of fun for me personally and my office peers to search your site at a minimum thrice in a week to learn the newest tips you have. And lastly, I am also actually satisfied concerning the breathtaking guidelines you serve. Certain 2 areas in this posting are clearly the simplest we have had.

Leave a Reply

Your email address will not be published. Required fields are marked *