Error passing variable as double parameter

Asked

Viewed 76 times

3

I have a function

pesquisaPagamentos($pesquisa)

I have a variable $pesquisa which is receiving the following amount: '2015-10-05','2015-10-01', with single quotes.

It happens that the way this, when it arrives at the 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 comes the integer value of the $query variable, ie: '2015-10-05','2015-10-01'. The second parameter of the function receives an empty value.

  • Do separate: $pesquisa_data_inicio = "2015-01-02"; $pesquisa_data_final = "2015-02-02"; searchPayments($pesquisa_data_inicio, $pesquisa_data_final)

  • But in the call you passed the second argument? in the example of the question no.

  • See: if '2015-10-05','2015-10-01'. note that there is a comma in the middle. This is not enough for the variable to populate the 2 parameters when printed?

  • 1

    This is called the function? $rel->pesquisaPagamentos($pesquisa);? You only have one argument, you need two

  • the function asks for 2 arguments, however, the value output of the $query variable emulates a string of type 'valor1', 'valor2' but I ended up doing according to your orientation. It became more legible! Thank you!

2 answers

4

To receive two or more arguments in function it is necessary to change your signature first.

function pesquisaPagamentos($pesquisa){

for

function pesquisaPagamentos($datainicio, $datafim){

The call must be made that way

pesquisaPagamentos('2015-09-01', '2015-10-05');
pesquisaPagamentos($data1, $data2);

No use passing a string seperada by 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 function, since the second parameter has a default value.

function pesquisaPagamentos($datainicio, $datafim='2015-12-31'){

Calling for:

pesquisaPagamentos('2015-09-01');

From php5.6 there is an operator ... that sets multiple parameters for a function it basically does what func_get_args

Example - ideone

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 variadic Function implementation?

  • Thanks! That’s what I’d like to avoid. But it turned out I had to do it anyway!

3

Make separate:

$pesquisa_data_inicio = "2015-01-02"; 
$pesquisa_data_final = "2015-02-02"; 
pesquisaPagamentos($pesquisa_data_inicio, $pesquisa_data_final) 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.