Question :
I have a function
pesquisaPagamentos($pesquisa)
I have a $pesquisa
variable that is getting the following value: '2015-10-05','2015-10-01'
, with single quotation marks.
It turns out that the way it, when it arrives at function
<?php
if(isset($_GET["acao"]) && $_GET["acao"] == "listaArr") {
$pesquisa = (isset($_POST["dataIni"])) ? "'".$PhpUtil->formataData($_POST["dataFim"])."','".$PhpUtil->formataData($_POST["dataIni"])."'" : "'',''";
print "<pre>";
print_r($pesquisa);
print "</pre>";
$arrecadacaoDia = $rel->pesquisaPagamentos($pesquisa);
I try to print the value of the first parameter and it comes with the integer value of the $ search variable, that is: '2015-10-05','2015-10-01'
. The second parameter of the function receives an empty value.
Answer :
To receive two or more arguments in function, you need to change your signature first.
function pesquisaPagamentos($pesquisa){
for
function pesquisaPagamentos($datainicio, $datafim){
The call should be made this way
pesquisaPagamentos('2015-09-01', '2015-10-05');
pesquisaPagamentos($data1, $data2);
It’s no use passing a string separated by a comma, the function will understand that this is just an argument.
Invalid call
pesquisaPagamentos('2015-09-01,2015-10-05');
It is also possible to have two parameters and pass only one value to the function, as long as the second parameter has a default value.
function pesquisaPagamentos($datainicio, $datafim='2015-12-31'){
Call:
pesquisaPagamentos('2015-09-01');
Since php5.6 there is a ...
operator that defines multiple parameters for a function, it basically does func_get_args
Recommended reading:
What is the difference between parameter and argument?
What is the name of the operator … used in PHP 5.6?
What can change with the implementation of variadic function?
Make it separate:
$pesquisa_data_inicio = "2015-01-02";
$pesquisa_data_final = "2015-02-02";
pesquisaPagamentos($pesquisa_data_inicio, $pesquisa_data_final)