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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?php /* string is enclosed in double quotes */ $string1 = "Hello world!"; //double quote string $string2 = '"Hello world!"'; //single quote string echo $string1, "<br>"; echo $string2, '<br>'; $i = 123; //positive integer $n =-123; //negative integer $d = 1.23; //float /* boolean and null */ $b = true; $f = false; $N = null; /* do not try to print booleans */ echo "\$b = $b", '<br>'; //unexpected: $b = 1 echo "\$f = $f", '<br>'; //unexpected: $f = echo "\$N = $N", '<br>'; //unexpected $N = /* variable introspection var_dump() */ echo var_dump($b), '<br>'; echo var_dump($f), '<br>'; echo var_dump($N), '<br>'; ?> |
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:
1 2 3 4 5 6 7 8 9 10 11 | <?php /* declare array of 3 strings */ $fruits = array("Orange","Banana","Apple"); /* quick introspection for debug */ echo var_dump($fruits), "<br><br>"; /* string interpolation using array elements */ echo "$fruits[0],$fruits[1],$fruits[2]"; ?> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php // declare object data type Person class Person { function Person($name, $age) { $this->name = $name; $this->age = $age; } } // create an object of type Person $barbu = new Person("Barbu",35); // show the "name" attribute echo $barbu->name; ?> |
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:
1 2 3 4 5 6 7 8 | <?php /* define a constant */ define("HELLO", "Hello World!"); /* use a constant */ echo HELLO; ?> |
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”.
Operator | Description |
= | 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:
Operator | Name | Expression | Result |
+ | Addition | $x + $y | 6 |
– | Subtraction | $x – $y | 2 |
* | Multiplication | $x * $y | 8 |
/ | Division | $x / $y | 2 |
% | Modulus | $x % $y | 0 |
** | Exponential | $x ** $y | 16 |
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.
Symbol | Keyword | Example |
---|---|---|
&& | 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:
Symbol | Description | Example |
---|---|---|
~ | Bitwise not | ~1010 == 0101 |
& | Bitwise and | 1010 | 1100 == 1000 |
| | Bitwise or | 1010 | 1100 == 1110 |
^ | Bitwise xor | 1010 ^ 1100 == 0110 |
Note: Bitwise operators actually work for Booleans due to automatic conversion to integer values.
Next article: Control Flow