How to use a Function inside another Function in php?

Asked

Viewed 545 times

-1

I need to wear one function inside of another function ...

function formata_data( $datacad ){
    $datacad = explode(' ', $data);
    $datacad = $data[0];
    $datacad = explode("-", $data);
    $datacad = $data[2]."-".$data[1]."-".$data[0];
    return $datacad;
}

Now I have a function to list users who need to return the data including the data de cadastro and format it with the created function.

function usuarios(){
    // listando usuarios
    $datacadastro = ("Y-m-d H:i:s");
    $datacadastro = ; //usar a função formata_data para formatar o retorno da data no banco
}
  • Please comment the negative so that I re-edit without changing the objective but correcting the question. Sincerely

1 answer

5


To use the function created by you, just use it in the same way as any pre-existing one in PHP:

function usuarios(){
    // listando usuarios
    $datacadastro = formata_data("Y-m-d H:i:s");
}

However, your function needs to return something, otherwise it won’t help you much. Missing put in the return what the function should return. Behold:

function formata_data( $datacad ){
    $datacad = explode(' ', $data);
    $datacad = $data[0];
    $datacad = explode("-", $data);
    $datacad = $data[2]."-".$data[1]."-".$data[0];
    return $datacad; // Aqui acrescentamos o que deve ser retornado.
}
  • Perfect application of Function format_data() within another Function as explained ... <?php echo formata_data($line['datacad']); ?> ... Thank you

Browser other questions tagged

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