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

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!

Related

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

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

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

Error: Parse error: syntax error, unexpected '[', expecting ',' or ';' [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I'm getting a parse error:
Parse error: syntax error, unexpected '[', expecting ',' or ';' in index.php on line 23
Line 23 is this:
echo "<h3>Message received from: </h3>" . $er->GetMessage($i)["header"]->fromaddress;
I changed the syntax in all similar lines to
echo "<h3>Message received from: </h3>" . $er->GetMessage($i)["header"]['fromaddress'];
but I get the identical error message.
$er is an array of email messages. Each array element is an array of key-value pairs, and the value for the key 'header' is a long array of key-value pairs.
The function $er->GetMessage($i) returns an array, so this sytax
$er->GetMessage($i)["header"]['fromaddress']
seems like it would work. So, I'm at a loss. The arrow syntax doesn't make sense to me at all, because 'fromaddress' is not a member of 'header' — 'header' is not an object.
What am I missing? I did a search on "Parse error: syntax error, unexpected '[', expecting ',' or ';'" and I found one match. The solution was "- the php wasn't on automatic update so now it is the site is working fine." I don't think this applies to my issue.
The code is below.
Line 23, the line that triggered the error message, is in the for loop.
echo "<h3>Message received from: </h3>" . $er->GetMessage($i)["header"]->fromaddress;
This is php between body tags:
for ($i=0;$i<$er->GetInboxCount();$i++)
{ // fromaddress, date, subject are from imap_headerinfo
echo br.br;
echo "<h3>Message received from: </h3>" . $er->GetMessage($i)["header"]['fromaddress'];
echo br.br;
echo "<h3>Message received on: </h3>" . $er->GetMessage($i)["header"]['date'];
echo br.br;
echo "<h3>Subject: </h3>" . $er->GetMessage($i)["header"]['subject'];
echo br.br;
echo "<h3>Message in text format:</h3>" . $er->GetMessage($i)['body_text'];
echo br.br;
echo "<h3>Message in html format: </h3>" . $er->GetMessage($i)['body_html'];
Try to use this:
$foo = $er->GetMessage($i);
echo "<h3>Message received from: </h3>" . $foo["header"]->formadresss;

Parse error: syntax error, unexpected 'booked' (T_STRING) in eval()'d code

I am receiving the following error:
Parse error: syntax error, unexpected 'booked' (T_STRING) in eval()'d code.
Here is my code:
$cvalue = do_shortcode( $cvalue );
eval( '$cvalue="\n<div class=\"advcustomvalue\">\n' . $cvalue . '\n</div>\n";' );
echo urldecode( stripslashes( $cvalue ) );
if ( $posttext ) {
echo $posttext;
}
}
How can I resolve this syntax error in the eval code (line 3)?
Thanks,
Jonathan
Don't use eval, like NEVER, especially not if you want to do a trivial thing as concatenating strings.
Just do this:
$cvalue = sprintf("\n<div class=\"advcustomvalue\">\n%s\n</div>\n", $cvalue);

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.

Categories