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;


Tuesday 17 December 2013

PHP Variable & Data Types | Chapter 2



What Are Variables?
  •  Variables are used to store data.
  • A variable in PHP always starts with a $ sign.
  • PHP Variable can stored 8 types of data types.


php variables and data types











Rules of Variables
  • A variable name should not start with any special character or spaces. 
  • A variable name should start with a letter, number or underscore followed by any letter, number or underscore. An underscore is not treated as special character. 
  • A variable in PHP doesn't know in advance that what type of data type it will be storing as in c++,c. 
  •  Variables are not necessary to be declared before assignment of any value.  

php variables and data types





General Syntax -
$variablename=value;

Example 1-
<?
$test=1;
 echo $test;
?>

Output - 1



Example 2 -
<?php
$test=1;
$test1=$test;
echo $test1;
?>

Output - 1

In this example we create a variable test & store an integer to it and then we create an another variable test1 & assign it the value of test.  Now $test1 holds or stores the value of test which is 1. This example was just to show that the value of one variable can be stored in the another just by pointing the variable name instead of the value as shown in the example.


Example 3 -
<?php
$one=1;
$two=2;
$result=$one+$two;
echo $result;
?>

Output - 3

In the above example we have created two variables & assigned integer values to it. Now we create an another variable result & assign it the value we get after the addition of the first & second variable. For the addition of two variables we use the simple addition arithmetic operator. 1+2 is 3 so 3 gets stored in variable $result.