Handling data

Every programming language has its own set of data types that help developers to write programs. A data type is a classification of data . This could mean categorizing the the type of data as an amount of money, time or percentage. In PHP the data type can be broadly classified as scalar and compound data types.

Scalar data type

Four type of data comes under the category of scalar data type: Boolean, integer, float and string.


The Boolean data type can either be true or false. The numeric representation of the Boolean data type is 1 for true and 0 for false. The the fallowing example uses this data type:

$myVal = True; // Boolean variable type.


An integer data type consist of numbers. You can use both negative or positive numbers like [...-2,-1,0,1,2,3,...]. The size of an integer in PHP solely depends on the operating system that you are using. typically , the size of an integer can be up to 2 billion. In the fallowing code, $myVal is an integer type variable with a value of 9:

 $myVal =9; //Is an integer.


A float is a data type that is also use for storing numbers. Usually floats are use to express decimal numbers, you can use the integer data type for non decimal numbers. Floats are also known as doubles or real numbers and have a precision of up 14 decimal places. In the fallowing code ,$floatVal is a float type variable and has a value of 9.98765:

$ floatVal = 9.98765 // Is a float type variable


A string can be best defined as a series of characters. In PHP, a character is nothing but a byte. There are 256 different types of characters that can be used in strings. There is no maximum limit specification in relation to strings to PHP; therefore you need not worry about a very long string .There are two ways to specify a string discussed on fallowing sections.

Single quoted

You can use single quotes to specify a simple string. How ever when a string is enclosed in single quotes and it also contains some text enclosed in double quotes, you must use the backslash escape character to escape the single quotes. In other wards when you don't want to parser to treat single quotes as syntax and display them as part of the output of the code, you need o use a back slash before the single quotes.

Lets consider a simple example to understand how single quotes are used to specify a string:

      echo 'This is my First String'; // will print the string on the screen.

Now consider an example where double quotes are used within single quotes and single quotes is represent in the text enclosed in double quotes:

echo 'Rishi says: "I'm doing well "'; , //will display error.

When you try to execute this statement, an error message will appear on the screen. This is because when you use a single quote within double quotes, you need to escape the single quote with a backslash. In the fallowing statement is written correctly:
echo 'Rishi says: "I\'m doing well "'; , //is Correct now.

The output will be Rishi says: "I'm doing well".

Another thing that you should remember when using single quoted string is that such string don't support variable expansion: If you use a name of variable within single quote and try to print the value of a variable, the name of the variable will be printed instead of its value. Consider the fallowing example:
  echo 'Hello $name';

The output will be Hello $name.
The correct syntax is
  echo "Hello $name";
The output will be Hello Rishi.

Double quoted

A double quoted strings supports several other escape characters as follows
 - \n: new line
 - \r: carriage
 - \t: Horizontal tab
 - \\: backslash
 - \$: Dollar sign
 - \": Double quote

comments powered by Disqus