2

I'm passing a lot of parameters in a function

I want to know if its wrong what i am doing and if it is possible to put all those variables into an array and just call the array:

Here my function parameters:

function addjo($logo, $job_category, $job_name, $status, $localization, $job_type, $contract_type, $description)

My code to recognize all the variables.

  if (isset($_POST['add_jo'])){

         $job_category  =$_POST['job_category'];
         $description  = $_POST['description'];
         $job_name  = $_POST['job_name'];
         $logo  = $_POST['logo'];    
         $status  = $_POST['status'];
         $localization  = $_POST['localization'];
         $job_type  = $_POST['job_type'];
         $contract_type  = $_POST['contract_type'];

         addjo($logo, $job_category, $job_name, $status, $localization, $job_type, $contract_type, $description);

         }else{


         $logo = NULL;
         $job_category = NULL;
         $job_name = NULL;
         $status = NULL;
         $localization = NULL;
         $description = NULL;
         $job_type = NULL;
         $contract_type = NULL;
         $check1 = NULL;
         $check2 = NULL;

         }

Is it possible to do something like this?

if(isset($_POST['mybutton'])){

array[](
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
);

function(callarrayhere);

else{

$var1 = null;
$var2 = null;

}

Thanks.

  • yeah, an array would be sensible here. – bwoebi May 17 '14 at 13:59
  • @user while using an array is sensible here, in doing so you lose the opportunity to enforce data types. It is somewhat verbose, but using [PHP8's named paratmeters](https://stackoverflow.com/a/64997399/2943403) can help you to keep the types more strict, declare default values when some are missing, and helpfully complain when unexpected parameters are passed into the function. – mickmackusa Aug 22 '23 at 22:17

3 Answers3

1

Since $_POST is already an array, just use it:

addjob($_POST);

And in addjob:

function addjob($input) {
    // verify that array items are present and correct.
}

Exporting an array's values to individual variables is not only a waste of time and memory, but it also makes your code harder to read (where do these variables come from? It's not obvious they came from the same source array)

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • I would suggest validating/sanitizing POST data before utilizing it in a function from within which it might not be clear that this is user-supplied input. – Mike Brant May 17 '14 at 14:07
  • @MikeBrant Fair enough, but personally I would prefer validating the data as soon as you know it's under your control for whatever purpose - the same input might be validated in different ways depending on where the data is going, but if I'm in `addjob` then I know exactly what needs doing. – Niet the Dark Absol May 17 '14 at 14:08
0

Yes it is possible to have an array as an argument to a function. Your syntax isn't quite right. This would do it:

function addjo($params){
    echo $params['logo'];
    echo $params['job_category'];
    echo $params['job_name'];
}

Usage example:

$arr = array(
    'logo' => $logo,
    'job_category' => $job_category,
    'job_name' => $job_name
);

addjo($arr);

You could also have default parameters for each of the array elements, or make them optional. See this answer for how to do that.

Community
  • 1
  • 1
MrCode
  • 63,975
  • 10
  • 90
  • 112
0

Of course it's possible:

 if (isset($_POST['add_jo'])){
         $a = array();   
         $a['job_category']  =$_POST['job_category'];
         $a['description']  = $_POST['description'];
         // and so on   
         $a['contract_type']  = $_POST['contract_type'];    
         addjo($a);

    } 
    else
    {
          $a['job_category'] = null;
         // and  so on    
    }

And in function addjo you can display all values that way:

function addjo($a) {
   foreach ($a as $k => $v) {
      echo $k.' '.$v."<br />"
   }

   // or
   echo $a['job_category'];
}
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291