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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:46:27+00:00 2026-06-12T20:46:27+00:00

Hi. I recently learned PHP and am trying to declare a global array so

  • 0

Hi. I recently learned PHP and am trying to declare a global array so I can access inside a function. But I seem to be missing something because I get the error ‘Undefined variable:’

Here is my code:

global $second_array;
$second_array = array();

function operatii($v) {
  $var1 = $second_array[count($second_array)-1];
  $var2 = $second_array[count($second_array)-2];
  $rez = null;

  echo $var1 . $var2 . "este?";
}

for ($i = 0; $i < count($a); $i++){
  if ($a[$i] === "+" || $a[$i] === "-" || $a[$i] === "*" || $a[$i] === "/" ) {
    operatii($a[$i]);
  } else {
    array_push($second_array, $a[$i]);
  }
}

I seem to be able to use the $second_array in the for loop, but can’t use it in the operatii function.
How can I solve this problem?

  • 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-12T20:46:28+00:00Added an answer on June 12, 2026 at 8:46 pm

    There are two ways to reference a global variable in PHP:

    1. Use the global keyword at the start of every function that uses the variable.
    2. Use the $GLOBALS array.

    Of these, I recommend sticking with the second, since it makes it absolutely clear at all times that the variable is a global.

    One of the problems with globals is keeping track of where they’re being used; using the $GLOBALS array mitigates this issue to some extent.

    However, there are still issues with using globals; it’s generally considered poor practice to use too many globals in your code. Having worked with a number of legacy systems that used globals extensively, I can vouch for the fact that they can cause a lot of headaches for future developers.

    Using globals also makes it harder to write formal test suites for your code (ie unit tests).

    My recommendation would therefore be to avoid using globals at all where possible. They are necessary in some cases, but the more you can avoid them, and instead pass variables into your functions and classes rather than making them global, the better things will be.

    So to summarise:

    If you must use globals, reference them with $GLOBALS['varname'], but it’s usually better not to use them at all.

    Hope that helps.

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

Sidebar

Related Questions

I have recently learned to create dynamic links via php, but my problem today
I've just recently learned how jquery can access dynamically generated content, by placing a
I've recently learned about the boot process for Linux. I can't help but wonder
When I learned PHP it was pretty much in procedural form, more recently I've
I've recently learned how to join 2 arrays using the + operator in PHP.
I recently learned that all stl containers have swap function: i.e. c1.swap(c2); will lead
I recently learned the ezplot function in MATLAB. Recently I typed in ezplot('x^y -
I recently learned that I can get hg log to print the history in
I recently learned of the IIF(A,B,C) function. I'm a long time VB/VB.NET Coder who
I've recently learned that on linux, git can be setup to autocomplete commands: If

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.