Aims:
if $age < 18
{
echo 'You are too young to study CS1109';
echo 'Come back when you\'re older.';
}
if ( $age < 18 );
{
echo 'You are too young to study CS1109';
echo 'Come back when you\'re older.';
}
if ( $age < 18 )
echo 'You are too young to study CS1109. ';
echo 'Come back when you\'re older.';
if ( $age < 18 )
{
echo 'You are too young to study CS1109. ';
echo 'Come back when you\'re older.';
}
if ( $age > 18 )
{
echo 'Welcome to CS1109.';
}
if ( $age = 18 )
{
echo 'You are the perfect age to study CS1109.';
}
2 * 3 evaluates to 6=) is also an operator
$age = 18
$ageecho $age = 18;echo ($my_age = 19) + 1;false and true
The following all convert to false |
The following all convert to true |
|---|---|
|
0 0.0 '' (empty string) '0' An array with zero elements An object with no values or functions NULL
|
All other values |
$x = 1;
$y = '';
if ( $x )
{
echo 'Whoo hoo!';
}
if ( $y )
{
echo 'Whey hey!';
}
if ( $z )
{
echo 'Yee haa!';
}
if ( $age = 18 )
{
echo 'You are the perfect age to study CS1109.';
}
<form action="colour.php" method="get">
<fieldset>
<legend>Guess my favourite colour</legend>
<div>
<input type="radio" name="colour" value="red" id="red" checked="checked" />
<label for="red">Red</label>
<input type="radio" name="colour" value="green" id="green" />
<label for="green">Green</label>
<input type="radio" name="colour" value="blue" id="blue" />
<label for="blue">Blue</label>
</div>
</fieldset>
...submit button, etc...
</form>
<input type="radio" />
name
value
checked="checked" on
one button so that it is already pre-selected
name and
value are sent to the server
$colour = $_GET['colour'];
$winnings = 0;
if ( $colour == 'green' )
{
$winnings = 5;
}
echo "<p>You win {$winnings} eurines!</p>";