unexpected 'if' (T_IF)

1

I have the following code

$data .= '<tr>
            <td>'.$number.'</td> 
            <td>'.$row['nombre'].'</td>
            <td>'.$row['apellido'].'</td>
            <td>'.$estado.'</td>
            <td>
                <button onclick="GetUserDetails('.$row['id'].')" class="btn btn-warning">Update</button>
            </td>
            <td>'.if( $estado == 5){.'  



                <button onclick="DeleteUser('.$row['id'].')" class="btn btn-success">Iniciar</button>       

'.}.'
                <button onclick="DeleteUser('.$row['id'].')" class="btn btn-warning">Iniciar</button>   


            </td>
        </tr>';

but the if tag does not grab me, this is inside the corresponding <?php tags.

    
asked by Juan Manosalva 17.04.2017 в 15:56
source

2 answers

1

You can change it to a ternary condition:

$data .= '<tr>
            <td>'.$number.'</td> 
            <td>'.$row['nombre'].'</td>
            <td>'.$row['apellido'].'</td>
            <td>'.$estado.'</td>
            <td>
                <button onclick="GetUserDetails('.$row['id'].')" class="btn btn-warning">Update</button>
            </td>
            <td>'.(($estado == 5) ? '

                <button onclick="DeleteUser('.$row['id'].')" class="btn btn-success">Iniciar</button>       

' : '').'
                <button onclick="DeleteUser('.$row['id'].')" class="btn btn-warning">Iniciar</button>   


            </td>
        </tr>';

Documentation ternary operations

    
answered by 17.04.2017 / 16:15
source
6

The error is because you have a if (which is a constructor) attached (with the ".") to the variable $data (which is a string / character string). You can correct it by closing the string and then executing the "if":

$data = '<tr>...
    <td>';
if ($estado == 5) {
    $data.= '<button ...>';
}
$data.= '</td></tr>';
    
answered by 17.04.2017 в 16:10