Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn If Statement | Conditional Statements
Introduction to PHP

bookIf Statement

An if statement is a construct that allows you to execute a code block if a specific condition is met.

The condition is an expression that evaluates to a bool value, which can be true or false. If the condition is true, the block of code is executed. If the condition is false, the block of code is skipped.

Syntax

if (condition) {
    // code block
};

The syntax of the conditional operator is so simple: if keyword, condition in the parentheses ( ) and a code block in the curly brackets { }.

The open curly bracket { } opens the code block, and the close curly bracket symbol closes it. Let's consider a example:

main.php

main.php

copy
123456
<?php $num=5; if ($num>0) { // 5 > 0 ? echo "Number is positive"; // output if it's `true` } ?>

The condition was confirmed and the block of code worked. Let's consider a different example:

main.php

main.php

copy
123456
<?php $num = 10; if ($num < 0) { // 10 < 0 ? echo "Number is negative"; // output if it's `true` } ?>

The condition was not confirmed, and the code block did not work.

Task

Swipe to start coding

Imagine you are creating a small program for a candy shop. The program should help customers quickly understand whether a chocolate bar is expensive based on its price.

  1. The variable $price_of_chocolate is already initialized with a numeric value.
  2. Use an if statement to check if the price is less than or equal to 3.
    • If true, print "This chocolate bar has an average price.".
  3. Use another if statement to check if the price is greater than 3.
    • If true, print "This chocolate bar is expensive.".

Make sure the messages are displayed exactly as shown, including punctuation.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

Awesome!

Completion rate improved to 4.35

bookIf Statement

Swipe to show menu

An if statement is a construct that allows you to execute a code block if a specific condition is met.

The condition is an expression that evaluates to a bool value, which can be true or false. If the condition is true, the block of code is executed. If the condition is false, the block of code is skipped.

Syntax

if (condition) {
    // code block
};

The syntax of the conditional operator is so simple: if keyword, condition in the parentheses ( ) and a code block in the curly brackets { }.

The open curly bracket { } opens the code block, and the close curly bracket symbol closes it. Let's consider a example:

main.php

main.php

copy
123456
<?php $num=5; if ($num>0) { // 5 > 0 ? echo "Number is positive"; // output if it's `true` } ?>

The condition was confirmed and the block of code worked. Let's consider a different example:

main.php

main.php

copy
123456
<?php $num = 10; if ($num < 0) { // 10 < 0 ? echo "Number is negative"; // output if it's `true` } ?>

The condition was not confirmed, and the code block did not work.

Task

Swipe to start coding

Imagine you are creating a small program for a candy shop. The program should help customers quickly understand whether a chocolate bar is expensive based on its price.

  1. The variable $price_of_chocolate is already initialized with a numeric value.
  2. Use an if statement to check if the price is less than or equal to 3.
    • If true, print "This chocolate bar has an average price.".
  3. Use another if statement to check if the price is greater than 3.
    • If true, print "This chocolate bar is expensive.".

Make sure the messages are displayed exactly as shown, including punctuation.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1
single

single

some-alt