Hello Reader's ,
Today in my blog i am going to Describe Data Type in PHP. Data Type is a very important part of any programming language. Let's start learning about Data Type
What is Data Type?
A data type identifies the characteristics that the assigned data will be interpreted by when interacting with the PHP programming language. When a value is assigned a specific data type, the PHP interpreter will work with the data based on the expected type of data it is.
For example: 27654 could be considered a numeric data type or it could be considered a string data type,such as in the zip code for the customer. Even though you could perform mathematical equations on the zip code, it would make no sense to do so. Thus,zip codes, even though they look like they would be numeric numbers should be identified as a string data type to eliminate problems such as zip codes that start with zero(ex. 08545). Assigning the correct data type to the expected value is an important part of working with PHP.
There are three categories of data types in PHP : Scalar, Compound and Special.
In this blog, we are going to discuss Scalar data types.
Scalar Data Type:
A Scalar Data Type is identified as being able to hold one value at a time.In PHP,There are four data types that fall into this category.
1) Boolean - A Boolean value is a truth value,either ‘true’ or ‘false’,Often code 1 and 0,respectively. When a value is assigned the boolean data type,there are only two responses that the value can be assigned: True(1) or False(0). If a value assigned anything other then these two values,it will default to True(1).
//Example:
<?php
$wealthy= false; // $wealthy is a False
$wealthy= 1; // $wealthy is a true
$wealthy= 5; // $wealthy is a true
$wealthy= -1; // $wealthy is a true
$wealthy= 0; // $wealthy is a false
?>
2) Integer - An Integer is a whole numeric data type(meaning it does not contain any functions) that can be set to decimal (base 10),octal(base 8) or hexadecimal (base 16). The following demonstrate some of the values that can be assigned the numeric data type:
//Example :
<?php
$Temperature =22; //$temperature is an integer
$x= -58975; //$x is an integer number
$y= 06548; //$y is an integer number (octal)
$z= 0X5E5B; //$z is an integer number (hexadecimal)
?>
The Maximum number that can be assigned to an integer is based on the system.32 bit systems have a maximum signed integer range of -2147483648 to 2147483647.The Maximum signed value for 64 bit systems is 9223372036854775807. Any value that exceeds the maximum integer size will be assigned the float data type.
3) Float - Floating point numbers are any numbers that have a fractional component exceed the integer maximum value. This data type also referred to as floats,doubles or real numbers. Floats are used to represent such numbers as monetary values,distance,weights,scientific notation and a host of others values.
//Example:
<?php
$TotalCost= 67.50; //$TotalCost is a float.
$Distance =4.5e5 //$Distance is a float equal to 450000
$Weight =92.0 //$Weight is a float.
$Diff=2.34E+12 //$Diff is a float (2340000000000).
?>
The size of a floating number is dependent by the system being used. In addition, floating point numbers are not accurate as integer numbers when it comes to precision. Even simple number like 0.2 and 0.17 can not be converted into their binary equivalents without a loss of precision.
4) String -A String data type is a series of characters that are associated with each other in a defined order. There is no limits to the length of astringg data value. The following represent some of the Values that can be assigned to a string data type:
//Example:
<?php
$Name='Rafi Ahmad'; // $Name is a string.
$Value='$23.20'; //$Value is a string.
$x="This is a 'Test'"; // $x is a string.
?>
The Characters that can be used in PHP string data types are limited to the ISO-8859-1 or latin-1 character set.This character set allows for 256 (the size of byte) different possible characters(191 of them that are actually printable), however, there is support in PHP to encode the string to the UTF-8 standard(UTF-8 is a standard mechanism used by Unicode for encoding wide character value into a byte stream ).
In my next blog, I will describe Compound and Special data type.
I hope this will help you.
0 Comment(s)