IF syntax within an ECHO

2

I'm having a syntax problem when trying to put a if inside a echo in PHP.

Check out the code:

foreach($resultadoBusca as $escrever){
    echo "<tr><td>" . $escrever['cod_votoran'] . "</td>
    <td>" . utf8_encode($escrever['empresa_user']) . "</td>
    <td>" . utf8_encode($escrever['cidade_user']) . "</td>
    <td>" . $escrever['estado_user'] . "</td>
    <td>" . $escrever['fone_user'] . "</td>
    <td>" . $escrever['cpfcnpj_user'] . "</td>
    <td>" . $escrever['email_user'] . "</td>
    <td>" . $escrever['status_user'] . "</td>
    <td>".if($escrever['status_user']=='ativo'){."<a href=\"ativarUsuario.php?cod=".$escrever['cod_user']."\"><i class=\"icon-check\" title=\"Ativar Usuário!\"></i></a>"}."<a href=\"editarUsuario.php?cod=".$escrever['cod_user']."\"><i class=\"icon-edit\" title=\"Editar Usuário!\"></i></a><a href=\"excluirUsuario.php?cod=".$escrever['cod_user']."\"><i class=\"icon-remove\" title=\"Remover Usuário!\"></i></a></td></tr>";
}      

I know that errors are occurring well in the part where if starts. Well if I take it out it works normally. This is an error.

    
asked by anonymous 24.10.2014 / 10:37

2 answers

5

You can not if within echo . The function echo is for concatenating strings , and you can not put code inside a string otherwise it will not work.

You may want to end echo whenever you want for more code:

foreach($resultadoBusca as $escrever){
    echo "<tr><td>" . $escrever['cod_votoran'] . "</td>
    <td>" . utf8_encode($escrever['empresa_user']) . "</td>
    <td>" . utf8_encode($escrever['cidade_user']) . "</td>
    <td>" . $escrever['estado_user'] . "</td>
    <td>" . $escrever['fone_user'] . "</td>
    <td>" . $escrever['cpfcnpj_user'] . "</td>
    <td>" . $escrever['email_user'] . "</td>
    <td>" . $escrever['status_user'] . "</td>
    <td>";
    if($escrever['status_user']=='ativo')
    {
        echo "<a href=\"ativarUsuario.php?cod=".$escrever['cod_user']."\"><i class=\"icon-check\" title=\"Ativar Usuário!\"></i></a>"
    }
    echo "<a href=\"editarUsuario.php?cod=".$escrever['cod_user']."\"><i class=\"icon-edit\" title=\"Editar Usuário!\"></i></a>";
    echo "<a href=\"excluirUsuario.php?cod=".$escrever['cod_user']."\"><i class=\"icon-remove\" title=\"Remover Usuário!\"></i></a></td>
                        </tr>";
}     
    
24.10.2014 / 10:42
10
To use if within an echo, you need to make use of a ternary operator all in parentheses: ( (condição) ? true : false )

$id = 1;
echo 'Olá ' . ( ($id === 1) ? 'id-1' : 'id-2' );

output : Ola id-1

More about ternary operator

    
24.10.2014 / 11:05