CodeBlanks Logo Icon CodeBlanks

float and bool

PHP course · Beginner · Lesson 14/116

Learn the float and bool types and the use of true and false.

What is a float?

A float (or double) holds a number with a decimal point. Example: $price = 19.99;

In PHP the decimal separator is a dot; a comma is not allowed. Writing 19,99 causes a syntax error.

Use a float for fractional values like temperature, price or a ratio. Use an int for whole numbers and a float when you need a fractional part.

Computers cannot store all decimals exactly; the BCMATH functions for money will be covered later.

bool: true or false

A bool has only two values: true or false. It represents a logical state.

Example: $isOpen = true; or $isReady = false;.

PHP accepts the true and false keywords case-insensitively; so TRUE also works.

When you echo a bool, true appears as 1 and false appears as an empty string.

Exercise Print temperature and active status

Assign 23.5 to $temperature and true to $isActive. On the first line print Temperature: 23.5 C, on the second line print Active: 1; end each line with \n.

Show example solution
"Temperature: " . $temperature . " C\n", second: "Active: " . $isActive . "\n".
Example solution:
<?php
$temperature = 23.5;
$isActive = true;
echo "Temperature: " . $temperature . " C\n";
echo "Active: " . $isActive . "\n";

Test yourself

  1. Which one is a float value?

    $price = 19.99;
    • $age = 25;
    • $name = "25";
    • $price = 19.99;
    • $ok = false;
    Show answer

    $price = 19.99; — Correct! A number with a decimal point is a float.

  2. Assign true to a bool variable

    $isOpen = ____;
    Show answer
    $isOpen = true;
  3. Match each value to its type

    42
    integer
    19.99
    float
    true
    boolean
    "text"
    string

All lessons in Beginner