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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:26:49+00:00 2026-05-31T15:26:49+00:00

Hey guys I have a weird problem trying to use a global array in

  • 0

Hey guys I have a weird problem trying to use a global array in php.

I don’t even really want to use a global but here’s what i’m trying to do.

I have 2 functions

createMatrix($name) which creates a 5×5 matrix of texts boxes and remembers values
loadMatrix($name) which loads a 5×5 matrix from post data.

For some reason, after I run loadMatrix(), the values I set are lost in createMatrix()

I’ve tried a ton of debugging methods and declaring my matrix variables as globals but no luck.
I know it’s a scope issue because when I tried running everything outside of the functions it worked just fine.

<form action="Program4.php" method="post">
<?php
$matrixa = array(array());
$matrixb = array(array());
if($_POST['add'])
    echo "Add Clicked";
if($_POST['subtract'])
    echo "Subtract Clicked";
function loadArrays($name1,$name2) {
        //The Solution: global $matrixa, $matrixb;
    for($x = 0; $x < 5; $x++)
        for($y = 0; $y < 5; $y++) {
            $matrixa[$x][$y] = $_POST[$name1 . $x . $y];
            $matrixb[$x][$y] = $_POST[$name2 . $x . $y];
        }
}
function createMatrix($name) {
        //The Solution: global $matrixa, $matrixb;
    echo "<h2>" . $name . "</h2>\n";
    for($x = 0; $x < 5; $x++)
        echo $matrixa[0][$x];
    echo "<div style=\"border:ridge; border-width:7px; padding:12px; float:left; background-color:gray\">\n";
    for($x = 0; $x < 5; $x++) {
        for($y = 0; $y < 5; $y++) {
            echo "<input type=\"text\" name=\"" . $name . $x . $y . "\" maxlength=\"6\" style=\"width:50px\" value=\"";
            if($name == "MatrixA")
                echo "" . $matrixa[$x][$y];
            else if($name == "MatrixB")
                echo "" . $matrixb[$x][$y];
            echo "\">\n";
        }
        echo "<br>\n";
    }
    echo "</div>\n";
    echo "<br><br><br><br><br><br><br><br><br>\n";
}
loadArrays("MatrixA","MatrixB");
createMatrix("MatrixA");
createMatrix("MatrixB");
?>
<input type="submit" value="Add Matrices" name="add" />
<input type="submit" value="Subtract Matrices" name="subtract" />
</form>

This project is due at midnight so I’d appreciate some help 🙂

Basically the problem is trying to save the values.
I am able to load the variables from post data into the arrays using loadArrays() just fine, but the values don’t show up AT ALL when I use createMatrix();

  • 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-31T15:26:50+00:00Added an answer on May 31, 2026 at 3:26 pm

    It looks like you need to use http://www.php.net/manual/en/reserved.variables.globals.php

    You can use the $GLOBALS variable to get variables from the global scope

    $GLOBALS['matrixa']
    

    or the define variables with global before using it, then the $matrixa, $matrixb will refere to the global versions, more info here

    function createMatrix() {
        global $matrixa, $matrixb;
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, I have a weird problem. I have an update system that refreshes
Hey guys I have an assigned array from mysql results and I simply want
Hey guys I have setup Nivo slider to my specification, but what I want
Hey guys, I have a bit of a weird problem. I have a jquery
Hey guys I have a weird error I wrote this code against setbubblepopup $(document).ready(function
Hey guys I got a weird issue.. I have a datalist that's only loaded
Hey Guys I have four integer array lists which were created after parsing a
Hey guys I have a query that selects data and organizes but not in
Hey guys I have done lots of work with ASIHTTPRequest so far and use
Hey guys i have been trying to download it from the below link http://www.enterprisedb.com/products-services-training/pgdownload

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.