In
this fourth article of this series, Luigi Arlotta explains
different datatypes that you can use in PHP. Mainly integers,
floats and strings have been dealt with. This article also explains
the use of escape characters.
In
this entire article, I shall not be providing complete examples,
since I assume you know enough of PHP to add the basic tags
and other lines required to make a complete program. I shall
only be discussing the important part of the programs.
Datatypes
To declare a variable it is sufficient to choose a name, through
which the variable will be identified, and then initialize it.
It is not necessary, as it happens for other programming languages,
to declare the type of the variable. PHP is able to deduce the
type of a variable from the value with which it has been initialized.
For instance, if we write
PHP
will create a variable of integer type with the name ' $var1
' and store the value of 7 in it.
If instead we write
| $var2
= "This is a variable"; |
PHP
will create a variable of string type named ' $var2 '
and store the value "This is a variable" in
it.
The type of a variable indicates what kind of data the variable
will store. The concept of datatypes is particularly important,
not only in PHP, but in all programming languages.
A
few of the datatypes in PHP are Integers, Floating Point numbers,
Strings. I
shall explain all of these in the rest of the article.
Datatypes : Integers
Variables
of Integer type represent positive and negative integers, or
zero. The declaration and initialization of an integer variable
has the following syntax
$VarName
is the name we have chosen for the variable, while IntValue
has to be replaced with a datatype compatible number (a positive,
negative or zero number).
The
various operations that you can perform with integers are shown
in the table below.
|
Arithmetic
Operations
|
|
$a
+ $b
|
Addition
|
|
$a
- $b
|
Subtraction
|
|
$a
* $b
|
Multiplication
|
|
$a
/ $b
|
Division
|
|
$a
% $b
|
Modulo
(Remainder)
|
|
Increment/Decrement
Operators
|
|
$a++
OR ++$a
|
Increment
$a by 1
|
|
$a--
OR --$a
|
Decrement
$a by 1
|
To
make comparisons between integer variables we can use the comparison
operators as shown previously while explaining conditional statements.
They are summed up for you in the below table.
|
Comparisons
for integer datatypes
|
|
$a
== $b
|
$a
< $b
|
$a
<= $b
|
$a
> $b
|
$a
>= $b
|
$a
!= $b
|
Datatypes : floating point numbers
Variables
of floating point datatype are used to store numbers with a
decimal part. The declaration and initialization of a floating
point value has the following syntax
$VarName
is the name we have chosen for the variable, while FloatValue
has to be replaced with a datatype compatible number.
In
order to execute operations with floating point numbers we can
use the classic math operators as shown in the table for integer
datatypes.
Note : You can carry out all the arithmetic operations
with floats that you can perform with integers except the modulo
operation. Obviously the module operator (%) lacks, because
the division between decimal numbers has no remainder.
You
can make similar comparisons between floating point values as
you did with integer values.
Datatypes:
strings
The
declaration of a string variable is same as that of any other
variable. The only difference is that the value that you want
to store in a string variable must be enclosed in inverted commas
or quotes. I have already talked about the existing difference
between inverted commas and quotes previously. Here are two
string variable declarations. The first one uses inverted commas,
while in the second one quotes are used.
|
$str1
= "This is a string datatype variable";
$str2 = 'This is a string datatype variable, too';
|
Previously I had said that PHP interprets all that is enclosed
between inverted commas and carries out the needed substitutions;
whereas it leaves unchanged all that is enclosed between quotes.
Let's look at some examples that will help us to be familiar
with the PHP works. Observe the following script and try to
guess the output.
$Civicc
= 8;
$Address = "Via Tespi, $Civic";
echo 'My address is $Address'; |
The
script's output is displayed below
If
you guessed the output right it means you understood this series
properly till now. If you have still have some trouble understanding
why PHP has printed the variable name instead of its value,
the answer once again is : PHP does not interpret the content
between quotes.
Here
is another script. Try guessing the output.
|
$Civic
= 8;
$Address = "Via Tespi, $Civic";
echo "My address is $Address";
|
The output of this script is as follows
|
My
address is Via Tespi, 8
|
By
replacing the quotes with the inverted commas in the third line,
the PHP interprets the text before printing it. The $Address
variable is replaced with its actual value at that instant (
in our program it is equal to Via Tespi, 8).
Note the double substitution that takes place in that line
- One for $Address and another for $Civic.
Escape
Characters
At
this point you may have a doubt. Suppose you want to display
something like this in an HTML page using PHP
| The
value of $Address variable is Via Tespi, 8 |
As a beginner you may try writing the following 2 scripts if
you wanted the above output. but you would be disappointed with
the actual output of both these programs
Code
for Program 1
$Civic = 8;
$Address = "Via Tespi, $Civic";
echo "The value of $Address variable is $Address";
|
Output
of Program 1
The value of Via Tespi, 8 variable is Via Tespi, 8 |
| --- |
Code
for Program 2
$Civic
= 8;
$Address = "Via Tespi, $Civic";
echo 'The value of $Address variable is $Address';
|
Output
of Program 2
The value of $Address variable is $Address |
On possible solution to the problem is shown below
$Civic
= 8;
$Address = "Via Tespi, $Civic";
echo 'The value of $Address variable';
echo " is $Address"; |
This above script would display the required
output, but we are compromising by using two echo statements
rather than one.
An easier and better way exists. It is possible by using escape
characters ' \ ' (backslash) between inverted commas,
that allow printing of some special characters.
$Civic
= 8;
$Address = "Via Tespi, $Civic";
echo "The value of \$Address variable is $Address"; |
The above program would display the following
output
| The
value of $Address variable is Via Tespi, 8 |
Even
though we have used inverted commas, addition of the escape
character ' \ ' before the variable name, prevents
PHP from interpreting that variable.
Similarly
in case you want inverted commas as a part of your output
you could use the following
|
$Civic
= 8;
$Address
= "Via Tespi, $Civic";
echo "The value of \$Address variable is \"$Address\""
|
This
program would display the following
| The
value of $Address variable is "Via Tespi, 8" |
There
are other escape characters as well, that are used to represent
special characters. These are shown in the table below.
|
Escape
Characters
|
|
\"
|
Insert
an inverted comma in the output |
|
\n
|
Insert
a newline (goes to next line) in the output |
|
\r
|
Insert
a carriage return in the output |
|
\t
|
Insert
a tab in the output |
|
\$
|
Insert
a dollar sign in the output |
|
\\
|
Insert
a backslash in the output |
That's all in this article.
In the next and final article in this series, I shall explain
Arrays which is a very common programming structure used
by many beginners as well.

|
©
Copyright by Luigi Arlotta. All rights reserved. Contact
the author for permissions.
|