1

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 5x5 matrix of texts boxes and remembers values loadMatrix($name) which loads a 5x5 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();

5
  • I don't see you define loadMatrix() anywhere on your code. Commented Mar 16, 2012 at 19:49
  • 1
    Before using a global variable inside a function. Do this: global $matrixa; Commented Mar 16, 2012 at 19:49
  • You also forgot a ; at the end of loadMatrix("MatrixA","MatrixB") Commented Mar 16, 2012 at 19:51
  • Haha you guys answered super fast. So awesome :) Commented Mar 16, 2012 at 19:53
  • I solved the problem right before I looked back. Bjorne Malmanger is right. I declared global $matrixa inside of each function and it worked like a charm. Thanks for the help guys! Commented Mar 16, 2012 at 19:54

3 Answers 3

1

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;
} 
Sign up to request clarification or add additional context in comments.

1 Comment

To all you guys, thanks. loadMatrix() was supposed to be loadArrays() my bad I was going to try to change it to return some values or whatever but I was able to solve the problem like you have said. Basically I was thinking of the word "global" backwards (coming from C# or C++) Declaring global inside of each function instead of declaring global at the very top was what I was doing wrong New functions: function createMatrix($name) { global $matrixa, $matrixb; echo "<h2>" . $name . "</h2>\n"; function loadArrays($name1,$name2) { global $matrixa, $matrixb; Works like a charm!
1

In order to use variables generated inside a function, you have to define it as a global variable.

global $variable; $variable=1;

inside your second function you must also tell that $variable is global. Function's variables are otherwise treated local only, and are used specifically for the function itself.

Comments

0

You defined the function as loadArrays() instead of loadMatrix().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.