2016-12-14 86 views
0

我想知道如果我可以在下面的例子中通过一个变量作为自变量,如:变量作为自变量

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)); 
} 

此函数具有变量$检查器作为参数。

我调用函数内部的变量检查发现它的下一行:

'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 {} 

这项工作?像这样传递一个变量作为参数? 我听到你在想什么,为什么你不要继续前进,测试一下,看看自己。问题是我无法测试一些复杂的目的。所有我需要知道的是如果这样的事情是可能的

+1

确定它会工作。 – Yoleth

+0

您为什么认为这可能无效?我在这里没有看到任何特别的东西。 – simon

+1

它必须工作,但我不明白你在哪里传递在该函数中使用的其他变量? '$ organization_id' '$ strToRemove' 而且,你在哪里初始化调用这个函数结束的对象: '$ SimplicateApi' –

回答

1

是的,这是可能的。

对于什么是值得的,为了让答案不仅仅是一个“是”,而是通过以这种方式调用函数来传递值。

从功能上来说,这是处理您的用例的好方法。

具体来说,它比使用全局变量更好。

function add_sales(){ 
    global $checker; 
    $sales_payload = array(

全局变量使得你的代码更难维护,因为他们引进机制,使您可以影响一个变量的值(也可能是覆盖正在使用的其他地方变量)。

这比通过引用传递它还好。

function add_sales(&$checker){ 
    $sales_payload = array(