Php if statement syntax

PHP if. else. elseif Statements

Conditional statements are used to perform different actions based on different conditions.

PHP Conditional Statements

Very often when you write code, you want to perform different actions for different conditions. You can use conditional statements in your code to do this.

In PHP we have the following conditional statements:

  • if statement — executes some code if one condition is true
  • if. else statement — executes some code if a condition is true and another code if that condition is false
  • if. elseif. else statement — executes different codes for more than two conditions
  • switch statement — selects one of many blocks of code to be executed

PHP — The if Statement

The if statement executes some code if one condition is true.

Syntax

Example

Output «Have a good day!» if the current time (HOUR) is less than 20:

PHP — The if. else Statement

The if. else statement executes some code if a condition is true and another code if that condition is false.

Syntax

if (condition) code to be executed if condition is true;
> else code to be executed if condition is false;
>

Example

Output «Have a good day!» if the current time is less than 20, and «Have a good night!» otherwise:

if ($t < "20") echo "Have a good day!";
> else echo «Have a good night!»;
>
?>

PHP — The if. elseif. else Statement

The if. elseif. else statement executes different codes for more than two conditions.

Syntax

if (condition) code to be executed if this condition is true;
> elseif (condition) code to be executed if first condition is false and this condition is true;
> else code to be executed if all conditions are false;
>

Example

Output «Have a good morning!» if the current time is less than 10, and «Have a good day!» if the current time is less than 20. Otherwise it will output «Have a good night!»:

if ($t < "10") echo "Have a good morning!";
> elseif ($t < "20") echo "Have a good day!";
> else echo «Have a good night!»;
>
?>

PHP — The switch Statement

The switch statement will be explained in the next chapter.

Источник

PHP if

Summary: in this tutorial, you’ll learn about the PHP if statement and how to use it to execute a code block conditionally.

Introduction to the PHP if statement

The if statement allows you to execute a statement if an expression evaluates to true . The following shows the syntax of the if statement:

 if ( expression ) statement;Code language: HTML, XML (xml)

In this syntax, PHP evaluates the expression first. If the expression evaluates to true , PHP executes the statement . In case the expression evaluates to false , PHP ignores the statement .

The following flowchart illustrates how the if statement works:

PHP if flowchart

The following example uses the if statement to display a message if the $is_admin variable sets to true :

 $is_admin = true; if ($is_admin) echo 'Welcome, admin!';Code language: HTML, XML (xml)

Since $is_admin is true , the script outputs the following message:

Curly braces

If you want to execute multiple statements in the if block, you can use curly braces to group multiple statements like this:

 if ( expression ) < statement1; statement2; // more statement >Code language: HTML, XML (xml)

The following example uses the if statement that executes multiple statements:

 $can_edit = false; $is_admin = true; if ( $is_admin ) < echo 'Welcome, admin!'; $can_edit = true; >Code language: HTML, XML (xml)

In this example, the if statement displays a message and sets the $can_edit variable to true if the $is_admin variable is true .

It’s a good practice to always use curly braces with the if statement even though it has a single statement to execute like this:

 if ( expression ) Code language: HTML, XML (xml)

In addition, you can use spaces between the expression and curly braces to make the code more readable.

Nesting if statements

It’s possible to nest an if statement inside another if statement as follows:

 if ( expression1 ) < // do something if( expression2 ) < // do other things > >Code language: HTML, XML (xml)

The following example shows how to nest an if statement in another if statement:

 $is_admin = true; $can_approve = true; if ($is_admin) < echo 'Welcome, admin!'; if ($can_approve) < echo 'Please approve the pending items'; > >Code language: HTML, XML (xml)

Embed if statement in HTML

To embed an if statement in an HTML document, you can use the above syntax. However, PHP provides a better syntax that allows you to mix the if statement with HTML nicely:

 if ( expession) : ?>  endif; ?>Code language: HTML, XML (xml)

The following example uses the if statement that shows the edit link if the $is_admin is true :

html> html lang="en"> head> meta charset="UTF-8"> title>PHP if Statement Demo title> head> body>  $is_admin = true; ?>  if ( $is_admin ) : ?> a href="#">Edit a>  endif; ?> a href="#">View a> body> html>Code language: HTML, XML (xml)

Since the $is_admin is true , the script shows the Edit link. If you change the value of the $is_admin to false , you won’t see the Edit link in the output.

A common mistake with the PHP if statement

A common mistake that you may have is to use the wrong operator in the if statement. For example:

 $checked = 'on'; if( $checked = 'off' ) < echo 'The checkbox has not been checked'; >Code language: HTML, XML (xml)

This script shows a message if the $checke d is ‘off’ . However, the expression in the if statement is an assignment, not a comparison:

$checked = 'off'Code language: PHP (php)

This expression assigns the literal string ‘off’ to the $checked variable and returns that variable. It doesn’t compare the value of the $checked variable with the ‘off’ value. Therefore, the expression always evaluates to true , which is not correct.

To avoid this error, you can place the value first before the comparison operator and the variable after the comparison operator like this:

 $checked = 'on'; if('off' == $checked ) < echo 'The checkbox has not been checked'; >Code language: HTML, XML (xml)

If you accidentally use the assignment operator (=), PHP will raise a syntax error instead:

 $checked = 'on'; if ('off' = $checked) < echo 'The checkbox has not been checked'; >Code language: HTML, XML (xml)
