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

  • Home
  • SEARCH
  • 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 614753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:08:44+00:00 2026-05-13T18:08:44+00:00

Following is code snippet : function something() { $include_file = ‘test.php’; if ( file_exists($include_file)

  • 0

Following is code snippet :

function something() {
$include_file = 'test.php';
if ( file_exists($include_file) ) {
    require_once ($include_file);
//      global $flag;
//        echo 'in main global scope flag='.$flag;
    test();
   }
}

something();

exit;

 //in test.php

$flag = 4;
function test() {
   global $flag;

   echo '<br/>in test flag="'.$flag.'"';
   if ($flag) {
       echo 'flag works';
     //do something
   }
}

The above code snippet, echoes ‘global scope’ $flag value properly but doesnt recognises the $flag with value 4, assumes null value for $flag .
Please point out what is wrong in accessing that $flag global variable.

  • 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-13T18:08:45+00:00Added an answer on May 13, 2026 at 6:08 pm

    $flag = 4; is not in the global scope.

    If the include occurs inside a
    function within the calling file, then
    all of the code contained in the
    called file will behave as though it
    had been defined inside that function.

    — PHP Manual page for include, which also applies for include_once, require, and require_once

    I’m going to make a guess that the error you’re getting is on the if ($flag) line, because at that point, $flag is uninitialized, because the global $flag variable has never been assigned a value.

    Incidentally, echo 'global scope flag='.$flag; isn’t displaying the global flag either, as you need a global $flag; in that function to display the global copy, which also has the side effect of making $flag = 4; affect the global copy in the included file.

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

Sidebar

Related Questions

Given the following code snippet: $i= 11; function get_num() { global $i; return (--$i
Look at the following code snippet: <?php class A { function fn() { print
Here is the following code-snippet I'm using in my Google Spreadsheet onEdit() function: else
delete_define.php has the following code snippet: <?php session_start(); ?> <form action=delete_now.php target=upload_target onsubmit=return my_func_1();>
So..Let's say I have the following code snippet function Rectangle(w,h){ this.width = w; this.height
I have the following code snippet to test a plain-text username/password against the AD,
Consider the following code snippet: var count = 0; function a(req, res, next){ count++;
By taking into consideration the following code snipped: if('<?= Yii::app()->controller->action->id?>' == 'create'){ $(#Event_name).focusout(function(){ $.ajax({
The following code snippet on SQL server 2005 fails on the ampersand '&': select
The following code snippet is from The Official GNOME 2 Developer's Guide : GMemChunk

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.