SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
VARIABLE SCOPE IN PHP
MUHAMMED MASHAHIL P
ASSISTANT PROFESSOR
CAS VAZHAKKAD
Variable scope
PHP has three different variable scopes:
• local
• global
• static
Local
• A variable declared within a PHP function is local
• It can only be accessed within that function. (the variable
has local scope):
• The script above will not produce any output because the
echo statement refers to the local scope variable $a, which
has not been assigned a value within this scope.
• 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
• Global scope refers to any variable that is defined
outside of any function.
• Global variables can be accessed from any part of
the script that is not inside a function.
• To access a global variable from within a function,
use the global keyword:
• The script above will output 15
Continues…..
• PHP also stores all global variables in an array called
$GLOBALS[index].
• 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.
The example above can be rewritten as this:
Static
• 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.
Continues…..
Output:-
0
1
2
3
END

More Related Content

What's hot (20)

PDF
Php introduction
krishnapriya Tadepalli
 
PPTX
Php basics
Jamshid Hashimi
 
PPTX
Php.ppt
Nidhi mishra
 
PPTX
Files in php
sana mateen
 
PPTX
Cascading style sheets (CSS-Web Technology)
Timbal Mayank
 
PPTX
PHP slides
Farzad Wadia
 
PPTX
Uploading a file with php
Muhamad Al Imran
 
PPT
Php Ppt
vsnmurthy
 
PDF
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
PPTX
PROGRAMMING IN JAVA unit 1.pptx
SeethaDinesh
 
PDF
jQuery for beginners
Arulmurugan Rajaraman
 
PPT
CSS.ppt
MukulSingh293955
 
PPT
Introduction to PHP.ppt
SanthiNivas
 
PPTX
Html5 and-css3-overview
Jacob Nelson
 
PPTX
Sessions in php
Mudasir Syed
 
PPTX
Introduction to xampp
Jin Castor
 
PPT
01 Php Introduction
Geshan Manandhar
 
PPSX
HTML5 - Forms
tina1357
 
Php introduction
krishnapriya Tadepalli
 
Php basics
Jamshid Hashimi
 
Php.ppt
Nidhi mishra
 
Files in php
sana mateen
 
Cascading style sheets (CSS-Web Technology)
Timbal Mayank
 
PHP slides
Farzad Wadia
 
Uploading a file with php
Muhamad Al Imran
 
Php Ppt
vsnmurthy
 
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
PROGRAMMING IN JAVA unit 1.pptx
SeethaDinesh
 
jQuery for beginners
Arulmurugan Rajaraman
 
Introduction to PHP.ppt
SanthiNivas
 
Html5 and-css3-overview
Jacob Nelson
 
Sessions in php
Mudasir Syed
 
Introduction to xampp
Jin Castor
 
01 Php Introduction
Geshan Manandhar
 
HTML5 - Forms
tina1357
 

Similar to Variable scope in php (20)

PPTX
Function Returns
primeteacher32
 
PPT
PHPVariables_075026.ppt
06Vinit
 
PPTX
GlobalLocal_ExceptionHndlng_InPython.pptx
DrBAMASCSE
 
PPT
Chapter Introduction to Modular Programming.ppt
AmanuelZewdie4
 
PPT
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
harinipradeep15
 
PPT
Basic c++
Maria Stella Solon
 
PPT
FUNCTIONS IN c++ PPT
03062679929
 
PPTX
Introduction to PHP and MySql basics.pptx
PriyankaKupneshi
 
PPTX
Lecture 5 Functions all about business categories
suhaanhakani69
 
PDF
Introduction to PHP_Slides by Lesley_Bonyo.pdf
MacSila
 
PDF
Chap 4 PHP.pdf
HASENSEID
 
PPTX
Modularisation techniques new
Jeet Thombare
 
PPTX
php basic part one
jeweltutin
 
PPTX
PHP Basics
Bhanuka Uyanage
 
PPT
9780538745840 ppt ch02
Terry Yoast
 
PPTX
Functions
Munazza-Mah-Jabeen
 
DOCX
Notes of Important Programming Fundamental Questions
Adeel Rasheed
 
PPTX
Global variables, sorting static variables,function and arrays,
Ahmad55ali
 
PPTX
Object oriented java script
vivek p s
 
Function Returns
primeteacher32
 
PHPVariables_075026.ppt
06Vinit
 
GlobalLocal_ExceptionHndlng_InPython.pptx
DrBAMASCSE
 
Chapter Introduction to Modular Programming.ppt
AmanuelZewdie4
 
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
harinipradeep15
 
FUNCTIONS IN c++ PPT
03062679929
 
Introduction to PHP and MySql basics.pptx
PriyankaKupneshi
 
Lecture 5 Functions all about business categories
suhaanhakani69
 
Introduction to PHP_Slides by Lesley_Bonyo.pdf
MacSila
 
Chap 4 PHP.pdf
HASENSEID
 
Modularisation techniques new
Jeet Thombare
 
php basic part one
jeweltutin
 
PHP Basics
Bhanuka Uyanage
 
9780538745840 ppt ch02
Terry Yoast
 
Notes of Important Programming Fundamental Questions
Adeel Rasheed
 
Global variables, sorting static variables,function and arrays,
Ahmad55ali
 
Object oriented java script
vivek p s
 
Ad

More from MUHAMMED MASHAHIL PUKKUNNUMMAL (14)

PPTX
IO and threads Java
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PDF
flow of control python.pdf
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PDF
Database Management using MS Access.pdf
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PPTX
CA-Web Hosting-Slide.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PDF
loops python.pdf
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PDF
flow of control python.pdf
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PPTX
java 4 Part 1 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PPTX
Java 3 Computer Science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PPTX
Java 2 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PPTX
java part 1 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PPTX
Vardump and printr in php
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PPTX
Basic sql Commands
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PPTX
Unit1 DBMS Introduction
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
IO and threads Java
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
flow of control python.pdf
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Database Management using MS Access.pdf
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
CA-Web Hosting-Slide.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
flow of control python.pdf
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
java 4 Part 1 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Java 3 Computer Science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Java 2 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
java part 1 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Vardump and printr in php
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Basic sql Commands
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Unit1 DBMS Introduction
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Ad

Recently uploaded (20)

PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PPT
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PPTX
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Amity University, Patna
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PPTX
Big Data and Data Science hype .pptx
SUNEEL37
 
PPTX
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PDF
smart lot access control system with eye
rasabzahra
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Amity University, Patna
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
Big Data and Data Science hype .pptx
SUNEEL37
 
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
Design Thinking basics for Engineers.pdf
CMR University
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
smart lot access control system with eye
rasabzahra
 

Variable scope in php

  • 1. VARIABLE SCOPE IN PHP MUHAMMED MASHAHIL P ASSISTANT PROFESSOR CAS VAZHAKKAD
  • 2. Variable scope PHP has three different variable scopes: • local • global • static
  • 3. Local • A variable declared within a PHP function is local • It can only be accessed within that function. (the variable has local scope): • The script above will not produce any output because the echo statement refers to the local scope variable $a, which has not been assigned a value within this scope. • 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.
  • 4. Global • Global scope refers to any variable that is defined outside of any function. • Global variables can be accessed from any part of the script that is not inside a function. • To access a global variable from within a function, use the global keyword: • The script above will output 15
  • 5. Continues….. • PHP also stores all global variables in an array called $GLOBALS[index]. • 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. The example above can be rewritten as this:
  • 6. Static • 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.
  • 8. END