Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6960435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:24:06+00:00 2026-05-27T15:24:06+00:00

Variable scope (as defined here ) The scope of a variable is the context

  • 0

Variable scope (as defined here)

The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well.

//a.php
<?php
class a {
 function &func () {
   $avar = array("one", "two", "three");
   return $avar;
}
?>

__

//b.php
<?php
class b {
 include("a.php");
 $ainstance = new a;
 var_dump($ainstance->func()); 
}
?>

The above code will Dump information about the variable as expected (I mean WRT the structure as formed in the function func).

My doubt is that,

  • where are the variable stored when it is in the scope of a function?
  • If it is on the call stack, then wont the variable be cleaned/destroyed when the function terminates?
  • Since the variable is not getting destroyed (as per the code above), why is it not getting destroyed or does PHP have a mechanism to save the variable (say in a heap)and return the reference to it.
  • Does PHP have call stack at all?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:24:07+00:00Added an answer on May 27, 2026 at 3:24 pm

    All variables in PHP are zval*, that is, C pointers.

    If you return by value, PHP will, in most cases, automatically copy the zval* and return that. If you return by reference, PHP will return the original zval*. In none of these cases does the returned zval* refcount reaches 0.

    On the C side, well, when you return a variable, it returns a pointer to a zval, which is a C struct containing information about the variable (namely type, value, the refcount and a is_ref flag).

    Since it’s a pointer, it’s not actually returning a local C variable, but a pre-allocated zval pointer, which points to the location of the actual zval. Unless that zval* refcount reaches 0 (i.e.: not storing the return value), the variable will still live until the end of the program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the difference between cfproperty tag defined variable and the variables scope variable
I would like to define variables within scope of paginate() . However, any variable
Global scope allows you to use a variable in a function that was defined
If a variable is declared as static in a function's scope it is only
What is the scope of variables in javascript? Do they have the same scope
I have read in various places that having variables with global scope, i.e. a
I have some task to solve and the most important part at the moment
So the problem I'm having is I have a function which uses a variable
I've run into what appears to be a variable scope issue I haven't encountered
I am looking for an alternative to using an object/variable in global scope --

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.