php can't find syntax error in echo line [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
So I am getting the following parse error when this file loads:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE),
expecting identifier (T_STRING) or variable (T_VARIABLE) or number
(T_NUM_STRING)
I have tried everything to work out why and I narrowed it down to the
href=\"start.php?id=<?php echo $res['id'] ?>\"
section of the code. I am sure I left out a ' or " but unsure where as
it all makes sense to me. Can anyone with a keener eye see where I am
going wrong? Thank you.
My code:
<td>
<?php if($res['ndaSent'] == "No") {
echo "<span class=\"buttonTestDisabled\"> Start Test</span>";}
else {
echo "<a class=\"buttonTest\" href=\"start.php?id=<?php echo $res['id']
?>\">Start Test</a> ";}
?>
</td>

Take a look at that. You can't use echo command inside of an echo " " line. Try using ' inside and not \ may help you too from confusion.
<?php if($res['ndaSent'] == "No") {
echo "<span class='buttonTestDisabled'> Start Test</span>";
}else{
echo "<a class='buttonTest' href='start.php?id=".$res['id'].">Start Test</a>';}
?>
</td>

Related

Why does <?= work only sometimes where <?php works? [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 2 years ago.
Inside the body of an html document I have something like this:
<?=str_replace(' ', '_', $result[0]['something'])?>
This works perfectly fine. In the same document I have this:
<?php if(!empty($result[0]['something'])) { echo "Hello"; } else { echo " "; }?>
Which also works fine, but it slightly bothers me that I am using <?= in one place and <?php in another. When I try to change the if code to become:
<?=if(!empty($result[0]['something'])) { echo "Hello"; } else { echo " "; }?>
or
<?= if(!empty($result[0]['something'])) { echo "Hello"; } else { echo " "; }?>
Both result in a Parse error: syntax error, unexpected 'if' (T_IF) in....
I've attempted to find some documentation on the respective differences between <?php and <?= as a php opening tag but all I get is data on short tags - which this is not. Can someone explain this behavior for me?
<?= is like <?php echo. You can't echo an if statement.

Image wont display from mysql [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 2 years ago.
Hey below I tried to display my image, it may be from the way I referenced in the src. Any ideas hot to fix? Thanks
(I get Parse error: syntax error, unexpected '<' )
<td><?php echo <img src='"/images/thumbs/.$row['thumb']."';?></td>
more ways... just keep attention on " and ' orders
<td><?php echo '<img src="/images/thumbs/'.$row['thumb'].'"';?></td>
you may also use:
<td><?php echo "<img src=\"/images/thumbs/{$row['thumb']}\"";?></td>
hope to be usefull
A cleaner method:
<?php $foo = $row['thumb'];?>
<td><?php echo '<img src="/images/thumbs/'.$foo.'"';?></td>

PHP: Edit href in <a> link [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 3 years ago.
I want to edit the URL to a specific URL as I can't find the user meta in WordPress nor in the phpMyAdmin in the cPanel.
I want to change this:
if(get_user_meta( $user_id, 'googleplus', $single) !=""){
echo "<br/><a class='author-link g' title='Follow on Google+' href=".get_user_meta( $user_id, googleplus', $single )." target='_blank'>Google+</a>";
}
To:
if(get_user_meta( $user_id, 'googleplus', $single) !=""){
echo "<br/><a class='author-link g' title='Follow on Google+' href="https://example.com" target='_blank'>Google+</a>";
}
And the error is:
syntax error, unexpected 'https' (T_STRING), expecting ',' or ';'
I have tried:
href="<?php echo "http://www.example.com"; ?>"
href="https:\/\/www.example.com"
href="https://www.example.com"
Would appreciate any advice as I have zero knowledge in PHP.
You have an error in using single quoted and double quoted
in your code
change "example.com" to 'example.com'
OR use it like this way \"example.com\" to escape double quoted
The finnal right code will be
if(get_user_meta( $user_id, 'googleplus', $single) !=""){
echo "<br/><a class='author-link g' title='Follow on Google+' href='https://example.com' target='_blank'>Google+</a>";
}
change
echo "<br/><a class='author-link g' title='Follow on Google+' href="https://example.com" target='_blank'>Google+</a>";
to
echo '<br/><a class="author-link g" title="Follow on Google+" href="https://example.com" target="_blank">Google+</a>';

I can't get to echo images with php [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I am trying to echo multiple images but i get the error:
"PHP Parse error: syntax error, unexpected '?>' in
/public_html/View/Pages/Home.php on line 17".
Can you give me any guidance on how to fix it? code below
<?php
for($i=0;$i<$length;$i++) {
echo ?><img class="meme-image" src="<?php $meme["$imd_id" == "$i"]->$path?>"><?php
}
?>
Try By this.
<?php
for($i=0;$i<$length;$i++) {
echo '<img class="meme-image" src='.$meme[$imd_id == $i]->$path.' ">';
}
?>
Note : You do not require to close php tag. instead of that insert this in php echo.and also you must read this For best practice.

php if then else statement syntax error somewhere [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
So I am getting the following parse error when this file loads:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE),
expecting identifier (T_STRING) or variable (T_VARIABLE) or number
(T_NUM_STRING)
I have tried everything to work out why and I narrowed it down to the
href=\"start.php?id=<?php echo $res['id'] ?>\"
section of the code. I am sure I left out a ' or " but unsure where as it all makes sense to me. Can anyone with a keener eye see where I am going wrong? Thank you.
My code:
<td>
<?php if($res['ndaSent'] == "No") {
echo "<span class=\"buttonTestDisabled\"> Start Test</span>";}
else {
echo "<a class=\"buttonTest\" href=\"start.php?id=<?php echo $res['id'] ?>\">Start Test</a> ";}
?>
</td>
echo "<a class=\"buttonTest\" href=\"start.php?id=<?php echo $res['id'] ?>\">Start Test</a> ";
}
Is incorrect, you are closing the PHP code before you are done with the PHP code. And $res['id'] cannot be in your echo like that, you should interpolate the variable correctly in the string. Remove the starting and closing tag inside the echo like this:
echo "<a class=\"buttonTest\" href=\"start.php?id={$res['id']}\">Start Test</a> ";
Please check this awesome guide on how to fix these kind of syntax errors.
You cant had snippet in echo use simple string concatenation
<td>
<?php if($res['ndaSent'] == "No") {
echo "<span class=\"buttonTestDisabled\"> Start Test</span>";}
else {
echo "<a class=\"buttonTest\" href=\"start.php?id=".$res['id']."\">Start Test</a> ";}
?>
</td>
Try this
echo "<a class=\"buttonTest\" href=\"start.php?id=" . $res['id'] . " \">Start Test</a> ";}

Categories