Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
What are the differences between PHP constants and variables ?
Next article icon

What are the different scopes of variables in PHP ?

Last Updated : 06 May, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Variable Scopes: The scope of a variable is defined as its extent in the program within which it can be accessed, i.e. the scope of a variable is the portion of the program within which it is visible or can be accessed. Depending on the scopes, PHP has three variable scopes. 

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. For now, consider a function as a block of statements. 

Example:

PHP
<?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 <br>"; }  local_var();  // $num outside function local_var() is a // completely different variable than that of // inside local_var() echo "Variable num outside local_var() function is $num \n";  ?> 

Output:

local num = 50   Variable num outside local_var() function is 60

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.

Example: 

PHP
<?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";  ?> 

Output:

Variable num inside function : 20   Variable num outside function : 20 

Static variable: It is the characteristic of PHP to delete the variable, once it completes its execution and the memory is free. But sometimes we need to store the variables even after the completion of function execution. To do this, we use static keywords and the variables are called static variables. PHP associates a data type depending on the value for the variable. 

Example: 

PHP
<?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();  ?> 

Output:

6  3  7  3

Next Article
What are the differences between PHP constants and variables ?
author
sravankumar_171fa07058
Improve
Article Tags :
  • Web Technologies
  • PHP
  • PHP-basics
  • PHP-Questions

Similar Reads

  • What is the use of the $_REQUEST variable in PHP ?
    Uses of PHP $_REQUEST: The PHP $_REQUEST is a PHP superglobal variable that is used to collect the data after submitting the HTML forms as the $_REQUEST variable is useful to read the data from the submitted HTML open forms.$_REQUEST is an associative array that by default contains contents of an $_
    2 min read
  • Shell Scripting - Different types of Variables
    The shell is a command-line interpreter for Linux and Unix systems. It provides an interface between the user and the kernel and executes commands. A sequence of commands can be written in a file for execution in the shell. It is called shell scripting. It helps to automate tasks in Linux. Scripting
    4 min read
  • What's the difference between and in PHP ?
    There are many types of line-ending characters that are used in PHP. They differ depending upon the operating system and editors using them. It's also based on how apps, libraries, protocols, and file formats deal with things. These characters are invisible. \n is used for the newline or linefeed, w
    2 min read
  • Different Ways to Initialize a Variable in C++
    Variables are arbitrary names given to the memory location in the system. These memory locations are addressed in the memory. In simple terms, the user-provided names for memory locations are called variables. Additionally, a data type is used to declare and initialize a variable. Suppose we want to
    4 min read
  • What are the differences between PHP constants and variables ?
    PHP Constants: PHP Constants are the identifiers that remain the same. Usually, it does not change during the execution of the script. They are case-sensitive. By default, constant identifiers are always uppercase. Usually, a Constant name starts with an underscore or a letter which is followed by a
    2 min read
  • What is the difference between var_dump() and print_r() in PHP ?
    In this article, we will discuss the difference between var_dump() and print_r() function in PHP. var_dump() Function: The var_dump() function is used to dump information about a variable that displays structured information such as the type and value of the given variable. Syntax: void var_dump ($e
    3 min read
  • What is difference between __sleep and __wakeup() in PHP ?
    In PHP, the __sleep and the __wakeup methods are called as magic methods. These methods are invoked or executed when we want to deal with serialization and deserialization of objects during runtime to store or save the object information in string format and again the information can be restored.  _
    4 min read
  • What is the difference between == and === in PHP ?
    In this article, we will discuss the differences between '==' and '===' operators in PHP. Both are comparison operators used to compare two or more values. == Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. Syntax: oper
    2 min read
  • What is Variable Scope in JavaScript ?
    Variable scope is the context of the program in which it can be accessed. In programming, a variable is a named storage location that holds data or a value. Think of it as a container that you can use to store and manipulate information in your code. Variables allow you to work with data in a flexib
    4 min read
  • What is the use of array_count_values() function in PHP ?
    In this article, we will discuss array_count_values() function & its usage in PHP, along with understanding its basic implementation through the illustrations. The array_count_values() function is used to count all the values inside an array. In other words, we can say that array_count_values()
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences