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

Contenido del Curso

Introduction to PHP

Introduction to PHP

1. First Acquaintance
2. Variables and Data Types
3. Conditional Statements
4. Arrays
5. Loops

If 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


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:

php

main

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

php

main

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

Tarea

Complete the code to create a program that checks the price of a chocolate bar and prints the appropriate message. If the price of the chocolate bar is less than or equal to 3, the program should print "This chocolate bar has an average price". If the price of the chocolate bar is greater than 3, the program should print "This chocolate bar is expensive".

Tarea

Complete the code to create a program that checks the price of a chocolate bar and prints the appropriate message. If the price of the chocolate bar is less than or equal to 3, the program should print "This chocolate bar has an average price". If the price of the chocolate bar is greater than 3, the program should print "This chocolate bar is expensive".

¿Todo estuvo claro?

Sección 3. Capítulo 1
toggle bottom row

If 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


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:

php

main

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

php

main

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

Tarea

Complete the code to create a program that checks the price of a chocolate bar and prints the appropriate message. If the price of the chocolate bar is less than or equal to 3, the program should print "This chocolate bar has an average price". If the price of the chocolate bar is greater than 3, the program should print "This chocolate bar is expensive".

Tarea

Complete the code to create a program that checks the price of a chocolate bar and prints the appropriate message. If the price of the chocolate bar is less than or equal to 3, the program should print "This chocolate bar has an average price". If the price of the chocolate bar is greater than 3, the program should print "This chocolate bar is expensive".

¿Todo estuvo claro?

Sección 3. Capítulo 1
toggle bottom row

If 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


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:

php

main

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

php

main

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

Tarea

Complete the code to create a program that checks the price of a chocolate bar and prints the appropriate message. If the price of the chocolate bar is less than or equal to 3, the program should print "This chocolate bar has an average price". If the price of the chocolate bar is greater than 3, the program should print "This chocolate bar is expensive".

Tarea

Complete the code to create a program that checks the price of a chocolate bar and prints the appropriate message. If the price of the chocolate bar is less than or equal to 3, the program should print "This chocolate bar has an average price". If the price of the chocolate bar is greater than 3, the program should print "This chocolate bar is expensive".

¿Todo estuvo claro?

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


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:

php

main

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

php

main

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

Tarea

Complete the code to create a program that checks the price of a chocolate bar and prints the appropriate message. If the price of the chocolate bar is less than or equal to 3, the program should print "This chocolate bar has an average price". If the price of the chocolate bar is greater than 3, the program should print "This chocolate bar is expensive".

Sección 3. Capítulo 1
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt