I am having problems with the code below where I'm getting:

Parse error syntax error, unexpected '<'

What I'm I doing wrong?

Here's the code:

<?php if($myvar[123]){ ?>

<div>

<?php if ($myvar == 'value1'){ //ERROR IN THIS LINE
echo '<img src="image1.png" />';
} else {
echo '<img src="image2.png" />';    
} 
?>
<input type="text">
</label>
</div>


<?php } ?>
link|improve this question

Looks Ok to me. Which line is PHP complaining about? – Salman A Feb 15 at 10:48
No errors in that code, can you show the real thing? – Mark Baker Feb 15 at 10:49
Just noticed the </label> closing tag is unmatched. – halfer Feb 15 at 10:51
The code syntax looks ok. Could you check again if the Parse error is actually on that line? – mazzucci Feb 15 at 10:51
What are you checking by $myvar[123] on the 1st if statement? – itachi Feb 15 at 10:54
show 2 more comments
feedback

2 Answers

up vote 4 down vote accepted

Try with:

<?php if($myvar[123]): ?>

<div>
    <?php if ($myvar == 'value1'){ //ERROR IN THIS LINE
        echo '<img src="image1.png" />';
    } else {
        echo '<img src="image2.png" />';    
    } 
    ?>
    <input type="text">
    </label>
</div>

<?php endif; ?>
link|improve this answer
+1. Also, the if block can be replaced with a single line containing a ternary operator. – halfer Feb 15 at 10:50
I didn't switched it because it does not cause error. – hsz Feb 15 at 10:51
Sure, although the bit you changed, whilst an improvement, also wouldn't have caused the error either. My suggestion is a similar improvement; I try to only use single lines of PHP in my template layer. – halfer Feb 15 at 10:53
This is just another variant of if - else statement. If OPs code throws an error, this one will do too. – itachi Feb 15 at 11:01
feedback

i ran this on my web server

        <?php $myvar[123] = true; ?>
<?php if($myvar[123]){ ?>

<div>

<?php if ($myvar == 'value1'){ //ERROR IN THIS LINE
echo '<img src="image1.png" />';
} else {
echo '<img src="image2.png" />';    
} 
?>
<input type="text">
</label>
</div>


<?php } ?>

it worked fine all i added was initilisation of $myvar

link|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.