Lesson 3 of 100
Types12 min5 checksPHP 8.5

Scalar Types: Strings, Integers, Floats and Booleans

Learn how PHP's four scalar types differ, how to inspect them, and how automatic conversion and casts can change the meaning of a value.

01 · THE IDEAREAD → TRY → LEARN

What a type tells you

Every expression in PHP has a type, and the type decides which operations make sense. PHP is dynamically typed, so you do not have to declare a type for each variable. The type is worked out at runtime from the current value. This lesson covers the four scalar types, which hold a single simple value:

  • int for whole numbers such as 42 or 0
  • float for numbers with a fractional part such as 3.5
  • string for text such as "hello" or "42"
  • bool for true or false

null is a separate type that means "no value". Arrays and objects are compound types and are not scalars.

Inspecting a type

There are two main ways to ask about a type. get_debug_type() returns a readable name for the value, and var_dump() shows both the type and the value. When you only need a yes-or-no answer, use the is_int(), is_float(), is_string() and is_bool() family.

The example below is the worked example for this lesson. It walks over four values and prints the type name of each one.

Example
<?php
$values = [42, 3.5, "42", true];
foreach ($values as $value) {
    echo get_debug_type($value), "\n";
}
Output
int
float
string
bool

The integer 42 and the string "42" look alike when printed, but they are different types. That difference affects how they behave in later operations.

Automatic conversion

When you use a value in an operation its type does not support, PHP tries to convert it to a type that works. This is called type juggling, and the result depends on the operator. The + operator wants numbers, so a numeric string is treated as a number. The . operator wants strings, so a number is turned into text.

Example
<?php
$total = "5" + 3;
$text = "5" . 3;
var_dump($total);
var_dump($text);
Output
int(8)
string(2) "53"

The first result is the integer 8. The second is the two-character string "53". Same inputs, different operators, different types out. If you want to know which type you ended up with, check it with var_dump() rather than assuming.

Casts and floating-point care

A type cast forces a conversion: (int), (float), (string) and (bool). Casting is explicit, so a reader can see that you meant to convert. Two details matter in practice:

  • Casting a string that starts with digits to int reads the leading number, so (int) "12abc" gives 12.
  • The string "0" casts to false. Many other non-empty strings cast to true.

Floats are stored in binary, so some decimal fractions cannot be represented exactly. Adding 0.1 and 0.2 does not give exactly 0.3. Compare floats by checking that the difference is smaller than a small tolerance, not with ===.

Example
<?php
$price = 0.1 + 0.2;
echo json_encode([
    'same' => $price === 0.3,
    'close' => abs($price - 0.3) < 0.00001,
    'int' => (int) "12abc",
    'bool' => (bool) "0",
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"same":false,"close":true,"int":12,"bool":false}

Common mistakes

  • Assuming "42" and 42 are interchangeable. They may compare or print the same way in some contexts, but they are different types.
  • Testing a value for truthiness when you really need a type check. 0, "" and false are all falsey, yet they are three different kinds of value.
  • Using gettype() output where get_debug_type() names are expected. The older function returns names such as integer and double, while get_debug_type() returns int and float.

Exercise contract

Write a function scalarKind($value) that returns one of five strings:

  • "int" for integers
  • "float" for floats
  • "string" for strings, including the empty string
  • "bool" for booleans, including false
  • "other" for anything else, such as null or an array

The function must look at the real type of the value, not at whether it is truthy. The tests include 0, "" and false as boundary inputs, so a solution built on if ($value) will fail them. Start with the is_* functions and handle each type in turn.

Recap

PHP has four scalar types: int, float, string and bool. Use get_debug_type() and var_dump() to see what you have, and the is_* functions to test it. Remember that operators may convert values automatically, and that casts make a conversion explicit. Treat float comparisons with a tolerance, and do not mistake a falsey value for a different type.

Before you start

Variables

You’ll practice
  • Distinguish the basic scalar types
  • Use values with the intended type
Your task

Make scalarKind() pass all 5 checks. Run your code as often as you like, then check your answer.

exercise.phpSaved in this browserPHP 8.5
CONSOLE & CHECKS● Ready

Ready when you are

Your output will appear here.
Make it work. Then make it pass.

PHP loads on your first run. Each check starts fresh. Your code stays in this browser; clearing site data removes your progress. A check or the task looks wrong? Report a problem with this exercise.