Click here to Skip to main content
15,669,524 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building a system and the objective is by a form, put the list dynamically on the screen... I am beginner when is about php... Someone can help me solve that? PLS

That is the code i am using:
$lista_ocorrencia = [];
function imprimir_ocorrencias($lista_ocorrencia) {
        $n_pedido = $_GET['n-pedido'];
        $nome = $_GET['nome'];
        $cidade = $_GET['cidade'];
        $ocorrencia = $_GET['ocorrencia'];
        $valor = $_GET['valor'];
        $ocorrencia = "<ul class='modelo'>
                        <li class='w10'>$n_pedido</li>
                        <li class='w20'>$nome</li>
                        <li class='w15'>$cidade</li>
                        <li class='w40'>$ocorrencia</li>
                        <li class='w15'>$valor</li>
        array_push($lista_ocorrencia, $ocorrencia);
        foreach($lista_ocorrencia as $x){
            echo $x;

What I have tried:

I tryed a lot of things and the last was using array_push() of php but always when i call the function in the site the array reset, so now i am trying put array out the function but return to me the same result. Wherever i put this array it is reseted.
Updated 5-Sep-22 22:21pm
Member 15627495 5-Sep-22 23:06pm    
to initialise your array :

$lista = array(); for start an array

or without array :: for the use you have ::
 $ocorrencia = "<ul class='modelo'>                        <li class='w10'>$_GET['n-pedido']</li>                        <li class='w20'>$_GET['nome']</li>                        <li class='w15'>$_GET['cidade']</li>                        <li class='w40'>$_GET['ocorrencia']</li>                        <li class='w15'>$_GET['valor']</li>                    </ul>";

don't forget that : $_GET is an array , It's a var like the others.
while you don't operating instructions on values : $one = $_get['one']; is a copy of the value, but a second var for nothing.

1 solution

PHP variables are scoped to wherever they're declared, and PHP passes by value rather than by reference by default. What this means is that when you pass the array into the function, PHP creates a clone of the array and any changes you make to it happen on the clone, not on the original value. You can change this by using PHP Pass by Reference: The & Ampersand | PHP Reference Book Blog[^]

Simply change your function declaration to include an ampersand (&) before the variable:
function imprimir_ocorrencias(&$lista_ocorrencia) {
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900