PHP Data Types
A data type is the kind of a value. PHP values come in kinds like whole numbers, decimal numbers and text.
Three kinds under var_dump
var_dump prints the type and the value of what it is given. Three values of three kinds print on three lines.
<?php
var_dump(42);
var_dump(3.5);
var_dump("Hana");True and false under var_dump
A bool value goes through var_dump here. Each line names the type and shows which of the two values it holds.
<?php
var_dump(true);
var_dump(false);Type names from gettype
gettype gives the name of the type of a value as text. Four values print four type names.
<?php
echo gettype(42), "\n";
echo gettype(3.5), "\n";
echo gettype("Hana"), "\n";
echo gettype(true), "\n";The null value
The code dumps the null value here. The output shows the word PHP writes for it.
<?php
var_dump(null);Example
<?php
var_dump(7);
var_dump(2.5);
var_dump("Mina");
var_dump(false);Key points
- The int type holds whole numbers.
- The float type holds numbers with a decimal part.
- The string type holds text.
- The bool type holds true or false.
- Null means no value.
- Each value belongs to one kind, and that kind is the data type of the value.
In this example, four values go through var_dump in order. The first dump shows an int. The second dump shows a float. The third dump shows a string with its length. The last dump shows a bool.
Note
gettype calls a float double, an old name that stayed.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Programiz PHP Compiler
Exercise
What does var_dump("Hana") print?