Unit I
Unit I
With PHP
?>
Local variables: The variables declared within a function are called local variables to that function and
have their scope only in that particular function. In simple words, it cannot be accessed outside that
function. Any declaration of a variable outside the function with the same name as that of the one
within the function is a completely different variable. We will learn about functions in detail in later
articles. For now, consider a function as a block of statements.
<?php
$num = 60;
function local_var()
{ // This $num is local to this function
// the variable $num outside this function
// is a completely different variable
$num = 50;
echo "local num = $num \n";}
local_var();
// $num outside function local_var() is a
// completely different Variable than that of
// inside local_var()
echo "Variable num outside local_var() is $num \n";
?>
● Global variables: The variables declared outside a function are called global
variables. These variables can be accessed directly outside a function. To get
access within a function we need to use the “global” keyword before the variable
to refer to the global variable.
<?php
$num = 20;
// function to demonstrate use of global variable
function global_var()
{ // we have to use global keyword before
// the variable $num to access within
// the function
global $num;
echo "Variable num inside function : $num \n";}
global_var();
echo "Variable num outside function : $num \n";
?>
Static variable: It is the characteristic of PHP to delete the variable, once it completes its execution and
the memory is freed. But sometimes we need to store the variables even after the completion of
function execution. To do this we use the static keywords and the variables are then called static
variables. PHP associates a data type depending on the value for the 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();
?>
PHP Operators
Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
● Arithmetic operators
● Assignment operators
● Comparison operators
● Increment/Decrement operators
● Logical operators
● String operators
● Array operators
Arithmetic Operators
The PHP arithmetic operators are used with numeric values to perform common
arithmetical operations, such as addition, subtraction, multiplication etc.
<?php
$x = 29; // Variable 1
$y = 4; // Variable 2
// Some arithmetic operations on
// these two variables
echo ($x + $y), "\n";
echo($x - $y), "\n";
echo($x * $y), "\n";
echo($x / $y), "\n";
echo($x % $y), "\n";
?>
Assignment Operators
The PHP assignment operators are used with numeric values to write a value to
a variable.The basic assignment operator in PHP is "=". It means that the left
operand gets set to the value of the assignment expression on the right.
Comparison Operators
The PHP comparison operators are used to compare two values (number or string).
Increment / Decrement Operators
The PHP increment operators are used to increment a variable's value. The PHP
decrement operators are used to decrement a variable's value.
Logical Operators
The PHP logical operators are used to combine conditional statements.
String Operators
PHP has two operators that are specially designed for strings.
Array Operators
The PHP array operators are used to compare arrays.
PHP Constants
A constant is an identifier (name) for a simple value. The value cannot
be changed during the script. A valid constant name starts with a letter
or underscore (no $ sign before the constant name).
Note: Unlike variables, constants are automatically global across the
entire script.