|
Example:
$Name,
$Name1, $First_Name, $Last_Name
$Name = "David";
$Age = 16;
|
Another
important thing in PHP is that
all the statements must end
with a semicolon ";".
In PHP we needn't have to specify
the variable type, as it takes
the data type of the assigned
value. From the above example
we understand that '$Name' is
of Data type String and '$Age'
is of type Numeric.
Limitations in Variable Naming:
PHP has
no limit on the length of variable
name as in the case of other
programming languages. The variable
name must start with a letter
or underscore. The variable
name must be a combination of
letters, numbers and underscores.
Other characters such as *,
+, #, @ are not allowed and
causes error if used.
Data Types in PHP
In PHP the data type of a variable
is not set by the programmer.
PHP decides the data type of
variables after interpreting
the web page. Data Types in
PHP include:
- Numeric data type
- String data type
- Array data type
Numeric data type
Numeric data type is used for
number. There are two different
numeric data types:
Integer
Data Type
Integer
data types contain whole number
values.
Doubles
Double
contains floating point numbers.
|
Example:
$doubtotal
= 99.5;
|
String Data Type
String
data type is used to contain
textual information or letters.
The value is assigned within
quotes as shown below.
|
Example:
$strName
= "David";
$strId = "David123";
|
String Concatenation
String Concatenation is a process
of adding two strings. This
is done by attaching one string
to the end of another string.
This is done by using '.' (period)
operator as shown below:
|
Example:
$strFirst_Name
= "David";
$strSpace = " ";
$strLast_Name = "John";
$strName = $strFirst_Name.$strSpace.$strLast_Name;
echo $strName;
Output:
David John
|
Array Data Type
Array data type is used to
contain many values in a single
variable. Each array element
can be retrieved by using the
array variable name and its
key/index value. We can declare
an array variable in different
ways as shown below:
|
Example:
$arrMark[0]
= 95;
$arrMark[1] = 88;
$arrMark[3] = 77;
(Or)
$arrMark = array(95,88,77);
|
In the above example, the variable
'arrMark'
contains three different values.
In the first method the array
elements are stored using the
key values (0, 1, 2), but in
the second example the keyword
'array'
is used to assign values to
the array variable. By default,
array key element starts with
0.
The array elements can be accessed
by using their key values. The
different marks stored in the
array elements can be added
together to obtain the total
marks as shown below:
|
Example:
$intTotalMark
= $arrMark[0] + $arrMark[1]
+ $arrMark[2];
echo $intTotalMark;
Output: 260 |
In the above two examples we
have used the key values as
numbers, but it not necessary
that the key value must be a
number, it can also be a string
as shown below:
|
Example:
$arrMark['maths']
= 95;
$arrMark['english'] =
88;
$arrMark['science'] =
77;
$intTotalMark = $arrMark['maths']
+ $arrMark['english']+$arrMark['science'];
echo $intTotalMark;
Output: 260 |