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

PPTX
Constructor and Types of Constructors
PPT
C++ Arrays
PPTX
Procedural programming
PPTX
PHP Variables and scopes
PPSX
Type conversion
PPTX
Introduction to php
PPTX
Pointers in c++
PPT
PHP - Introduction to Object Oriented Programming with PHP
Constructor and Types of Constructors
C++ Arrays
Procedural programming
PHP Variables and scopes
Type conversion
Introduction to php
Pointers in c++
PHP - Introduction to Object Oriented Programming with PHP

What's hot (20)

PPTX
PHP FUNCTIONS
PPTX
Statements and Conditions in PHP
PPTX
PPTX
Static Data Members and Member Functions
PDF
Php array
PPTX
Introduction to php
PPT
Generics in java
PPTX
PDF
Arrays in Java
PPT
structure and union
PPTX
Constants in java
PPT
File handling in c
PPTX
Two-dimensional array in java
PDF
Classes and objects
PDF
Xml schema
PPT
Introduction to oop
PDF
Php introduction
PPTX
Constructor in java
PPTX
Functions in c++
PPTX
Polymorphism in c++(ppt)
PHP FUNCTIONS
Statements and Conditions in PHP
Static Data Members and Member Functions
Php array
Introduction to php
Generics in java
Arrays in Java
structure and union
Constants in java
File handling in c
Two-dimensional array in java
Classes and objects
Xml schema
Introduction to oop
Php introduction
Constructor in java
Functions in c++
Polymorphism in c++(ppt)
Ad

Similar to Variable scope in php (20)

PPTX
Function Returns
PPT
PHPVariables_075026.ppt
PPTX
GlobalLocal_ExceptionHndlng_InPython.pptx
PPT
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
PPT
Chapter Introduction to Modular Programming.ppt
PPT
PPT
FUNCTIONS IN c++ PPT
PPTX
Introduction to PHP and MySql basics.pptx
PPTX
Lecture 5 Functions all about business categories
PDF
Introduction to PHP_Slides by Lesley_Bonyo.pdf
PDF
Chap 4 PHP.pdf
PPTX
Modularisation techniques new
PPTX
php basic part one
PPTX
PHP Basics
PPT
9780538745840 ppt ch02
PPTX
DOCX
Notes of Important Programming Fundamental Questions
PPTX
Global variables, sorting static variables,function and arrays,
PPTX
Object oriented java script
Function Returns
PHPVariables_075026.ppt
GlobalLocal_ExceptionHndlng_InPython.pptx
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
Chapter Introduction to Modular Programming.ppt
FUNCTIONS IN c++ PPT
Introduction to PHP and MySql basics.pptx
Lecture 5 Functions all about business categories
Introduction to PHP_Slides by Lesley_Bonyo.pdf
Chap 4 PHP.pdf
Modularisation techniques new
php basic part one
PHP Basics
9780538745840 ppt ch02
Notes of Important Programming Fundamental Questions
Global variables, sorting static variables,function and arrays,
Object oriented java script
Ad

More from MUHAMMED MASHAHIL PUKKUNNUMMAL (14)

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

Recently uploaded (20)

PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
PPT on Performance Review to get promotions
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PPTX
Current and future trends in Computer Vision.pptx
PPTX
Safety Seminar civil to be ensured for safe working.
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Artificial Intelligence
PPTX
Construction Project Organization Group 2.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
Project quality management in manufacturing
PDF
Well-logging-methods_new................
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPT on Performance Review to get promotions
Fundamentals of safety and accident prevention -final (1).pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Current and future trends in Computer Vision.pptx
Safety Seminar civil to be ensured for safe working.
Internet of Things (IOT) - A guide to understanding
Artificial Intelligence
Construction Project Organization Group 2.pptx
Foundation to blockchain - A guide to Blockchain Tech
R24 SURVEYING LAB MANUAL for civil enggi
Embodied AI: Ushering in the Next Era of Intelligent Systems
Project quality management in manufacturing
Well-logging-methods_new................

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