PHP: Boolean Operators I

Derek Bridge

Department of Computer Science,
University College Cork

PHP: Boolean Operators I

Aims:

Ifs within ifs

Class exercise: What is the output of the following?

$age = 23;
$nationality = 'Nerd';

if ( $age > 18 )
{
    echo '<p>You are able to pay tax in Nerdland</p>';
    if ( $nationality == 'Nerd' )
    {
        echo '<p>You are able to stand for election in Nerdland</p>';
    }
}

Also, what if the person is a 17 year old Nerd? Or a 17 year old Geek? Or a 23 year old Geek?

Common error

Class exercise

Incomplete version of al_outcome.php

$cont = (int) $_GET['cont'];
$exam = (int) $_GET['exam'];

$message = '<p>You have failed</p>';
if



        
    

echo $message;

Boolean operators: &&

Another incomplete version of al_outcome.php

$cont = (int) $_GET['cont'];
$exam = (int) $_GET['exam'];

$message = '<p>You have failed</p>';
if 

        

echo $message;

Boolean operators: &&

expr1expr2expr1 && expr2
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

See

Class exercise

A version of wc_outcome.php that uses conjunction

$cont = (int) $_GET['cont'];
$exam = (int) $_GET['exam'];

$message = '<p>You have passed</p>';
if ( $cont < 40 && $exam < 40 )
{
    $message = '<p>You have failed</p>';
}
echo $message;

Incomplete version of wc_outcome.php

$cont = (int) $_GET['cont'];
$exam = (int) $_GET['exam'];

$message = '<p>You have failed</p>';
if 

    



    

echo $message;

Boolean operators: ||

Another incomplete version of wc_outcome.php

$cont = (int) $_GET['cont'];
$exam = (int) $_GET['exam'];

$message = '<p>You have failed</p>';
if 

        

echo $message;

Boolean operators: ||

expr1expr2expr1 || expr2
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

See

Comments

<!-- This is how comments are done in HTML -->

/* This is how comments are done in CSS */

/* It is also one way that comments are done in PHP */

#  But this is another way: from the hash to the end of the line

// And this is a third way: from the slashes to the end of the line is a comment

-- This is a common way in SQL (but using /* */ and # are also allowed)