PHP Syntax Check: Parse error: syntax error, unexpected [duplicate] - php

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>';
}
?>

Related

syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE) [duplicate]

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

Not able to solve Parse Error [duplicate]

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

syntax error, unexpected T_STRING, expecting ',' or ';'” [duplicate]

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.

Why differences in output using SUPERGLOBAL in PHP?

I am getting error by running following code:
<?php
//superglobal.php
foreach($_SERVER as $var=>$value)
{
echo $var=>$value.'<br />'; //this will result in to following error:
//Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ',' or ';' in
//C:\xampp\htdocs\w_j_gilmore\CH_03_PHP_BASICS\superglobal.php on line 6
}
?>
And following code runs successfully
<?php
//superglobal.php
foreach($_SERVER as $var=>$value)
{
echo "$var=>$value<br />";
}
?>
Printing in single quotation and double quotation is difference.
WHY?
The difference between the 2 is that in the first you are trying to use the => operator (which is not valid in that place, so will result in a syntax error), while in the second you print a string which happens to have the characters = and > in it.
The second effectively could be rewritten as:
<?php
//superglobal.php
foreach($_SERVER as $var=>$value)
{
echo $var . "=>" . $value . "<br />";
}
?>
If you are just trying to output $_SERVER for debug reasons, I suggest using var_dump or print_r
var_dump($_SERVER);
You have not quoted the string:
echo $var=>$value.'<br />';
quoted would look like this:
echo '$var => $value <br />';
if single quoted, variables are not interpreted.

PHP-Paragraph in Echo statement error?

I want to echo these two strings. This is my current code:
echo str_replace(array_keys($swears), array_values($swears), $main).<br />;
echo str_replace(array_keys($swears), array_values($swears), $post).<br />;
But it produces this error:
Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\new\new.php
Please let me know your feedback.
Use this instead since you have to use quotes for displaying HTML tags:
echo str_replace(array_keys($swears), array_values($swears), $main)."<br />";
echo str_replace(array_keys($swears), array_values($swears), $post)."<br />";
echo str_replace(array_keys($swears), array_values($swears), $main) . '<br />';
Ths is a string, so it has to be quoted.
echo str_replace(array_keys($swears), array_values($swears), $main). '<br />';

Categories