Variable in php
What is Variable

Variable is just like a container for data. We can say we store data in variable. In PHP, there are two type of variable. Predefine variable(SuperGlobal Variable) and User define Variable.

1. SuperGlobal Variable:- This is predefine variable and these variables are automatically available to any PHP program.  There is no need to declare and define these variable in the program.These are following  type of SuperGlobal Variable.

$_GET
When we send data through form tag by get method and send data through querystring , then we  reterive data by this superglobal variable .

$_POST
When we send data through form tag by post method then we reterive data by  this superglobal variable.

$_COOKIE
We reterive cookies data through this superglobal variable.

$_SESSION
We  reterive session data through this superglobal variable.

$_FILES
When we use file browse html control then we reterive file name,type and size by this superglobal variable.

$_ENV
We reterive variables available from the server environment.

$_REQUEST
This superglobal contains the data  that sent by get method, post method and query string.

$_SERVER
This is an array variable that contain information about webserver such file path,etc.

we discuss each superglobal variable in details in next classes.

2. User define Variable:- This variable is declare and define by programmer

for example:
$num=34;

$num is a variable and 34 is a data and this data store in variable $num.

How to print variable data.
echo $num;

here echo statement print variable data.
output:-
34

another statement:
echo "Number is $num";

output:-
Number is 34.

Example 1:-

<?php
$num=34;
echo $num;
?>

Output:-
34

Example 2:-

<?php
$num=34;
echo "Number is $num";
?>

Output:-
Number is 34

Example 3:-

<?php
$num1=34;
$num2=45;
$num3=$num1+$num2;
echo "Result is $num3";
?>

Output:-
Result is 79

Example 4:-

<?php
$num1=34;
$num2=45;
$num3=$num1+$num2;
echo "First Number is $num1"."<br>";
echo "Second Number is $num2"."<br>";
echo "<b>Result is $num3</b>";
?>

Output:-
First Number is 34
Second Number is 45
Result is 79.


Previous
Next