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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:23:00+00:00 2026-05-29T13:23:00+00:00

I have a function that uses lots of global vars and arrays – e.g.

  • 0

I have a function that uses lots of global vars and arrays – e.g.

$a=1;
$b[0]='a';
$b[1]='b';
$c='Hello';

function foo() {
 echo "$a 
       $b[0] 
       $b[1] 
       $c";
}

I understand that as opposed to JS, you have to include the vars when you call the function:

function foo($a,$b,$c)

but since I’m using lots of different vars and arrays in the function, and since the main origin of most of the vars is the $_GET array (after using extract($_GET)), I’m not sure how to do this.

is there a way to make a PHP function behave like a JS function for that manner?

  • 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-29T13:23:01+00:00Added an answer on May 29, 2026 at 1:23 pm

    You can use the global keyword:

    $a = "Hello World";
    $b = "Hello World";
    
    function outputStrings(){
      global $a, $b;
      echo $a." - ".$b;
    }
    
    $b = "Goodbye World";
    
    outputStrings(); // ouputs "Hello World - Goodbye World"
    

    However, its best not to use this structure. Its generally confusing and will make your code difficult to maintain. WordPress uses this approach a lot in their code base and it makes for very tricky debugging. Other plugins and code can interject and modify global variables, changing the output of your script.

    What would be better would be to either:

    Use an OOP structure for your web app.

    This way you can use objects instead of just random global variables. This gets around the issue of you accidentally overwriting a global variable in the course of a script. It also helps to organise the variables correctly, so all variables concerning users can be in the User class. It makes more sense to structure it like that.

    class User {
      private $firstName;
      private $secondName;
      private $gender;
    
      public function __construct($fname, $sname, $gend){
        $this->firstName = $fname;
        $this->secondName = $sname;
        $this->gender = $gend;
      }
    
      public function outputDetails(){
        echo $this->firstName." ".$this->secondName." is ".$this->gender;
      } 
    }
    
    $user = new User("Thomas", "Clayson", "Male");
    $user->outputDetails();
    

    Pass variables into functions

    Just like you’ve shown in your example. This is the generally accepted standard way of doing this. You should always pass in variables like this, it helps you define scopes and a proper structure. Also it means you know what the value of variables is, as you own them and pass them to functions, rather than just plucking variables from global scope.

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

Sidebar

Related Questions

I have a function that uses Pattern#compile and a Matcher to search a list
I have a function that uses out parameters to return multiple values to the
I have a function that uses CDO to send emails with request to have
I currently have a Javascript function that uses a string to reference an object
I have written a javascript function that uses setInterval to manipulate a string every
I have a static logging function that uses StringBuilder to concatenate a bunch of
I have code that uses Win API function RegSaveKeyEx to save registry entries to
I have some code that uses the Oracle function add_months to increment a Date
We have system here that uses Java JNI to call a function in a
Long story short, I have a long code that uses jQuery. Lots of files,

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.