2

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!

Sterling Archer
  • 22,070
  • 18
  • 81
  • 118

8 Answers8

9

$_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
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

$_POST is a super-global. All PHP code, no matter the scope, automatically has access to this data structure.

Therefore, the following is fine:

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

and then

function actionName() {
  print_r($_POST);
  // code
}
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
0

$_POST is already a superglobal variable so you do not need to pass it to a function. If the function needs the content of $_POST then make a copy and pass it to the function.

However, from your example, it seems to me that you need to pass $_POST['action'] to your function instead of $_POST.

Emre Yazici
  • 10,136
  • 6
  • 48
  • 55
0

Well in your example you need to have quotes around your string variables:

//calls function if the $_POST is present
if ($_POST['action'] == 'actionName') actionName($_POST); 
function actionName($_POST) { /*code*/ }
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
0

Although $_POST is super-global, yes of course you can do that and is actually a good idea. This way you decouple your code and maybe you can reuse actionName in another part of the application.

However, don't name the parameter of the function $_POST (this is more confusing than helpful).

To avoid a bunch of if statements for every possible action, have a look at call_user_func [docs].

E.g. you could do:

$allowed_actions = array_fill_keys(array('actionName', ...), true);

if(isset($allowed_actions[$_POST['action']])) {
    call_user_func($_POST['action'], $_POST);
}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

You should move the function before the code that executes it along with what everyone else here is saying about the $_POST array. Try this if you want

function actionName($input) { // code and return }
if (isset($_POST['action'])) {
    $action = $_POST['action'];
    if ($action == 'actionName') {
        $name = actionName($action);
    }
}
Drewdin
  • 1,732
  • 5
  • 23
  • 35
0
  • $_POST is a super global (as other have mentioned), so you don't need to pass it in.
  • You can pass it in so that you decouple the function code from the environment in which it's running. For example, should you wish to initiate such an action as a result of something else in the code, you can pass in an array with the same key/values that $_POST would have to do so
  • If you do pass in $_POST to a function, name the function argument something else, hopefully something that indicates what the data represents
  • Consider that the function may not need all the key/values that $_POST normally contains, and if you might be better off defining a function that takes only the arguments it needs in order to function
RHSeeger
  • 16,034
  • 7
  • 51
  • 41
0

If you are talking about dynamically calling a function, you could achieve it this way:

/*

Example Post

$_POST['functionName'] = 'doSomething';
$_POST['value'] = 'woot!';


*/

function doSomething($message) {
    echo $message;
}


// Call the function if it exists
if (function_exists($_POST['functionName']) {

    call_user_func($_POST['functionName'], $_POST['value']);    

}


// The above would output "Woot!"
Andy Fleming
  • 7,655
  • 6
  • 33
  • 53