Saturday, 16 March 2019

Variables of PHP

php Veriables is one of the most intaresting portion of php tutorial, In case of  php Veriables the Veriables name is starts with $ like as $name,
there is no particular seperation for int or float or string in PHP Veriables there is only one syntex that the Veriables name will starts with $
Example:
<?php
$name = "Name Goes Here";
echo $name;
?>

How to Write a php Veriables.

to write a php Veriables developer have to maintain some roles, that the Veriables name must starts with a character after the sign of $
There should not a number or _ (underscore) at the begining but the _ or number can be used at the middle portion or at the end of a php Veriables.
In case of a php Veriables user can use uppercase or lower case both in any position of the Veriables name.
and user must not use a - (minas) or # or any special charanter except _ (underscore) while declaring php Veriables.
Example:
<?php
$name = "My Name";
$first_name = "My First Name";
$last_name_2 = "My Last Name";
$middleName = "My Middle Name";
?>

php Veriable inside or outside of Scope.

if a variable is declared outside of a function and call the theriable inside of the function it will give an error.
Example:
<?php
$name = "My Name";
function callName(){
echo $name; //will generate an error
}
callName();
echo $name; //will Print 'My name'
?>
similarly if a veriable is desclared inside of a function and user wanted to use the veriable outside of the function even after calling the function, it will generate an error.
Example:
<?php
function callName(){
$name = "My Name";
echo $name; //will Print 'My name'
}
callName();
echo $name; //will generate an error
?>

php Global Veriable.

To declare a global veriable in php the syntax is global and then the veriable name starting with $, user can declare the veriable and assign a value also befire make the veriable global and after making the veriable global the veriable can be used globaly. and any time the value changed of the veriable, after declaring the veriable as global it will assign the new value into the global veriable , maybe its inside or outside of the scope.
Example:
<?php
$a = 15;
$b = 20;
function myGlobalAddition() {
global $a, $b;
$b = $a + $b;
}
myGlobalAddition();
echo $b; //the outputs will be 35
?>
After declaring a veriable outside of scope, PHP also stores all global variables in an array, the name of the array is $GLOBALS['here will be the veriable name without $ sign']. The index holds the name of the variable. This array is also accessible from inside functions and can be used to update global variables.
Example:
<?php
$a = 15;
$b = 20;
function myGlobalAddition() {
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
myGlobalAddition();
echo $b; //the outputs will be 35
?>

Static Veriable in php 

Normaly when a function is exicuted all the veriable inside the function is gurbage cullected, but sometime we needed the loval veriable outside of the scope, at that time we can use static veriable. and to declare a veriable as static the syntex is static and then the veriable name.
Example:
<?php
function myStaticIncrement() {
static $a;
$a = 1;
echo $a;
$a++;
}
myStaticIncrement();
myStaticIncrement();
myStaticIncrement();
//the outputs will be
// 1
// 2
// 3
?>

How to declare an array in php ,

There is mainly two type of array in php, and they are signed and unsigned array, the signed array is the index of the array is given by user and it may be string or number but they must be unic for a particular array. and similarly an unsign array's index is automatically generated from 0 to the n-1 where n is the size of the array. and in both case the array is declared as veriable name equals array().
And there is multidimension array also and it is a array inside an array. the example is given below.
Example:
<?php
$unsigned_arr = array('red', 'blue', 'green'); // its a unsigned array
$signed_arr = array('clr1' => 'red', 'clr2' => 'blue', 'clr3' => 'green');//Its a signed array
$multy_dimensional_array = array(
'arr_ky1'=>array('red', 'blue', 'green'),
'arr_ky1'=>array('clr1'=>'red', 'clr2'=>'blue', 'clr3'=>'green')
); // The array is a multi dimensional array
?>
In that post we have learned about PHP veriables, and I hope its helped you to learn php.
Thanks for Reading.

No comments:

Post a Comment