Data Types in php – PHP Lessons

elephant_phpHere we come to the data types in PHP. Currently PHP supports 8 types of data types.

In this tutorial we consider only four:
PHP is a language with dynamic typing. I will explain a little – in the strongly typed language when we create a variable we immediately say what kind of data will be stored there. If we were declare that there will be a number then an attempt to put there something else will result in an error. In strongly typed languages ​​we cannot afford to use a variable of any data type without declaring it. PHP itself is trying to determine what kind of data type is stored in variable and usually he does it correctly.

Type BOOLEAN

Simple data type denotes the true value – is TRUE or FALSE – lie. TRUE and FALSE are case-insensitive. When we give some data to the server it does not have an idea what is true or false (TRUE or FALSE) and so these data are converted or reduced to a line. If we write the following code:

<?php
$user_name = true;
echo $user_name; // ?
?>

What will display browser? In this case the data is converted to a string and a value of TRUE is reduced to a string. TRUE in the string version is “1”. And the browser will print one (1). The question is – what will the value FALSE? An empty string!

Type INTEGER

It’s all positive and negative whole numbers.

Type FLOAT

This is a floating point number (real). For example:

<?php
$float = 1.234;
$float = 1.2e3;
$float = 7E-10;
?>

Type STRING

In PHP for the most parts we are working with strings. As we have seen you can enter put a string value in single quotes or double. In the double quotes we can substitute the values ​​of variables. For example:

<?php
$user_name = 'Martin';
echo "Hello, $user_name";
?>

It will print «Hello, Martin». Everything is quite simple. But that’s not all. Just inside the double quotes we can use special characters. PHP to understand that this is a special character and not just a symbol it is protected by a slash.

<?php
$user_name = 'Martin';
echo "Hello, \n$user_name";
?>

Type NULL

Variable will return NULL if it was not defined or it was deleted.

Leave a Reply

Your email address will not be published. Required fields are marked *