Take a variable from within a function

Asked

Viewed 24 times

-1

How can I get a variable from inside a function.

my code:

 function buscaGeralPessoas($buscaPessoas){
    $result_msg_contatos2 = "Select idGeralPessoas, cpf, nome, telefone1, telefone2, celular, email  from geralPessoas where idGeralPessoas in  ('$buscaPessoas')";

    $inforGeral = "";

    $resultado_msg_contatos2 = mysqli_query($conn, $result_msg_contatos2);
    while ($row_msg_contatos2 = mysqli_fetch_assoc($resultado_msg_contatos2)) {

        $idGeralPessoas = $row_msg_contatos2['idGeralPessoas'];
        $nome = $row_msg_contatos2['nome'];
        $telefone = isset($row_msg_contatos2['telefone1']) ? $row_msg_contatos2['telefone1'] : $row_msg_contatos2['celular'];
        $celular = isset($telefone) ? $telefone : $row_msg_contatos2['telefone2'];
        $email = $row_msg_contatos2['email'];
        $cpf = $row_msg_contatos2['cpf'];

        $inforGeral .= "ID: $idGeralPessoas, Nome: $nome, CPF: $cpf, Celular: $celular, Email: $email \n";

In this case, I need to return the variable $infoGeral, outside of my function.

  • "return the $inforGeral variable, out of my function" not to access a local variable of the method from another place, passes it by parameter or returns it in the method, or will need to be a public variable

1 answer

0

At the end of your function, after the end of while, write the line:

return $inforGeral;

That way, in the place where you call the function, you can receive your variable that way:

$inforGeral = buscaGeralPessoas($buscaPessoas);

Browser other questions tagged

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