PHP Data types
A variable can store different types
of Data. Let’s have a look at some of the data types supported by PHP:
PHP String
A string is a sequence of characters.
In PHP, you can write the string inside single or double quotes.
PHP
Integer
An integer data type is a
non-decimal number between -2,147,483,648 and 2,147,483,647. An integer must
have at least one digit and can be either positive or negative.
The following example takes $a as an integer. The PHP var_dump() function
returns the data type and value.
PHP
Float
A float or floating point
number is a number with a decimal point or a number in exponential form.
The following example takes $a as a
float and the PHP var_dump() function returns the data type and value.
PHP
Boolean
A Boolean represents two possible
states: TRUE or FALSE. They are often used in conditional testing.
PHP
Object
An object is a data type which
stores data and information on how to process that data. In PHP, an object must
be explicitly declared. We need to declare a class of object using the class
keyword.
PHP Array
An array stores multiple values in
one single variable. In the following example, the PHP var_dump() function
returns the data type and value.
Now that you have learnt about the
various Data Types, let’s move ahead with the PHP Tutorial and have a look at
the different PHP Variables.
PHP
Variables
Variables are containers for storing
information. All variables in PHP are denoted with a leading dollar
sign ($). Unlike other programming languages, PHP has no command for
declaring a variable. It is created the moment you first assign a value to it.
Declaring PHP Variables:
The PHP echo statement is often used
to output data to the screen.
PHP
Variables Scope
In PHP, variables can be declared
anywhere in the script. The scope of a variable is the part of the script where
the variable can be used.
In PHP we have three different variable scopes:
1. Local – A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:
<?phpfunction myTest(){$a = 7; // local scopeecho "<p>Variable a inside function is: $a</p>";}myTest(); // using x outside the function will generate an error echo"<p>Variable a outside function is: $a</p>";?>
2. Global– A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function. The global keyword is used to access a global variable from within a function:
<?php$a = 9; // global scopefunction myTest() {// using a inside this function will generate an errorecho "<p>Variable a inside function is: $a</p>";}myTest();echo "<p>Variable a outside function is: $a</p>";?>
- Static–
When a function is executed, all of its variables are deleted. But if you
want any variable not to be deleted, the static keyword is used when
you first declare the variable:
<?phpfunction myTest(){static $a = 0;echo $a; $a++;}myTest();myTest();myTest();?>
Now that you know about the
declaration of variables, let’s move ahead with the PHP Tutorial and have a
look at the operators in PHP.
PHP
Operators
Operators are used for performing
different operations on variables. Let’s have a look at the different operators
in PHP:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Array operators
Arithmetic
Operators
The PHP arithmetic operators are
used with numeric values to perform common arithmetical operations, such as
addition, subtraction, multiplication etc.
Operator |
Name |
Example |
Result |
+ |
Addition |
$a + $b |
Sum of $a and $b |
– |
Subtraction |
$a – $b |
Difference of $a and $b |
* |
Multiplication |
$a * $b |
Product of $a and $b |
/ |
Division |
$a / $b |
Quotient of $a and $b |
% |
Modulus |
$a % $b |
Remainder of $a divided by $b |
** |
Exponentiation |
$a ** $b |
Result of raising $a to the $b’th
power |
Assignment
Operators
The PHP assignment operators are
used with numeric values to write a value to a variable.
Assignment |
Similar to |
Result |
a = b |
a = b |
The left operand gets set to the
value of the expression on the right. |
a += b |
a = a + b |
Addition |
a -= b |
a = a – b |
Subtraction |
Comparison
Operators
The PHP comparison operators are
used to compare two numbers or strings
Operator |
Name |
Example |
== |
Equal |
$a == $b |
=== |
Identical |
$a === $b |
!= |
Not equal |
$a != $b |
<> |
Not equal |
$a <> $b |
!== |
Not identical |
$a !== $b |
> |
Greater than |
$a > $b |
< |
Less than |
$a < $b |
>= |
Greater than or equal to |
$a >= $b |
<= |
Less than or equal to |
$a <= $b |
Logical
Operators
The PHP logical operators are used
to combine conditional statements.
Operator |
Name |
Example |
and |
And |
True if both $a & $b are true |
or |
Or |
True if either $a or $b are true |
xor |
Xor |
True if either $a or $b are true,
but not both |
&& |
And |
True if both $a & $b are true |
|| |
Or |
True if either $a or $b are true |
! |
Not |
True if $a is not true |
Array
Operators
The PHP array operators are used to
compare arrays.
Operator |
Name |
Example |
+ |
Union |
$a + $b |
== |
Equality |
$a == $b |
=== |
Identity |
$a === $b |
!= |
Inequality |
$a != $b |
<> |
Inequality |
$a <> $b |
!== |
Non-identity |
$a !== $b |