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
=> Double Data Type
Integer Data Type
Integer data types contain whole number values. The below example will show that how to initialize integer data in php
Double Data Type
Double contains floating point numbers. The below example will show how to use double datatypes in php
String Data Type
String data type is used to contain textual information or letters. The value is assigned within quotes as shown below. The below example will show how to use String datatypes in php
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: Below example shows the usage of srting concatenation operator in php
Output:
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 1:
Example 2:
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:
Output:
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 in below example
Output:
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
=> Double Data Type
Integer Data Type
Integer data types contain whole number values. The below example will show that how to initialize integer data in php
$intmark = 100
Double Data Type
Double contains floating point numbers. The below example will show how to use double datatypes in php
$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. The below example will show how to use String datatypes in php
$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: Below example shows the usage of srting concatenation operator in php
$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 1:
$arrMark[0] = 95; $arrMark[1] = 88; $arrMark[3] = 77;
Example 2:
$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:
$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 in below example
$arrMark['maths'] = 95; $arrMark['english'] = 88; $intTotalMark = $arrMark['maths'] + $arrMark['english']; echo $intTotalMark;
Output:
183
0 comments:
Post a Comment