Boolean data type in PHP

How to define a boolean variable and assign value?


A boolean type is typically denoted by "bool" or "boolean".It can hold values either "true" (1) or "false" (0). Any non zero values and non empty string are also considered as TRUE.
When you want to declaring boolean variable, you can declare as given below
boolean $variable;

where boolean denotes the type of the variable.
You can also declare variable without datatype, in such case PHP will try to determine type of variable based on the value hold by that variable. For example if you assign a string value to variable, this variable becomes a string variable. Below I have declared a variable without its type.
$variable= true;
Here $variable is a variable of type boolean.
If you want to get a type of a variable, you can use gettype() function.

Example :

print gettype($variable);

Above code will print type as boolean.

PHP Topics

Ask Questions

Ask Question