Problems returning value of a function

0

I have a function within another function which is inside a class in PHP , the funcion1 receives the parameter of a query $row['fila1']; that assigns a variable $ id.

When printing $ idgasto inside the function, it sends me the value of this query but when wanting to use the value outside the function, it sends it to me as 1 or 0, which does not help me.

function insert($idgasto){ 
   $idgasto1=($idgasto*1);//lo convierto a entero
   echo $idgasto1;//se imprime el valor con exito
   return $idgasto1;
}

when I call the function and I want to use the value of $idgasto just send me 0

insert();
echo $idgasto;//me imprime 0
    
asked by matteo 07.02.2017 в 20:14
source

1 answer

2

Try this:

function insert($idgasto){ 
$idgasto1=($idgasto*1);//lo convierto a entero
echo "imprimo dentro de la funcion " . $idgasto1;//se imprime el valor con exito
return $idgasto1;
}

When you call the function, do it this way.

$variable_de_funcion = 2;
$idgasto = insert($variable_de_funcion);
echo "imprimo fuera de la funcion " . $idgasto;

The problem is that the variable $ idgasto, which samples outside the function, is outside the scope of this, so it will not catch the value it received within the function.

    
answered by 07.02.2017 в 20:40