Create multiple PHP variables in the same function

Asked

Viewed 188 times

0

I need a very simple function, but I couldn’t find anything related in my research.

I need a function where I can insert items, and then print these items on another page.

Ex:

    function funcaoExibe() {
 $var = 'none1';
 $var = 'email2';
 $var = 'telefone3';
    echo $var;
}

Created the function, I need to display all the values of the variables inserted within the function, something like that:

   <?= funcaoExibe(); ?>

No resultado aparece somente:

telefone3

I need the three shown, one on each line.

Any idea how I can do it?

  • places echo within the function

  • It is that the function will stay on one page, and the result on another. It will not all stay in the same PHP file.

  • As you said above, the processing will stay in one file and the result in another. You can return to the html function and display on the desired page.

  • I updated the question, the problem is that I don’t know how to create this function.

  • you are overwriting the variable $var. I could not understand properly what you want. The function should receive 3 arguments and print and not have the values defined within it.

  • You could work with get and set in this case, set puts the information and get the information

Show 1 more comment

2 answers

1

You can mount html in a php file and display it in another one like this.

//filename.php

    <?php
       function ProcessarInformacao($nome, $email, $telefone) {
          $html  = "<div>";
          $html .= "<p>Nome: ".$nome."</p>";
          $html .= "<p>Email: ".$email."</p>";
          $html .= "<p>Telefone: ".$telefone."</p>";
          $html  = "</div>";

          return $html;
       }
    ?>

//view result.php

echo ProcessarInformacao('Nome Teste', '[email protected]', '(17) 3030-4040');
  • It would not be that, the variables name, email, phone does not exist, I used only as an example. It is that I need to load JS files on a specific page, so I will put inside the function which JS should be loaded on the page, and where it displays the result, will insert the lines with the JS URL.

  • 1

    Reread your question and formulate it correctly, as it is difficult to understand what you need.

1

You can make the function return an array (array) In the a.php file you enter the information:

a. php

<?php
function funcaoExibe( $nome, $email, $telefone ) {
  $retorno = array(
    'nome'  => $nome,
    'email' => $email,
    'fone'  => $telefone
  );    
return $retorno;    
} 
?>

Then you choose what you want to show off

<?php
  $var = funcaoExibe('Carlos', '[email protected]', '(12)3454-6555');
  echo  $var['nome']."<br>" ;
  echo  $var['email']."<br>" ;
  echo  $var['fone']."<br>" ;
?>

Browser other questions tagged

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