PHP variables

PHP variables

PHP variables are used to hold values or expressions as temporary.
Variable is a memory location variable is a contain which is use to store or initialize single data.
the data can be change according to requirement.
A variable can have a short name, like a, b c, or a more descriptive name, like StudentName, StudentRollno.

Rules for PHP variable names:
1. We have to use $ symbol to initialize variables in php.
2. We are restricted to use $ symbol every time with variables in PHP.
3. Variables name are case sensitive.
4. A variable name does not contain any type of special character white spaces
5. Variable name must be alpha numeric.
6. If we want separate two name in a single variable we have to use (_) Underscore Symbol.
7. $ Symbol represent memory at the run time.
8. In the PHP we do not need to declare datatype before variable initialize.
9. We can also start variable name with ( _ ) underscore in PHP.

Declaring or Initialize PHP Variables.
Declaring variable in php do not need any command for declaring a variable. just use $ symbol and declare variable.

$StudentName=”PHPKIDA”;
$RollNo=20;

PHP Variable Scope

The scope of a variable is the part of the PP script in which the variable can be referenced.
PHP has four different variable scopes:

1. local
2. global
3. static
4. parameter

Local Scope

A variable declared within a PHP function becomes local and it can only be accessed within that function not access outside of the function.
You can have local variables with the same name in different functions,
because local variables are only recognized by the function in which they are declared.
Local variables are deleted as soon as the function is completed.

Global Scope

Global scope refers to any variable that is defined outside of any function.
Global variables can be accessed from any part of the program that is not inside a function.
To access a global variable from within a function, use the global keyword.

<?php
$a = 20; // global scope variable
function Testing()
{
echo global $a; // reference to global scope variable
}
Testing();
?>

HP also stores all global variables in an array called $GLOBALS[ ].

Its index is the name of the variable.
This array is also accessible from within functions and can be used to update global variables directly:
global $a;
$a=”abc”;

// is the same as

$GLOBALS[“a”]=”abc”;

Static Scope

When a function is completed, all of its variables are normally deleted.
However, sometimes you want a local variable to not be deleted.

To do this, use the static keyword when you first declare the variable:
static $rememberMe;

Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.

Note: The variable is still local to the function.
Parameters

A parameter is a local variable whose value is passed to the function by the calling code.

Parameters are declared in a parameter list as part of the function declaration:
function myTest($para1,$para2,…)
{
// function code
}

Parameters are also called arguments. We will discuss them in more detail when we talk about

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Free online tool to download Instagram videos, photos and save Instagram videos, photos to your device. It's free & easy to use.

Download Now

CURRENCY CONVERTER

Our real time currency converter calculator will convert your money based on current values from around the world. It's free & easy to use.

Convert Now

ROBOST.TXT GEN.

Create robots txt file that help search engines to crawling and indexing your site with help of our free online Robots.txt Generator tool.

Create Now

HTML MINIFIER

Free online tool to minify or compress html code and reduce the size of html code in few clicks. It's free & easy to use.

Minify Now

CSS MINIFIER

Free online tool to minify or compress css code and reduce the size of css code in few clicks. It's free & easy to use.

Minify Now

JAVASCRIPT MINIFIER

Free online tool to minify or compress javascript code and reduce the size of javascript code in few clicks. It's free & easy to use.

Minify Now

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.