PHP Conditionals


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'] == "Codevarsity"){ echo "You viewed the Codevarsity Page!"; }

Elseif

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

Else

<?php if ($_GET['name'] == "Codevarsity"){ echo "You viewed the Codevarsity Page!"; } elseif ($_GET['name'] == "Codevarsityguide"){ echo "You viewed the Codevarsity 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:

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.