Developer
PHP
PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP VariablesPHP ConstantsPHP OutputPHP Data TypesPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP StringsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms Finishing

PHP Data Types

Variables can store different data types. In PHP, data type is chosen by PHP automatically when the variable is declared first.

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 it's value.

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, ...)
Integer can either be a negative or a positive number

Four ways to specify a 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 (Since PHP 5.4)

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

Notation Preceded with Example
Decimal 128
Hexadecimal 0x 0x80
Octal 0 0200
Binary 0b 0b10000000

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 ››

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

Note: It's really important to understand the difference between single and double quotes. In case you missed that part, visit Single vs. Double Quotes part in PHP Output tutorial.

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 array() function.
  • An array can be declared wrapping with [ and ].

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 Arrays Chapter.

PHP Objects

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

In PHP, Objects are used in OOP (Object-Oriented Programming).

Note: We will discuss about OOP deeply later in this 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 assigned to a variable which is declared without any value.
  • Null can be used to empty a variable.
  • Null is case-insensitive. 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 ››
Advertisement
0
150