Can't Echo or Append to an Array within Functions

I’ve created a master array that I want to be able to append to in a couple of functions, depending on conditions, using array_push. I also want to be able to echo the values of the array from inside of a function.

When I do these without the function, everything works fine. When I put it in a function, I get nothing. Not sure what I’m doing wrong. Do I need to do something special to make an array able to be affected inside of functions?

Thanks!

sounds like a scope issue. Are you doing procedural or object oriented coding?

I’m fairly new to php, with a background in classic ASP… so it’s possible I blew my scope. This is more of a procedural. Nutshell looks like this:

  1. The array is established
  2. The “character” makes a decision, based on randomizing his 3 key deciding stats
  3. Call the function to determine how he did with his decision (like “defend” vs the oppoonent’s randomized stat)
  4. Add a new line to the array with the result, and go back to the decision maker again

My plan is to store the array at the end, in a db, for playback at a later time, which is why I need to build it out this way.

Thanks!

this is too abstract, and way too formal without any background on your project. Just show an example code that we can copy and paste, including some sample data and your desired output.

Sure thing. I’ve been trying to work with it, but here’s where I am:

$gamepbp=array(“Austin Minotaurs vs Nashville Overtone”, “Ready for the tip”);

$ActiveHomePlayerShootO = 86;
$ActiveVisPlayerShootD = 54;

function takeShot() {
//Randomize Stats
$ActiveShotAttempt = rand(1,100) * $ActiveHomePlayerShootO;
$ActiveShotDefend = rand(1,100) * $ActiveVisPlayerShootD;

    //Compare Stats

    if ($ActiveShotAttempt >= $ActiveShotDefend) {
        $pbpupdate = "Moon hits a shot!";
        $HomePoints = $HomePoints + 2;
    } else {
        $pbpupdate = "Moon has his shot blocked!";
    }
    
    //Append to Array
    array_push($gamepbp,$pbpupdate);
    $pbpupdate = '';
}

function pbpOutput() {
echo $gamepbp[0] . “
”;
echo $gamepbp[1] . “
”;
echo $gamepbp[2] . “
”;

    print_r($gamepbp);
}

In this simplified version, I should see the following:

Austin Minotaurs vs Nashville Overtone
Ready for the Tip
(either) Moon hits a shot! (or) Moon has his shot blocked!

At least there’s that scope issue - within functions you do not have access to the variables on the outside. Just give them into the functions as parameters. You would get a hint on that in most IDEs like PHPStorm, and PHP would raise an error when calling the function when error reporting is enabled, what is recommended for any delevelopment system

error_reporting(E_ALL);
ini_set('display_errors',1);

I’m pretty new to php. I’m working on Bitnami for my local computer server. What you just put up there, does that go in the php.ini?

Do you have an example of how I might pass these as parameters? Does this mean I do something like function doingStuff(array) and then call it with doingStuff(arrayname); in the call? I’m surprised I can’t affect variables inside functions, but my background came from Classic ASP (years ago) and JavaScript. I appreciate any guidance. Thanks!!

The error related settings should go in the php.ini, so that they can be changed at a single point, they will cause php syntax errors to be reported, and you don’t need to remember to put them into code for debugging and remove or change them when moving code to a live server. The php.ini should have existing lines setting these, but you may need to change the actual values to those shown. You should also set php’s output_buffering to OFF.

Php’s functions use the ‘black box’ model (which you can break if you are into bad coding that’s hard to debug when a variable gets mixed up and changed by some code elsewhere), where the only affect a function call has on the calling code can be seen/tested at the point of the function call. This allows the code inside of functions to do whatever they need to do without needing to keep track of anything going on outside of the function code. This makes writing larger applications, and when multiple programmers are working on a project, easier, since you don’t need to keep track of and insure all the variables being used or modified inside of functions are unique.

The black box model works by accepting all inputs as call-time parameters (the way you have theorized above), performing whatever processing the function has been designed to do, then returning the result to the calling code.

Some notes about functions. A function should be responsible for doing one thing, that helps you to build an application, by simplifying the main logic and eliminating repetitive logic, i.e. they are helpful building blocks. Functions should be general purpose, reusable. They should not contain application specific values that change when moved to a different server or a different domain. Functions should not echo output. You should not find yourself regularly editing the code inside of a function or copying/changing the code with a different function name. If the code inside of a function is just part of your main program logic, surrounded by a function definition, all you have done is add a layer of unnecessary syntax.

Sponsor our Newsletter | Privacy Policy | Terms of Service