Can we pass an array as parameter in any function in PHP?

后端 未结 12 576
感动是毒
感动是毒 2021-02-02 06:56

I have a function to send mail to users and I want to pass one of its parameter as an array of ids.

Is this possible to do? If yes, how can it be done?

Suppose w

相关标签:
12条回答
  • 2021-02-02 07:34
    <?php
    
    function takes_array($input)
    
    {
    
        echo "$input[0] + $input[1] = ", $input[0]+$input[1];
    
    }
    
    ?>
    
    0 讨论(0)
  • 2021-02-02 07:37

    What should be clarified here.

    Just pass the array when you call this function.

    function sendemail($id,$userid){
    Some Process....
    }
    $id=array(1,2);
    sendmail($id,$userid);
    
    0 讨论(0)
  • 2021-02-02 07:37
    function sendemail(Array $id,$userid){  // forces $id must be an array
    Some Process....
    }
    
    
    $ids  = array(121,122,123);
    sendmail($ids, $userId);
    
    0 讨论(0)
  • 2021-02-02 07:39

    Its no different to any other variable, e.g.

    function sendemail($id,$userid){
      echo $arr["foo"]; 
    }
    
    $arr = array("foo" => "bar");
    sendemail($arr, $userid);
    
    0 讨论(0)
  • 2021-02-02 07:39

    In php 5, you can also hint the type of the passed variable:

    function sendemail(array $id, $userid){
      //function body
    }
    

    See type hinting.

    0 讨论(0)
  • 2021-02-02 07:39

    Since PHP is dynamically weakly typed, you can pass any variable to the function and the function will try to do its best with it.

    Therefore, you can indeed pass arrays as parameters.

    0 讨论(0)
提交回复
热议问题