Web Info and IT Lessons Blog...

Sunday, 2 November 2014

PHP Data Types

The previous lesson of PHP Lessons Series was about Echo and Print Statements and in this lesson we will learn about the basic data types in PHP.

PHP Data Types

Note: This Lesson only discusses the Basic PHP Data Types, more complex data types like Arrays and Objects will be discussed in the later lessons.

The basic data types in PHP are as follows:

PHP Strings


A string in PHP is a combination of characters i.e 'This is a string!'.
This is how we can initialize a string in PHP:


$str = 'My name is John';

Now the text 'My name is John' is saved in $str. We can also initialize an empty string by doing this:


$str = '';

PHP Integers


An integer in PHP can be specified in three formats i.e Decimal, Hexadecimal and Octal. The simplest and most common of the three is decimal. This is how we can initialize an integer.


$intnum = 100;

An integer can also be negative i.e


$intnum = -100;

PHP Floats


A float in PHP is a number with a decimal point between the digits or a number in exponential form. This is how we can initialize a float with decimal point in PHP:


$floatnum = 10.124;

A float in exponential form can be initialized like this:


$floatnum = 10e6;

PHP Booleans


A boolean in PHP can either be true or false. This is how we can initialize a boolean in PHP:


$bool = true;

PHP Null


Null data type in PHP can only have one value i.e null. This is how the null value of a variable is set in PHP:


$a = null;

The null value of variable is usually set to empty a variable.



No comments:

Post a Comment