How can I echo it properly? HTML+PHP issue [duplicate] - php

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>";

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

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

This question already has an answer here:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE) [duplicate]
(1 answer)
Closed 6 years ago.
This is my code.
<?php
$name = 'John Doe';
echo 'My name is $name';
echo "I am from $_COOKIE['mylocation']";
i got an error from this
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in : eval()'d code on line 3
if any one know about this please help me.
The variables should not be enclosed in the quotes..It should be concatenated using . after closing the quotes
Close the single quotes in echo before $name as well as $_COOKIE['mylocation']
<?php
$name = 'John Doe';
echo 'My name is'.$name;
echo "I am from". $_COOKIE['mylocation'];
Remove the ':
<?php
$name = 'John Doe';
echo 'My name is $name';
echo "I am from $_COOKIE[mylocation]";
Beware of XSS vulnerability!

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

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

Displaying the number of users online and the page they are browsing

When I open the page it returns :
Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /home/a1361025/public_html/9/9/functions.php on line 44
and this is line 44
echo "$count. ($useronline[ip]) Browsing page: $useronline[page]";
Just try with:
echo $count . ' (' . $useronline['ip'] . ') Browsing page: ' . $useronline['page'] . '';
You have to escape your quotation marks, if you dont want them to end your string:
echo "$count. ($useronline[ip]) Browsing page: $useronline[page]";
You end the string and then paste a variable straight afterwards:
echo "$count. ($useronline[ip]) Browsing page: $useronline[page]";
// ^^^^^^^^^^^^^^^^^^^
You need to escape them with a backslash (because you use double quotes inside of double quotes):
echo "$count. ($useronline[ip]) Browsing page: $useronline[page]";
// ^ ^

PHP Echo post parameter surrounded by strings [duplicate]

This question already has answers here:
Print string with a php variable in it
(4 answers)
Closed 11 months ago.
I have a textbox named 'fname'. I need to echo out the input of this box inside double quotes on a another page.
User enters: Test123
returrns: "Test123"
so how can I do that with $_POST["fname"] ?
Or you can use:
echo "Hi how are you {$_POST['fname']} ? I am fine thanks";
If you want to use it in a string surrounded by letters, simply use curly brackets {}.
Put the $_POST['fname'] to curly brackets.
Try
<?php echo('"'.htmlspecialchars ($_POST["fname"]).'"'); ?>
There are many ways:
besides the one nyarathotep mentioned:
echo sprintf('"%s"', $_POST['fname']);
printf('"%s"', $_POST['fname']);
Use this:
echo '"' . $_POST["fname"] . '"';
Or
echo "'" . $_POST["fname"] . "'";
of course if you want to you can replace ' and " with " or ' in the code...

Categories