freeCodeCamp/guide/english/php/conditionals/index.md

1.2 KiB

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: