Thursday 26 December 2013

PHP Variable & Data Types | Chapter 2 Cont.



Boolean - It is a data type containing two values which is TRUE (1) & FALSE (0) . They are mainly used to compare conditions for use in conditional statements

php boolean













 General Syntax -
$variable=True;
$variable=False;



 Integer - It is a data type containing numbers. Integers can be defined in three formats which is Decimal, hexadecimal & octal. An integer can be both positive or negative.

General Syntax -
$variable = decimal number;
$variable = negative number ;
$variable = octal number ;
$variable = hexadecimal number;




Example -
<?php
$variable = 1234; // decimal number (base 10)
$variable = -123; // a negative number
$variable = 0123; // octal number (equivalent to 83 decimal) (base 8)
$variable = 0x1A; // hexadecimal number (equivalent to 26 decimal) (base 16)
?>




Float - A number containing a decimal point is known as Float Value. They can be positive or negative.

General Syntax -
$variable = 10.365;





 String -A set of characters is known as string.

$string="Welcome to PHP-Sec";
$string1="Learn PHP Online";

Output -
Welcome to PHP-Sec
Learn PHP Online



When strings are within double quotes they replace the variables with their values whereas when we use single quotes then  the variable is just printed on the webpage. Some special character sequences are interpreted only when we use the double quotes like "\n".


$variable="welcome";
$string='$variable to PHP-Sec';
$string1='Learn PHP Online\n';

Output -
$variable to PHP-Sec
Learn PHP Online\n






Array - Array is an collection of similar data types which is stored in contiguous memory locations sharing the common name. It is capable of storing more than one values at a time. All the values are stored in a single variable.

$variable=array("one","two","three");
echo "array contents are" . $variable[0] . "," . $variable [1] . "," .  $variable[2] . ".";

Output-
array contents are one, two, three.


PHP Arrays are of 3 types.

  - Indexed arrays 
  - Associative arrays
  - Multidimensional arrays




NOTE:   In this chapter we haven't gone deep into the array concept. In our next chapter(Arrays in PHP) we will be learning more about arrays.





No comments:

Post a Comment