The bool
type (short for boolean) has only two values -- true
and false
.
The true
and false
literal values are case insensitive in code.
<?php
$value_is_true = true;
$value_is_false = false;
Boolean values are often use in logical expressions as the result of an operator and then used in control structures.
There are several logical operators:
!$a
: returns true
if $a
is not true
$a && $b
: returns true
if $a
and $b
are both true
$a || $b
: returns true
if either $a
or $b
are true
There is an and
and or
operator and they are complementary to &&
and ||
respectively.
They do not have the same behaviour as &&
and ||
and may produce unexpected results, and as such their use is discouraged.
In this exercise, you'll be implementing the quest logic for a new RPG game a friend is developing. The game's main character is Annalyn, a brave girl with a fierce and loyal pet dog. Unfortunately, disaster strikes, as her best friend was kidnapped while searching for berries in the forest. Annalyn will try to find and free her best friend, optionally taking her dog with her on this quest.
After some time spent following her best friend's trail, she finds the camp in which her best friend is imprisoned. It turns out there are two kidnappers: a mighty knight and a cunning archer.
Having found the kidnappers, Annalyn considers which of the following actions she can engage in:
You have four tasks: to implement the logic for determining if the above actions are available based on the state of the three characters found in the forest and whether Annalyn's pet dog is present or not.
Implement a function named canFastAttack
that takes a boolean value which indicates if the knight is awake.
This function returns true
if the 'Fast Attack' action is available based on the state of the character.
Otherwise, returns false
:
<?php
$is_knight_awake = true;
$infiltration = new AnnalynsInfiltration()
$infiltration->canFastAttack($is_knight_awake);
// => false
Implement a function named canSpy
that takes three boolean values, indicating if the knight, archer and the prisoner, respectively, are awake.
The function returns true
if the 'Spy' action is available based on the state of the characters.
Otherwise, returns false
:
<?php
$is_knight_awake = false;
$is_archer_awake = true;
$is_prisoner_awake = false;
$infiltration = new AnnalynsInfiltration()
$infiltration->canSpy($is_knight_awake, $is_archer_awake, $is_prisoner_awake);
// => true
Implement a function named canSignal
that takes two boolean values, indicating if the archer and the prisoner, respectively, are awake.
The function returns true
if the 'Signal Prisoner' action is available based on the state of the characters.
Otherwise, returns false
:
<?php
$is_archer_awake = false;
$is_prisoner_awake = true;
$infiltration = new AnnalynsInfiltration()
$infiltration->canSignal($is_archer_awake, $is_prisoner_awake);
// => true
Implement a function named canLiberate
that takes four boolean values.
The first three parameters indicate if the knight, archer and the prisoner, respectively, are awake.
The last parameter indicates if Annalyn's pet dog is present.
The function returns true
if the 'Liberate Prisoner' action is available based on the state of the characters and Annalyn's pet dog presence.
Otherwise, it returns false
:
<?php
$is_knight_awake = false;
$is_archer_awake = true;
$is_prisoner_awake = false;
$is_dog_present = false
$infiltration = new AnnalynsInfiltration()
$infiltration->canLiberate(
$is_knight_awake,
$is_archer_awake,
$is_prisoner_awake,
$is_dog_present
);
// => false
Sign up to Exercism to learn and master PHP with 11 concepts, 110 exercises, and real human mentoring, all for free.