Parse error: syntax error, unexpected '=' . Code language: JavaScript (javascript)

Summary

  • The if statement executes a statement if a condition evaluates to true .
  • Always use curly braces even if you have a single statement to execute in the if statement. It makes the code more obvious.
  • Do use the pattern if ( value == $variable_name ) <> to avoid possible mistakes.

Источник

PHP if else

Summary: in this tutorial, you’ll learn about the PHP if. else statement that executes a code block when a condition is true or another code block when the condition is false .

Introduction to PHP if-else statement

The if statement allows you to execute one or more statements when an expression is true :

 if ( expression ) < // code block >Code language: HTML, XML (xml)

Sometimes, you want to execute another code block if the expression is false . To do that, you add the else clause to the if statement:

 if ( expression ) < // code block > else < // another code block >Code language: HTML, XML (xml)

In this syntax, if the expression is true , PHP executes the code block that follows the if clause. If the expression is false , PHP executes the code block that follows the else keyword.

The following flowchart illustrates how the PHP if-else statement works:

The following example uses the if. else statement to show a message based on the value of the $is_authenticated variable:

 $is_authenticated = false; if ( $is_authenticated ) < echo 'Welcome!'; > else < echo 'You are not authorized to access this page.' >Code language: HTML, XML (xml)

In this example, the $is_authenticated is false . Therefore, the script executes the code block that follows the else clause. And you’ll see the following output:

You are not authorized to access this page.Code language: JavaScript (javascript)

PHP if…else statement in HTML

Like the if statement, you can mix the if. else statement with HTML nicely using the alternative syntax:

 if ( expression ): ?>  else: ?>  endif ?>Code language: HTML, XML (xml)

Note that you don’t need to place a semicolon ( ; ) after the endif keyword because the endif is the last statement in the PHP block. The enclosing tag ?> automatically implies a semicolon.

The following example uses the if. else statement to show the logout link if $is_authenticated is true . If the $is_authenticated is false , the script shows the login link instead:

html> html lang="en"> head> meta charset="UTF-8"> title>PHP if Statement Demo title> head> body>  $is_authenticated = true; ?>  if ($is_authenticated) : ?> a href="#">Logout a>  else: ?> a href="#">Login a>  endif ?> body> html>Code language: HTML, XML (xml)

Summary

Источник

PHP If…Else Statements

In this tutorial you’ll learn how to write decision-making code using if. else. elseif statements in PHP.

PHP Conditional Statements

Like most programming languages, PHP also allows you to write code that perform different actions based on the results of a logical or comparative test conditions at run time. This means, you can create test conditions in the form of expressions that evaluates to either true or false and based on these results you can perform certain actions.

There are several statements in PHP that you can use to make decisions:

We will explore each of these statements in the coming sections.

The if Statement

The if statement is used to execute a block of code only if the specified condition evaluates to true. This is the simplest PHP’s conditional statements and can be written like:

The following example will output «Have a nice weekend!» if the current day is Friday:

Example

The if. else Statement

You can enhance the decision making process by providing an alternative choice through adding an else statement to the if statement. The if. else statement allows you to execute one block of code if the specified condition is evaluates to true and another block of code if it is evaluates to false. It can be written, like this:

if (condition) <
// Code to be executed if condition is true
> else <
// Code to be executed if condition is false
>

The following example will output «Have a nice weekend!» if the current day is Friday, otherwise it will output «Have a nice day!»

Example

The if. elseif. else Statement

The if. elseif. else a special statement that is used to combine multiple if. else statements.

if (condition1) <
// Code to be executed if condition1 is true
> elseif (condition2) <
// Code to be executed if the condition1 is false and condition2 is true
> else <
// Code to be executed if both condition1 and condition2 are false
>

The following example will output «Have a nice weekend!» if the current day is Friday, and «Have a nice Sunday!» if the current day is Sunday, otherwise it will output «Have a nice day!»

Example

You will learn about PHP switch-case statement in the next chapter.

The Ternary Operator

The ternary operator provides a shorthand way of writing the if. else statements. The ternary operator is represented by the question mark ( ? ) symbol and it takes three operands: a condition to check, a result for true , and a result for false .

To understand how this operator works, consider the following examples:

Example

Using the ternary operator the same code could be written in a more compact way:

Example

The ternary operator in the example above selects the value on the left of the colon (i.e. ‘Child’) if the condition evaluates to true (i.e. if $age is less than 18), and selects the value on the right of the colon (i.e. ‘Adult’) if the condition evaluates to false.

Tip: Code written using the ternary operator can be hard to read. However, it provides a great way to write compact if-else statements.

The Null Coalescing Operator PHP 7

PHP 7 introduces a new null coalescing operator ( ?? ) which you can use as a shorthand where you need to use a ternary operator in conjunction with isset() function.

To uderstand this in a better way consider the following line of code. It fetches the value of $_GET[‘name’] , if it does not exist or NULL , it returns ‘anonymous’.

Example

Using the null coalescing operator the same code could be written as:

Example

As you can see the later syntax is more compact and easy to write.

Bootstrap UI Design Templates

Is this website helpful to you? Please give us a like, or share your feedback to help us improve. Connect with us on Facebook and Twitter for the latest updates.

Источник

Читайте также:  Background image css auto width
Оцените статью