This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I am writing php code in an html. Now I get a parse error which I am not able to trace!Could somebody help me fix it
echo '<a href="'.base_url('uploads/files/'.$file['file_name']).'">
<img src="echo base_url('uploads/files/'.$file['file_name'])">
<p>Uploaded On '.date("j M Y",strtotime($file['created'])).'</p>';
Error:-Parse error: syntax error, unexpected 'uploads' (T_STRING), expecting ',' or ';'
echo '<a href="'.base_url('uploads/files/'.$file['file_name']).'">
<img src="'.base_url('uploads/files/'.$file['file_name']).'">
<p>Uploaded On '.date("j M Y",strtotime($file['created'])).'</p>';
Step by step
$bu = base_url('uploads/files/'.$file['file_name']);
$d = date("j M Y",strtotime($file['created']));
echo '<img src="'.$bu.'"/><p>Uploaded On '.$d.'</p>';
to prevent typos and syntax errors
Note: added an missing </a>
Make a var like this :
$linkToImg = base_url('uploads/files/'.$file['file_name']);
$uploadDate = date('j M Y',strtotime($file['created']));
echo "<a href='". $linkToImg ."'>";
echo "<img src='". $linkToImg ."'>";
echo "<p>Uploaded On ". $uploadDate ."</p>";
echo "</a>"; // Don't forget to close <a> tag
Related
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 3 years ago.
I have a problem with this line,is there any solution for a line which must contain both single and double quotation marks?
echo "<a href='/ad/.$row['id_ad']'>".$row['title'];
I get this error:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
Exit the "echo" and concatenate with a dot.
echo "<a href='/ad/" . $row['id_ad'] . "'>" . $row['title'];
That would be the exact match of your line but I believe you forgot to close the anchor.
echo "<a href='/ad/" . $row['id_ad'] . "'>" . $row['title'] . "</a>";
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
PHP Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
used
echo img src=$row['logo']>."<br />";
what is the correct use
I wanted to show it as a picture
You have to change your code as below:
echo "<img src='$row[logo]' /><br />";
OR
echo "<img src='".$row[logo]."' /><br />";
<?php
echo "some string {$variable['withKey']}";
encapsulate the variable with { and }
echo "<img src=\"$row['logo']\" />";
notice the quotes around your src attribute which are needed for your HTML to work
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
Cant figuer out the problem with my code. Theres a syntax error,and i just cant spot it
Checked it here - http://phpcodechecker.com/ , and got this as an answer -
Parse error: syntax error, unexpected ''.$val[''
(T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' in your code on
line 10
Here is the code -
<?php
include "products.php";
foreach($products as $key=>$val)
{
echo '<div style="float:right;text-align:center;margin:20px;">';
echo '<IMG src="img/'.$val['category'].'/'.$val['image'].'" WIDTH="94" HEIGHT="94" BORDER="0" ALT=""><br />';
echo $val['name'].'<br />';
echo '₪ '.$val['price'].'<br />';
echo '<a onmousedown="parent.AddItemToCart(''.$val['id'].'' ,''.$val['name'].'','img/'.$val['category'].'/'.$val['image'].'','.$val['price'].'')"></a><br/>';
echo '</div>';
}
?>
Replace
echo '<a onmousedown="parent.AddItemToCart(''.$val['id'].'' ,''.$val['name'].'','img/'.$val['category'].'/'.$val['image'].'','.$val['price'].'')"></a><br/>';
to
$imgtag = 'img/'.$val['category'].'/'.$val['image'];
echo '<a onmousedown="parent.AddItemToCart(\''.$val['id'].'\',\''.$val['name'].'\',\''.$imgtag.'\',\''.$val['price'].'\')"></a><br/>';
The unescaped ' terminates the string before you want it to. Try:
echo '<a onmousedown="parent.AddItemToCart(\''.$val['id'].'\' ,\''.$val['name'].'\','img/'.$val['category'].'/'.$val['image'].'','.$val['price'].'\')"></a><br/>';
or use double quotes:
echo "<a onmousedown=\"parent.AddItemToCart('".$val['id']."' ,'".$val['name']."','img/".$val['category']."/".$val['image']."','.$val['price']."')\"></a><br/>';
Reason: The reason of this happening, is that you use ' to start and end your string, but your string also contains the ' character. This way the string ends at the position you enter ' and is confused by the following symbols. To prevent this, you escape the symbols using a backslash \ like so: 'some string containing a \' character'
Change:
echo '<a onmousedown="parent.AddItemToCart(''.$val['id'].'' ,''.$val['name'].'','img/'.$val['category'].'/'.$val['image'].'','.$val['price'].'')"></a><br/>';
For this:
echo "<a onmousedown='parent.AddItemToCart(".$val['id']. ',' .$val['name']. ',img/'.$val['category'].'/'.$val['image'].','.$val['price'].")></a><br/>";
Change the code with given code
<?php
include "products.php";
foreach($products as $key=>$val)
{
echo '<div style="float:right;text-align:center;margin:20px;">';
echo '<IMG src="img/'.$val['category'].'/'.$val['image'].'" WIDTH="94" HEIGHT="94" BORDER="0" ALT=""><br />';
echo $val['name'].'<br />';
echo '₪ '.$val['price'].'<br />';
echo '<a onmousedown="parent.AddItemToCart('.$val['id'].','.$val['name'].'','img/'.$val['category'].'/'.$val['image'].','.$val['price'].')"></a><br/>';
echo '</div>';
}
?>
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
echo '<a href="'. $site_url .'" onclick=" window.open('. $refferal_url .'); ">';
results
<a href="http://example.com/" onclick=" window.open(http://example.com/some-url/); ">
The problem is i can't get to put single quote before and after http://example.com/some-url/
because i always get the error.
How can i do this?
You have to escape the single quotes because they are in a single-quoted string. Add \' where you want the single quotes.
Example:
echo '<a href="'. $site_url .'" onclick=" window.open(\''. $refferal_url .'\'); ">';
By the way, "referral" has two rs, not two fs.
This question already has answers here:
Difference between period and comma when concatenating with echo versus return?
(9 answers)
Closed 7 years ago.
In PHP how do you know if you should use each form of echo, and what is the proper uses for each:
with the period:
echo"<div>
<img src='".$row['image']."'>
".$row['text']."
</div>
with the comma:
echo"<div>
<img src='",$row['image'],"'>
",$row['text'],"
</div>
with the {}:
echo"<div>
<img src='{$row['image']}'>
{$row['text']}
</div>
preset:
$image=$row['image'];
$text=$row['text'];
echo"<div>
<img src='$image'>
$text
</div>
More specifically I'm looking for the difference in how PHP will add the text to the HTML dump on the front end.
. concatenates strings (+ in most languages, but not PHP). PHP manual operators
, separates arguments (echo can take more than 1 argument). PHP manual echo
In your first example
echo" ".$row['text']." must be changed to
echo " ".$row['text']." which is write syntax to concatenate two string.
For second example
echo" ",$row['text']," must be changed to echo " ",$row['text']," I don think this is valid syntax in php .
$image=$row['image']; $text=$row['text']; echo" $text must be like
$image=$row['image']; $text=$row['text']; echo $text;
with period:
echo"<div>
<img src='".$row['image']."'>
".$row['text']."
</div>";
src='".$row['image']."' denotes the concatenation of $row['image']
with comma:
echo"<div>
<img src='",$row['image'],"'>
",$row['text'],"
</div>";
^The comma , defines the string separation in the above example.
preset:
$image=$row['image'];
$text=$row['text'];
echo"<div>
<img src='$image'>
$text
</div>
^While this would still work with single '$image' A better practice would be '".$row['image']."' wrapped around quotes, i.e. below:
echo "<div>
<img src='".$image."'>
".$text."
</div>";
Note: Keeping it simple, . for concatenation and , for separation.
^An Example:
<?php
$str = "Hawas" . "Ka" . "Pujaari";
echo $str;
$str2 = "Hawas, " . "Ka, " . "Pujaari";
echo $str2;
?>