PHP Conditionals and Booleans

·

1 min read

A boolean variable can only be true or false;

We declared the variable $name as the name of the book, and the variable $read as our boolean variable, if it's true then the user reads the book, if it's false then the user didn't read it.

if($read) {

$message="You have read $name";

} means that if the $read value is true then execute the next code which declares a variable called message.

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <title>Demo</title>
    <style>
        body{
            display: grid;
            place-items: center;
            height: 100vh;
            margin: 0;
            font-family: sans-serif;
        }
    </style>
</head>
<body>
<?php
$name="Dark Matter";
$read=true;
if($read){
    $message="You have read $name";
}
else{
 $message="You have not read $name";
}


?>
<h1>
    <?php  echo $message?>
</h1>
</body>
</html>