我想知道我是否可以将变量作为参数传递,如下例所示:
function add_sales($checker){
$sales_payload = array(
'organization_id' => $organization_id,
'contact_id' => $contact_id,
'status' => 'Open',
'subject' => " ".str_replace($strToRemove, "", $_POST['billing_myfield12'])." - ".implode(" ",$checker),
'start_date' => date("Y-m-d"), // set start date on today
'expected_closing_date' => date("Y-m-d",strtotime(date("Y-m-d")."+ 14 days")), // set expected closing date 2 weeks from now
'chance_to_score' => '10%',
'expected_revenue' => 0, //set the expected revenue
'note' => $_POST['order_comments'],
'progress' => array(
'id'=>'salesprogress:200a53bf6d2bbbfe' //fill a valid salesprogress id to set proper sales progress
),
"custom_fields" => [["actief_in_duitsland"=>$value]],
);
// add the sales
$sales = $SimplicateApi->makeApiCall('POST','/sales/sales',json_encode($sales_payload));
}
此函数将变量$ checker作为参数。
我在函数内部调用此变量检查器,注意它在下一行:
'subject' => " ".str_replace($strToRemove, "", $_POST['billing_myfield12'])." - ".implode(" ",$checker),
当我打电话给我的功能时,我喜欢;
$vertalingen_check = array_intersect($product_names , $vertalingen);
$vertalingen_count = count($vertalingen_check);
if($vertalingen_count >= 1){
add_sales($vertalingen_check);
}else {}
这会有用吗?传递变量作为这样的参数? 我听到你在想,为什么不继续测试它自己看看。问题是我无法针对某些复杂的目的对此进行测试。我需要知道的是,这样的事情是否可能
答案 0 :(得分:1)
是的,这是可能的。
对于它的价值而言,为了使答案不仅仅是“是”,你通过以这种方式调用函数来传递它。
从功能上讲,这是处理用例的好方法。
具体来说,它比使用全局变量更好。
function add_sales(){
global $checker;
$sales_payload = array(
全局变量使您的代码难以维护,因为它们引入了可以影响变量值的机制(并且可能覆盖其他地方正在使用的变量)。
它也比通过引用传递它更好。
function add_sales(&$checker){
$sales_payload = array(