Quakenet/#php Tutorial

Note: If you opened this page from an external URL pay attention that all chapters are linked together. Be sure you also read all prior chapters of this tutorial, otherwise you will miss relevant content explained before.

Control structures

  1. Datatype boolean
  2. Control structures
  3. Alternative execution
  4. Errors you may get with if();

1. Datatype boolean

In PHP there is a datatype which may use only one single bit. So this datatype can only be used to save just two possible values 0 and 1. In the context of programming languages you normally speak of about true (for 1) and false (for 0). This datatype is used to say if something is true or something is false

PHP uses the codes true and false to get the corresponding boolean values. These codes are case-insensitive, however you normally write them in lowercase.

<?php
$var 
true;
$var2 false;
$var3 TRUE;
$var4 FaLsE;

$var5 'true'// this is a 4 character long string with the content "true", its not
                // the boolean value true
?>

This datatype is use for control structurs. With these you control which programm code is executed and which not.

2. Control structures

You can control the programm flow with control structures. You can control with these if a programm block is executed or not. You can build such a control structure with the language construct if

<?php
if (expressionstatement
?>

The expression is calulated and must return a value. This value is checked for equivalent agains true. If this is the case the following statement gets executed. If you want to control more than one statement with one if you must use curly brackets to put the statements together.

<?php
if (expression) {
    
statement_1;
    
statement_2;
    
// ...
    
statement_n;
}
?>

The expression to check can be build with several expression together if you use operations like and or !.

3. Alternative execution

If you write some code with an if statement you often wants to execute some code if the if statement is not executed. An example can be to show an admin menu if the login is valid otherwise show an error message. You can write such an alternative execution with the keyword else which must belong to a previous if statement.

<?php
if (login_valid) {
    
// show admin menu
} else {
    
// show error message
}
?>

You can also copy the if statement and negate the expression with !.

<?php
if (login_valid) {
    
// show admin menu
}
if (!
login_valid) {
    
// show error message
}
?>

This is however inadvisable and its sometimes not even possible to write.

You even can extend such an if-else with elseif structurs. This is a block between the if and else block which also contains an expression to check.

<?php
if (condition) {
    
// do this
} elseif (another_condition) {
    
// do that
} else {
    
// do at least this
}
?>

The elseif part is executed/checked if the previosly if check gets false. You can even write more than one elseif structures.

<?php
if (condition) {
    
// do this
} elseif (another_condition) {
    
// do that
} elseif (and_another_condition) {
    
// and do this
} elseif (expression) {
    
// php-code
} else {
    
// do at least this
}
?>

The condition which is first true gets executed. If no condition at all is true the else part gets executed.

4. Errors you may get with if();

As an if () looks like a function call it isn't one. A semicolon at the end isn't not just useless, it also breaks the correct working of an if statement. Check the following example.

<?php
if (false); // <-- notice the ; at the end
    
echo 'Foobar';
?>

Althought the condition is false the echo statement gets executed anyway. The if statement controls only the next statement of the next block written with curly brackets. But in this case its not the echo 'Foobar'; line but the single ; behind the if(). The code looks actually more like this:

<?php
if (false)
    ;
echo 
'Foobar';
?>

Or be more clear:

<?php
if (false) {
    ;
}
echo 
'Foobar';
?>

As you see the if statment just controls an empty statement, independent of the following echo statement.

Questions about the chapter

No questions

Back to Next to
Copyright © to the OPs of #php/QuakeNet Valid XHTML 1.0 Strict Valid CSS!