Php Variables
Variable is nothing it is just name of the memory
location. A Variable is simply a container that is
used to store both numeric and non-numeric
information.
Rules for Variable declaration
1. Variables in PHP starts with a dollar($) sign, followed by the
name of the variable.
2. The variable name must begin with a letter or the
underscore character.
3. A variable name can only contain alpha-numeric characters
and underscores (A z, 0-9, and _ )
4. A variable name should not contain space
Declaring Variables in PHP
Assigning a value to a variable in PHP is quite east: use the
equality(=) symbol, which also to the PHP's assignment
operators. This assign value on the right side of the equation to
the variable on the left. A variable is created the moment you
assign a value to it:
Variable Scope
The scope of the variable is the area within which the variable
has been created. Based on this, a variable can either have a
local scope, a global scope, or a static scope in PHP.
Global Variable:
A variable that was created in the main body of the code and
that can be accessed anywhere in the program is called a
Global Variable. Global variables can be directly accessed or
used in or outside of a function with the global keyword before
the variable. However, we can also call them without the global
keyword.
<?php
$name = "Harry Bhai"; // Global Variable
function global_var()
{
global $name;
echo "Variable inside the function: " . $name;
echo "</br>";
}
global_var();
echo "Variable outside the function: " . $name;
?>
Local Variable:
A local variable is created within a function and can only be
used inside the function. This means that these variables
cannot be accessed outside the function, as they have local
scope.
<?php
function mytest()
{
$capital = "Delhi";
echo "Capital of India is: " . $capital;
}
mytest(); // Calling the function
// Using $capital outside the function will generate an error
echo $capital;
?>
Static Variable:
PHP has a feature that deletes the variable once it has finished
execution and frees the memory. When we need a local variable
that can store its value even after the execution, we use
the static keyword before it, and the variable is called a static
variable.
<?php
// Function to demonstrate static variables
function static_var() {
// Static Variable
static $num = 5;
$sum = 2;
$sum++;
$num++;
echo $num, "\n";
echo $sum, "\n";
}
// First function call
static_var();
// Second function call
static_var();
?>
Comments in PHP
A comment in PHP code is a line that is not executed as a part
of the program. Its only purpose is to be read by someone who
is looking at the code.
Comments can be used to:
Let others understand your code
Remind yourself of what you did - Most programmers have
experienced coming back to their own work a year or two
later and having to re-figure out what they did. Comments
can remind you of what you were thinking when you wrote
the code
Leave out some parts of your code
// This is a single-line comment
This is also a single-line comment
/* This is a multi-line comment */