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 8397061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:41:43+00:00 2026-06-09T20:41:43+00:00

It may sound silly but I’m quite php outdated/unexperienced and coming from Java programming

  • 0

It may sound silly but I’m quite php outdated/unexperienced and coming from Java programming back to php, so I mix up the concepts.

If in a php webpage I declare a variable

$debug=TRUE;

and try to access it from below within a function

a(){
    if ($debug){
    echo "I'm here";
    }
}

the variable doesn’t exists or isn’t initiated? The whole file is just simply:

<?php
$debug=TRUE;
a(){
    if ($debug){
        echo "I'm here";
    }
}
?>

Do I need to make a session variable or something else? I’m quite clueless & the same concept is confusing me for the use of other variables within. Also for the further use of variables, I am trying to be forced to pass all the variables I need forward to the function where I use them and a class concept as in Java perhaps would be cleaner but is a kind of too much for this simplicity. Or do I need the functions (it’s a form processor) to be declared as a class?

I know this is silly, but I looked through Google and forums and the problem seems to be so obvious and simple that it’s hard to find a webpage or entry targeting this (or perhaps I’m asking the wrong question).

  • 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-06-09T20:41:45+00:00Added an answer on June 9, 2026 at 8:41 pm

    http://php.net/manual/en/language.variables.scope.php

    <?php
    $debug = TRUE;
    
    function a() {
        global $debug;
        if($debug === TRUE) {
             echo "I'm here....\n"
        }
    }
    

    Better, instead of using globals you can pass it in as a parameter:

    <?php
    $debug = TRUE;
    
    function a($debug = TRUE) {
      if($debug === TRUE) ....
    }    
    

    You can also use the $_GLOBALS array:

    <?php
    $debug = TRUE;
    
    function a() {
       if($_GLOBALS['debug'] === TRUE) ...
    }
    

    You can use constants, which are always in scope:

    <?php
    define('DEBUG', TRUE);
    
    function a() {
      if(DEBUG === TRUE) ...
    }
    

    You can also use a singleton:

    function a() {
        if(SingletonClass::get_instance()->debug === TRUE) {
            ...
        }
    }
    

    You’ll have to create a singleton class which extends StdClass() to get implicit ->get and ->set methods.

    http://www.talkphp.com/advanced-php-programming/1304-how-use-singleton-design-pattern.html

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

Sidebar

Related Questions

It may sound odd, but I've been programming games in PHP. The main problem
This question may sound silly.. but my intention is to understand advantages of Java
It may sound very silly to people who know this but I am quite
I'm new in the Python world, coming from PHP. So, this question may sound
I'm quite new to svn and stuff so this may sound silly but thats
The question may at first sound silly, but maybe it isn't at all. Java
I know that may sound silly, but I'm trying to submit a form on
This may sound like a silly question but in D (using std.regex ) how
This may sound silly but I didn't want to hijack a question. I recently
I know it may sound silly, but i worked for a client that seems

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.