Menu Close

Articles

PHP: Data types

PHP is a dynamic typed programming language. That means a variable in PHP can change its type dynamically. The type of a variable is not usually set by the programmer, rather it is decided at runtime by PHP depending on the context in which that variable is used.

Basic Types

Following basic data types are supported in PHP:

  • String
  • Integer
  • Float
  • Boolean
  • NULL

Example:

Reading next example you will learn quickly literal representation for each type. In PHP you must initialize a variable with a literal or null. You can not define uninitialized variables like you do in C.

Notes:

  • Remember in PHP all variables start with $ prefix;
  • Statement “echo” can not print correctly Boolean types;
  • For introspection you can use var_dump() function;

Composite Types

Following composite types are available in PHP:

  • Array
  • Object

Array Example:

Arrays are 0 based indexed collection of elements. To declare an array we use “array” constructor:

Object Example:

An object is a composite data type that have attributes and functions. First we define a “class” that is the object data type. Next we can create one or more “instances” of the class, that are called objects:

Notes:

  • Keyword “this” represent current object that is going to be created by constructor;
  • A constructor is a function belonging to a class that has same name as the class;
  • A constructor can receive parameters and establish object attributes;
  • Object attributes are accessed with symbol “->” that is inherited from C;

Constants

In PHP you can create identifiers for constants. These can be used like any global variable, except they can not be modified during program execution. You use keyword “define” with following syntax:

Example:

Operators

Depending on data type, we can perform specific operations to analyze variable values or perform mathematical computations using expressions. An expression can be used to modify values of variables.

Assign Operator:

One operator is much more different than any other operator. This is assignment operator: “=”. It can be used to setup initial value for a variable using a constant literal or an expression. It can be used in combination with other operators to create a “modifier”.

OperatorDescription
 = Assignment / Initialization
 += Addition modifier
 -= Subtraction modifier
 *= Multiplication modifier
 /= Division modifier
 %=Modulus modifier

Arithmetic Operators

Arithmetic operators are used with infix expressions. That means the operator is usually between two operands like in the following examples; given $x = 4, $y = 2:

OperatorNameExpressionResult
+Addition$x + $y 6
Subtraction$x – $y2
*Multiplication$x * $y8
/Division$x / $y2
%Modulus$x % $y0
**Exponential$x ** $y16

Relation Operators

PHP can compare data of different types and make a logical deduction. First it convert the data to a common type then execute the comparison. These operators are used in conditional expressions that produce a Boolean result: true or false.

==Equal (same value)
!=Not equal
<>Not equal
===Identical (same value and type)
!==Not identical
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Boolean Operators

If you know a bit of logic you must also know that logical values are called Boolean values: true and false. For variables having these values we can use special operators, called Boolean operators also known as logical operators. In PHP you can use a symbol or a keyword for making Logical expressions.

SymbolKeywordExample
&&and$a and $b == $a && $b
||or$a or $b == $a || $b
 xor$a xor $b == $a ^ $b 
!not!$a == not $a

Bitwise Operators

This operators are similar to logic operators, except they operate on integer numbers, considering 0 = false and 1 = true. Any binary number is actually a set of Boolean values. When you apply a bitwise operation between the two numbers,  the operation is executed bit by bit:

SymbolDescriptionExample
~Bitwise not~1010 == 0101
&Bitwise and1010 | 1100 == 1000
|Bitwise or1010 | 1100 == 1110
^Bitwise xor1010 ^ 1100 == 0110 

Note: Bitwise operators actually work for Booleans due to automatic conversion to integer values.

 

Next article: Control Flow