Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Boolean | Variables and Data Types
Introduction to PHP

book
Boolean

The boolean data type (bool) is used to represent logical values. A boolean variable can only have two possible values: true or false. Booleans are fundamental for decision-making and conditional operations within PHP programs.

php

main

copy
<?php
$isActive = true;
$isLogged = false;
?>
1234
<?php $isActive = true; $isLogged = false; ?>

Outputting Boolean Values


You can output boolean values (true or false) using the echo function. When you use echo with true, PHP automatically outputs "1", as true converts to this string:

php

main

copy
<?php
$isLoggedIn = true;
echo $isLoggedIn; // Outputs "1"
?>
1234
<?php $isLoggedIn = true; echo $isLoggedIn; // Outputs "1" ?>

When you use echo with false, nothing is output, as false converts to an empty string:

php

main

copy
<?php
$isLoggedIn = false;
echo $isLoggedIn; // Does not output anything
?>
1234
<?php $isLoggedIn = false; echo $isLoggedIn; // Does not output anything ?>

This approach allows for convenient output of boolean values without manually converting them to strings.

question mark

Choose the Correct Statements about bool in PHP

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4

Spør AI

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

We use cookies to make your experience better!
some-alt