PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP AdvancedPHP Include and RequirePHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP OOP IntroductionPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP InterfacesPHP OOP TraitsPHP OOP ConstantsPHP OOP StaticPHP OOP NamespacesPHP OOP Autoloading

PHP Data Types

Variables can store different data types. In PHP, PHP chooses the appropriate data type for the variable automatically.

Normally, we need to store different types of data in variables to do different tasks.

There are several data types in PHP.

var_dump() Function

var_dump() function dumps information about a variable. The importance of this function is, it outputs the data type of the variable with its value. It is also a output function like echo()

PHP var_dump() Example


<?php
$x = 12;
var_dump($x);

Run Example ››

PHP Booleans

A Boolean is a variable that can have one of two possible values, true or false

Boolean is the simplest data type in PHP. It holds either true or false.

To specify a boolean, you can use constants true and false (Both are case-insensitive).

PHP Booleans Example


<?php
$a = true; // assign true to $a
$b = false; // assign false to $b

var_dump($a);
var_dump($b);

Run Example ››

PHP Integers

An integer is a number which is not a fraction; a whole number (..., -2, -1, 0, 1, 2, 3, ...)
An integer can either be a negative or a positive number

Four ways to specify an integer in following notations:

  • Specifying Integers in Decimal - base 10
  • Specifying Integers in Hexadecimal - base 16
  • Specifying Integers in Octal - base 8
  • Specifying Integers in Binary - base 2

This table shows how to specify an integer (128) in different notations.

NotationPreceded withExample
Decimal128
Hexadecimal0x0x80
Octal00200
Binary0b0b10000000

PHP Integers Example


<?php
// 128 in different notations
$a = 128; 			// decimal number
var_dump($a);

$a = -128; 			// a negative number
var_dump($a);

$a = 0x80; 			// hexadecimal number
var_dump($a);

$a = 0200; 			// octal number
var_dump($a);

$a = 0b10000000;	// binary number
var_dump($a);

Run Example ››

As a beginner, you will not frequently use hexadecimal, octal and binary notations. So, it is not compulsory to remember them now.

PHP Floats

A float is a number that can contain a fractional part (2.56, 1.24, 7E-10)

Floats are also known as "Double", "Real" or "Floating-Point Numbers".

PHP Floats Example


<?php
$a = 2.56;
var_dump($a);

$a = 2.56e3; // 2.56 multiplied by 3rd power of 10
var_dump($a);

$a = 2.56e-5; // 2.56 multiplied by -5th power of 10 
var_dump($a);

Run Example ››

PHP Strings

A string is any finite sequence of characters (letters, numerals, symbols, punctuation marks, etc.)

There are four ways to specify a string.

  • Single Quoted
  • Double Quoted
  • Heredoc Syntax
  • Nowdoc Syntax

We learned all of them in the strings chapter.

PHP Strings Example


<?php
$str = 'This is single quoted';
$str = "This is double quoted";

$str = <<<EOD
This is a heredoc example
This can be multiline
EOD;

$str = <<<'EOD'
This is a nowdoc example
This can be multiline
EOD;

var_dump($str);

Run Example ››

PHP Arrays

An array is a series of values

There are two ways to declare an array.

  • An array can be declared using the array() function.
  • An array can be declared wrapping with [ and ].

Elements of the arrays should be separated with a comma. And, elements can be any type you learn in this chapter.

PHP Arrays Example


<?php
$array = array('Apple', 'Banana', 'Orange', 'Mango');
var_dump($array);

$array = ['Apple', 'Banana', 'Orange', 'Mango'];
var_dump($array);

Run Example ››

Note: Array is a huge part in PHP. We will learn more about arrays in the Arrays Chapter.

PHP Objects

An object is a particular instance of a class where it can be a combination of variables, functions, and data structures.

Note: We will learn about objects in the Object Oriented Programming tutorial.

PHP NULL

Null represents a variable with no value
  • Null means no value.
  • Null is not 0, false or any other value, it is null.
  • Null is case-insensitive. All Null, null and NULL are equal.

PHP Null Example


<?php
$var = null;
var_dump($var);

// unsetting variables with null
$text = 'Hello World';
$text = null; // now $text does not hold any value
var_dump($text);

Run Example ››
Need help with PHP? Post your question on our forum's PHP Group.
Facebook Twitter