PHP Control Structures 1 - if and else
Written by Mike James   
Thursday, 08 November 2018
Article Index
PHP Control Structures 1 - if and else
Compound statements

A small example

The if statement is the core of most PHP programs you simply cannot avoid it. To give you one final simple example consider how you might customize a web page according to the gender of the user. Suppose that we have a variable $Gender which is set to "Male" or "Female" by some other part of the program according to data the user has entered. Using this variable to customize the web page is simple:

 if( $Gender== "Male"){
echo "Welcome Master";
echo "Welcome Mistress" ;

Notice that this only works because $Gender, traditionally, can only be Male or Female – no additional test is carried out to make sure that the variable is indeed set to Female. This is the sort of thing that often occurs in programming. If you want to be 100% sure that your program will still work if someone introduces a third category for gender – then you need to write two if statements:

 if( $Gender== "Male"){
echo "Welcome Master";
if( $Gender== "Female"){
echo "Welcome Mistress" ;

Of course to deal with a third, or fourth category you would need additional if statements.

Building programs with ifs

If you are getting used to the idea of following the flow of control you should be able to see that building a more complicated program is mostly a matter of building more a complicated flow of control. You can think of it almost as if you were building a model railway set, putting together a track that branches at each if and joins up again so that each branch of the if continues on the single main line – the default flow of control.

With just the if and the if-else statements as introduced in this article you can build any shape of railway track you need to. That is, you don't really need to learn any other versions of the if statement. However PHP offers additional conditional statements – the elseif and switch. Don't move on to these until you have mastered the simple if and feel confident that given any program you can trace and understand the flow of control.

When you are ready, move on to more advanced conditional statements.

Introduction to PHP



  1. Getting started
    Getting Started With NetBeans PHP - Local Projects
  2. What PHP Does
  3. PHP Variables and Expressions for Complete Beginners
  4. PHP Control Structures 1 - if and else
  5. PHP Control Structures 2 - switch and elseif
  6. PHP loops
  7. Advanced loops
  8. Functions
  9. Ten minutes to PHP objects
  10. PHP Inner Functions And Closure
  11. NetBeans Remote Projects and debugging


To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.


Ten minutes To PHP Objects

PHP is a fully object-oriented language with lots of powerful features. This introductory guide looks at how PHP handles objects.

PHP Inner Functions And Closure

PHP inner functions and anonymous functions are a little strange to say the least. However, just because something is strange doesn't mean that it isn't useful. We take a close look at the way PHP fun [ ... ]

Other Articles




or email your comment to:








Last Updated ( Thursday, 08 November 2018 )