Hướng dẫn if statement in php

I have a form which fetched inner variables. Original looks like this:

Nội dung chính

  • PHP if / else / elseif statement
  • Description
  • PHP: Tips of the Day
  • How do I do an if statement in PHP?
  • What are the 4 PHP conditional statements?
  • Can you put an if statement inside an if statement PHP?
  • Why conditional statements are used in PHP?

if ($something != 'Some-thing'{
    $code = ' 
'; return $code; } else { $code = '
'; return $code; }

It first checked if variable $something equals a value, if so then first, if not then second.

But then I wanted to add in new fields, with if/else statement to toggle the whole field.

Now, I already have a toggle on the back end which I click a checkbox and it changes $variable1 to on or off. See below:

screenshot of the back end

Then I add this input field which fetches another field to fill it in if it is toggled:


Now, I was having trouble putting the if statement inside, so I duplicated the whole field. Which was fine for just one extra variable.

But then I wanted to add a second field with an on/off toggle. And this is where things began to get messy.

Here is my code, but it doesn't seem very practical. And if I wanted to add another variable it would just get absurd.

if ($something != 'Some-thing' && $variable1 != "on" && $variable2 != "on") {
    $code = ' 
'; return $code; } elseif ($something != 'Some-thing' && $variable1 != "on" && $variable2 === "on") { $code = '
'; return $code; } elseif ($something != 'Some-thing' && $variable1 === "on" && $variable2 != "on") { $code = '
'; return $code; } elseif ($something != 'Some-thing' && $variable1 === "on" && $variable2 === "on") { $code = '
'; return $code; } else { $code = '
'; return $code; }

Above, variable_A is the value put into the back end for $variable1 as shown in the screenshot given above. Likewise, variable_B is the text value for $variable2 from the back end.

What is the best way to add the if statement inside? Or how can I revise my code for optimal practice? Ideally I would like to make it easy for me to add in more variables with fields inside the input, without having to have a messy complicated code.

PHP if / else / elseif statement

Last update on August 19 2022 21:50:40 (UTC/GMT +8 hours)

Description

The if statement execute a single statement or a group of statements if a certain condition is met. It can not do anything if the condition is false. For this purpose else is used.

Syntax:

if (condition)
   execute statement(s) if condition is true;
else
  execute  statement(s) if condition is false;

Example :

As we have initialized the $overtime value as 60, therefore the else statement will be executed.

View the example in the browser

PHP: elseif statement

Description:

elseif is a combination of if and else. It extends an if statement to execute a single statement or a group of statements if a certain condition is met. It can not do anything if the condition is false.

The following example display 'x is greater than y', 'x is equal to y' or 'x is smaller than y' depends on the value of $x or $y.

Example :

 $y) 
{
echo "x is bigger than y"; 
}
elseif ($x == $y)
{
echo "x is equal to y";
} 
else
{
echo "x is smaller than y";
}
?>

PHP: else statement

Description:

The if statement execute a single statement or a group of statements if a certain condition is met. It can not do anything if the condition is false. For this purpose else is used.

Syntax:

if (condition)

   execute statement(s) if condition is true;
  else
  execute  statement(s) if condition is false;

Example:

As we have initialized the $overtime value as 60, therefore the else statement will be executed.

View the example in the browser

PHP: elseif statement

Description:

elseif is a combination of if and else. It extends an if statement to execute a single statement or a group of statements if a certain condition is met. It can not do anything if the condition is false.

The following example display 'x is greater than y', 'x is equal to y' or 'x is smaller than y' depends on the value of $x or $y.

Example :

 $y) 
{
echo "x is bigger than y"; 
}
elseif ($x == $y)
{
echo "x is equal to y";
} 
else
{
echo "x is smaller than y";
}
?>

Previous: Incrementing Decrementing Operators
Next: while statement

PHP: Tips of the Day

PHP: Showing all errors and warnings

Display errors could be turned off in the php.ini or your Apache configuration file.

You can turn it on in the script:

error_reporting(E_ALL);
ini_set('display_errors', 1);

You should see the same messages in the PHP error log.

Ref : https://bit.ly/34di0ji

How do I do an if statement in PHP?

Syntax.

if (condition1){.

//code to be executed if condition1 is true..

} elseif (condition2){.

//code to be executed if condition2 is true..

} elseif (condition3){.

//code to be executed if condition3 is true..

} else{.

What are the 4 PHP conditional statements?

PHP 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.

Can you put an if statement inside an if statement PHP?

We can use if statements inside if statements. These statements are called Nested If Statements.

Why conditional statements are used in PHP?

PHP allows you to choose what action to take based on the result of a condition. This condition can be anything you choose, and you can combine conditions to make actions that are more complicated.