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.




Variables Scope

- SuperGlobal
- Global
- Local
- Static


PHP Data Types

The are divided into two types
- Scalar Data Type
- Compound Data Type
- Special Types

Scalar Data Type
  • Boolean
  • Integer
  • Float
  • String

Compound Data Type
  • Array
  • Object

Special Data Type
  • Resources 
  • NULL

To be continued...

No comments:

Post a Comment