PHP CONDITIONS AND DECISIONS


 

What is Conditional Statement.

Before you can make a decision, there must be a condition or conditions you need to decide from. Conditional statement allow you to make decisions based on the values of variables or the result of comparisons. Conditional statement are fundamental programming constructs that allow you to control the flow of your program based on a certain condition.

Note you can make use of just the opening PHP tag alone if your webpage contains nothing but just only PHP codes without HTML code, a good example of such webpage is the logout page of an application.

Types of Condition

  1. if statement
  2. if else statement
  3. if else if statement

If statement

The if statement allows you to execute a block of code if a certain condition is true. Here's the basic syntax:

if (condition){

            //block of code

}

Explanation

The condition can be any expression that evaluates to a Boolean value (True or False). If the condition is True, the code block indented below the if statement will be executed. If the condition is False, the code block will be skipped.

Example 1: Write a PHP script that will check if first number is greater than the second number

<?php

     $number1=200

    $number2=50

    if($number1 > $number2){

        echo "$number 1 is greater than $number2";

    }



Output




Ameh Joseph Onyeke

Hello and welcome to my blog! My name is Ameh Joseph Onyeke a passionate educator and tech enthusiast with a BSc in Computer Science from Benue State University, a PGDE from the University of Maiduguri, and a Master’s in Software Engineering from Bayero University Kano. My journey blends a strong foundation in computing with a love for teaching, aiming to simplify complex tech concepts for learners at all levels. Through this blog, I provide answers and insights across 10+ programming languages to support students and tech enthusiasts in building their skills and confidence. Whether you're solving coding problems or sharpening your programming abilities, this blog is here to guide you. Let’s learn and grow together!

Post a Comment

Previous Post Next Post