Lecture 3 Using variable in PHP
Lecture 3 Using variable in PHP
COMP203TH
Lecture: 3
Using Variables in PHP
When the PHP parser reads a script, it executes only the code found between
PHP tags, everything outside these tags is ignored by the parser and returned
as is. This makes it extremely easy to embed PHP code within an HTML
document to create Web Pages.
e.g.
<html>
<body>
<?php
?>
</body>
</html>
All variables in PHP are denoted with a leading dollar sign ($).
Variable name must begin with a letter or underscore character
optionally followed by more letters, numbers or underscore characters.
Common punctuation characters such as commas, quotation marks or
periods are not permitted in variable names neither are spaces.
Use the equality (=) symbol, which is PHP’s assignment operator. This
assigns the value on the right side of the equation to the variable on the left.
<html>
<head>
<title> </title>
</head>
<body>
<?php
$name = „Victor‟
?>
</body>
</html>
In this example, the variable $name is assigned the value ‘Victor’. The echo
statement is then used to print the value of this variable to the Web page.
Important Points:
Variables in PHP do not have intrinsic types i.e. a variable does not
know in advance whether it will be used to store a number or a
string of characters. In other words, PHP is a loosely typed
language, it means PHP automatically converts the variable to its
correct data type.
Variable names are case sensitive.
You can also assign a variable the value of another variable or the result of a
calculation.
e.g.
<?php
$now= 2020;
$currentyear = $now;
$nextyear =$currentyear+1;
?>
Note:
code to be executed;
1. Local Variable
2. Global Variable
3. Static Variable
Local Variable:
The variables that are declared within a function are called local variables for
that function. These local variables have their scope only in that particular
function in which they are declared i.e. they cannot be accessed outside the
function.
e.g.
<?php
function local_var()
local_var();
?>
Global Variable:
The global variables are the variables that are declared outside the function.
These variables can be accessed anywhere in the program.
To access the global variable within a function, use the global keyword before
the variable. However, these variables can be directly accessed or used outside
the function without any keyword. Therefore, there is no need to use any
keyword to access a global variable outside the function.
e.g.
<?php
functio global_var()
global $name;
echo “</br>”;
global_var();
?>
Without using the global keyword, if you try to access a global variable inside
the function, it will generate an error that the variable is undefined.
Another way to use the global variable inside the function is predefined
$GLOBALS array.
e.g.
<?php
function global_var()
{
$num = $GLOBALS[„num1‟] + $GLOBALS[„num2‟];
global_var();
?>
If two variables local and global have the same name, then the local
variable has higher priority than the global variable inside the function.
Static Variable:
We use static keyword before the variable to define a variable and this variable
is called static variable. It does not free its memory after the program execution
leaves the scope.
e.g.
<?php
function static_var()
$num1++;
$num2++;
echo “Static”.$num1.”</br>”;
echo “Non-static:”.$num2.”</br>”;
static_var();
static_var();
?>
Here, in the output you will observe $num1 regularly increments after each
function call, whereas $num2 does not. Because $num2 is not a static
variable, so it freed its memory after the execution of each function call.