Files
freeCodeCamp/guide/english/php/conditionals/index.md
Dhiraj Kanchan 022ffb21b2 Fix syntax to use equal to operator instead of assignment operator. (#24467)
Fix if statement to use Double equals (==)  instead of Single equal  (=) which would overwrite the value of $_GET['name'] variable.
2018-12-12 05:37:07 -05:00

1.2 KiB

title
title
Conditionals

Conditionals

Conditionals in PHP are written using the if, elseif, else syntax. Using conditionals allows you to perform different actions depending on different inputs and values provided to a page at run time. In PHP conditionals are often referred to as control structures.

If

<?php
if ($_GET['name'] == "freecodecamp"){
  echo "You viewed the freeCodeCamp Page!";
}

Elseif

<?php
if ($_GET['name'] == "freecodecamp"){
  echo "You viewed the freeCodeCamp Page!";
} elseif ($_GET['name'] == "freecodecampguide"){
  echo "You viewed the freeCodeCamp Guide Page!";
}

Else

<?php
if ($_GET['name'] == "freecodecamp"){
  echo "You viewed the freeCodeCamp Page!";
} elseif ($_GET['name'] == "freecodecampguide"){
  echo "You viewed the freeCodeCamp Guide Page!";
} else {
  echo "You viewed a page that does not exist yet!";
}

Note

In cases where you have a lot of possible conditions you may want to use a Switch Statement.

More Information: