PHP conditions - ProgrammerTech
Language:

Search

PHP conditions

  • Share this:
PHP conditions

Condition PHP

Conditions are used to implement parts of the program only when they meet the requirements. In the previous lesson, we talked about episodes, and as we said in that lesson, episodes can be used for certain parts of the program a specified number of times or until the condition is met. Today we are going to talk about conditions and how to command a program to implement a specific condition.

 

Conditional statement if

It is one of the php functions, and this function enables us to implement a specific condition in the source code or program with a specification of its own book. We know that the programming world is about executing code until the condition, which is the program, is executed. For example, if the number is larger, print a specific thing, and if it is not, print a specific thing.

The general form of an if statement in php

if (condition){
    statement
}

whereas:

  1. The condition represents a condition.
  2. The commands to be executed or the condition statement.
<?php 
    $k = 2;
    if($k > 1){
        echo "<p> variable $k is greater than 1</p>";
    }
?>

We create a variable named k that saves data on the number 2 and then uses if to implement a certain condition. If k is greater than 1 print variable $k is greater than 1, then the condition will be printed because k is the number that actually belongs to it greater than 1. The data that k is saving is 2 and it will be printed on the server variable 2 is greater than 1.

When we run the above code, we will get the result

variable 2 is greater than 1

This condition is not smart, many ways can be added through that condition and this is what we will learn about in this lesson. Just bring a paper and a pen and remember everything you will read here in the article, I hope it helps everyone.

 

Conditional statement else

The second type of condition is eles where you can perform the inverse of the condition. If the condition returns false, that is, the condition is not met, in which case you can specify certain things within the source code or the code. For example, if the first condition is not met, or if the data you save k is less than 1, you can use else.

General form of else in php

if (condition)
{
Statement
}
else
{
Statement
}

First, if is to execute the condition, then else is the opposite of the condition.

example 1

<?php 
    $k = 4;
    if ($k > 5){
        echo "<p>variable $k is greater than 1 </p>";
    }
    else
    {
        echo "sorry";
    }
?>

We create a variable named k that holds the number 4 from the data and then we use if to implement a certain condition. If k is greater than 5, print the value of variable $k. So the condition will be printed because k is its number actually greater than k and if k is less than the condition. It will print sorry and in this example it will print sorry because the value of k is less than the condition statement.

sorry

Example 2

<?php
    if(date("Y") == 2019){
        echo "<p>Now it's spring!</p>";
    }
    else{
        echo "<p>I do not know what season it is!</p>";
    }
?>

We say here if the current year is 2019 it prints us the statement Now it is spring through the date function, and how to deal with time and date was explained in the fifth lesson. And here the condition will be executed if the current year is 2019 and then we use else if the condition is not executed if the current year is not 2019 prints us I do not know what it is.

 

Operators PHP

  1. The equal sign ( == ).
  2. The inequality sign ( =! ).
  3. greater than sign ( < ).
  4. Less than sign ( > ).
  5. The greater than or equal to sign ( =< ).
  6. Less than or equal to sign ( => ).

 

Logical Operators

  • The operator (&&) means and.
  • The operator (||) means or.
  • The operator (!!) means not.

There are many ways to improve the condition and increase its accuracy and as we mentioned factors that can be used to develop more accurate conditions. The example above can be expanded to include all spring months.

<?php
    if(date("m") >= 3 && date("m") <= 6 ) {
        echo "<p> Now it's spring!</p>";
    }
    else{
        echo "<p> Now it's either winter, summer or autumn!</p>";
    }
?>

The improved condition date("m") >= 3 && date("m") <= 5 can be translated automatically, if the month is greater than or equal to 3 and the month is greater than or equal to 5. This condition is clever, factors play a role important in php. Is it not, but the condition is still not strong. Let us develop the condition further so that the rest of the months mentioned are in the year.

<?php
        if(date("m") >= 3 && date("m") <= 5 ) {
            echo "<p> Now it's spring!</p>";
       }
        elseif(date("m") >= 6 && date("m") <= 8) {
            echo "<p> Now it's summer! </p>";
       }
        elseif (date("m")> = 9 && date("m") < = 11) {
            echo "<p>Now it's autumn! </p>";
        }
        else{
            echo "<p>Now is winter!</p>";
        }
?>

 

Conditional statement elseif

Using the elseif function combined with the if main function, a set of conditions can be specified. To cover the rest of the year in the previous example, this function was used in the second condition.

elseif ((date("m") >= 6 && date("m") <= 8

If the month is greater or equal to 6 and the month is less or equal to 8, the condition will be executed. The same is true with the third condition

elseif (date("m") >= 9 && date("m") <= 11

This can also be translated if the month is greater or equal to 9 and the month is less or equal to 11, the condition will be executed with the addition of else. This is how all months of the year were mentioned, I think it is clear.

else{
echo "<p>Now is winter!</p>";
}

To write the terms you have to think logically and be methodical. In all of the previous examples a set of conditions was created from easy mode to hard mode. There is another way to write the conditions or another function, which is the switch case in its general form.

switch (expression)
{
    case 1 :
      statement
      breack;

    case 2 :
      statement
      breack;

    default
      statement
      breack;
}

This method is based on the condition or expession and then lists various answers and values ​​through statements. In order not to complicate matters more, I always say examples are the best thing to teach a student well. As we know in the lesson dealing with time and date, the php function ("date("w) gives us the number of the day of the week, let's use that in an example.

<?php
    switch(date("w")
    {
        case 1 :
            echo "Now it's Monday";
            breack;
        case 2 :
            echo "Now it's Tuesday";
            breack;
        case 3 :
            echo "Now it's Wednesday";
            breack;
        case 4 :
            echo "Not it's Thursday";
            breack;
        case 5 :
            echo "Not it's Friday";
            breack;
        case 6 :
            echo "Not it's Saturday";
            breack;
        default :
            echo "Not it's Sunday";
            breack;
    } 
?>

Don't worry, dear trainee, it's not difficult. You can use switch when executing a large set of conditions (("switch(date("w). The condition is made here and through case you can specify the condition you want.

case 1 :
    echo "Now it's Monday";
breack;

If today is Monday, that condition is met, and the program will be suspended. Likewise with any other case, the function of the case is to create the conditions that you want to achieve, the second condition is done with the same procedure as the first condition.

You can liken this to the other functions if and else and elseif that do switch like if and case like elseif and default. It works like else if the condition is not true as well as break The program stops after the condition has been executed.

Tell us in the comments what you learned in this lesson, we will be glad to hear your feedback and see what you learned from the lesson


Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com