PHP - Scalar Data types

Introduction

PHP has eight primitive types, but for now, we will focus on its four scalar types:

TypeDescription
Booleans true or false values
Integers numeric values without a decimal point, for example, 2 or 5
Floating point numbers or floatsnumbers with a decimal point, for example, 2.3
Stringscharacters which are surrounded by either single or double quotes, like 'this' or "that"

Demo

<?php
    $number = 123; /*from w  w  w.j ava2 s  . co  m*/
    var_dump($number); 
    $number = 'abc'; 
    var_dump($number); 
?>

Result

Here, the code first assigns the value 123 to the variable $number.

As 123 is an integer, the type of the variable will be integer int.

After that, we assign another value to the same variable, this time a string.

The type of the variable changed from integer to string, and PHP did not complain at any time.

This is called type juggling.

Related Topics