Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$_POST as a function parameter [duplicate]

Tags:

php

I've been working on a project for a game (so you'll understand that I can't post the actual script), and I'm cleaning up the code and placing code into functions instead of just after the

if ($_POST['name'] { //code }

Is this the correct method of doing what I'm trying? (I tried, but it broke my system)

if ($_POST['action'] == "actionName") actionName($_POST); //calls function if the $_POST is present
function actionName($_POST) { //code }

Thanks in advance for correct answers!

like image 291
Sterling Archer Avatar asked Jan 21 '26 19:01

Sterling Archer


1 Answers

$_POST is globally accessible. So you don't have to pass to your function:

if ($_POST['action'] == 'actionName') actionName(); 

function actionName() {
    //code using $_POST
}

If you want your function to be able to do what it wants with any array (i.e. other than $_POST), then make the function take a parameter, but don't call it $_POST:

if ($_POST['action'] == 'actionName') actionName($_POST); 

function actionName(parameters) {
    //code using parameters
}
like image 112
JB Nizet Avatar answered Jan 23 '26 07:01

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!