PHP Parse error: syntax error, Unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING

Asked

Viewed 4,152 times

0

I need to do a validation, if it’s true it goes to a link and if it’s fake it goes somewhere else. my code is following.

<a href='<?php if($dado['st_nome'] =="FINALIZADO")
{echo "dashboard.php?link=17&id=<?php echo $dado["op_id"];?>";}
else{ echo '#';}?>'>

But it’s returning to me

PHP Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in E:\home\gigaclima\Web\giga_proj\paginas\listas\listar_op.php on line 50

Something in the code is generating a conflict I can’t identify.

3 answers

4


Since you are already in PHP you don’t need to open again, you understand?

The right thing would be:

<a href='<?php if($dado['st_nome'] =="FINALIZADO")
{echo "dashboard.php?link=17&id={$dado['op_id']}";}
else{ echo '#';} ?>'>

But I would advise you to do so:

<?php
if( $dado['st_nome'] == "FINALIZADO" )
    $link = "dashboard.php?link=17&id=".$dado["op_id"];
else 
    $link= "#";
?>
<a href='<?php echo $link; ?>'>

To have minimal PHP in the middle of HTML. It gets less confusing.

2

I think you can still use a ternary as an option:

<?php
     $dado['st_nome'] == "FINALIZADO" ?  $link = "dashboard.php?link=17&id=".$dado["op_id"]: $link= "#"; 
?>

<a href='<?php echo $link; ?>'>

1

Your code was misspelled, follows the correction of it, no change in your style:

<a href='<?php
    if($dado['st_nome'] =="FINALIZADO"){
        echo "dashboard.php?link=17&id=" .  $dado["op_id"]; 
    }else{ 
        echo '#';        
    } ?>'>Link</a>

It also has the version posted by Jorge B., which separates PHP from HTML and leaves the code cleaner.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.