Return function value php

0

I need to make a condition if with the return of a function that is in a class:

File with class :

class teste
{
    public function my_function()
    {
        ...
        if(...)
        {
            return 0;
        }else{
            return 1;
        }
    }
}

File index.php :

//Arquivo index.php
$chamada = new teste('...');
$retorno = $chamada->my_function($campos);

if($retorno==0)
{
    echo 'error';
}
else
{
    echo 'ok';
}

The operation of the class, as well as the function, is perfect. My problem is just getting the contents of the variable $retorno and applying it to if .

In this case, that return of my_function already prints the return itself on the screen. With this, I can not handle it with if . The problem is to just identify the return of the function and make the IF in index.php

    
asked by anonymous 22.04.2017 / 18:25

2 answers

0

This is how it works:

<?php
    class teste
    {
        public function my_function()
        {
            if(1 != 1) //CONDIÇÃO
            {

                return 0;

            } else
            {

                return 1;

            }
        }
    }

    //Arquivo index.php
    $chamada = new teste();

    $retorno = $chamada->my_function(); // $retorno assume o valor retornado pela função que é 0 ou 1

    if( $retorno == 0 )
    {

        echo 'error' . "<br />";
        echo $retorno; // IMPRIME: 0

    }
    else
    {

        echo 'ok' . "<br />";
        echo $retorno; // IMPRIME: 1

    }
?>

The two main problems:

Line 8 - Two parentheses closing condition:

        if(...))

Line 20 - Passing parameter to function that does not expect it:

    $retorno = $chamada->my_function($campos);
    
22.04.2017 / 18:41
0

Removing the ellipsis, the logic of your code seems correct. See the example:

Test.php

I removed the ellipsis, put a parameter in the function and wrote shorthand:

class Teste {

    public function my_function($var)
    {
        return ($var) ? 1 : 0;
    }
}

index.php

I passed the variable $ var to the function and abbreviated the conditional:

$chamada = new teste();

$var = 1; // Com valor 1 retorna OK, com valor 0 retorna 'Erro';

echo $chamada->my_function($var) ? 'OK' : 'Erro';
    
22.04.2017 / 23:10