解析错误:语法错误,意外'我'(T_STRING),期待','或';

时间:2016-01-28 16:04:01

标签: php

  <?php

if (isset($_POST)) {
$food = $_POST['food'];
$job = $_POST['job'];
$adjective = $_POST['adj'];
$phrase = $_POST['phrase'];
$animal = $_POST['animal'];
$verb = $_POST['verb'];
$place = $_POST['place'];
$celebrity = $_POST['celebrity'];
$buy = $_POST['buy'];
$thing = $_POST['thing'];
$month = $_POST['month'];


echo Hi my name is '.$celebrity.', but my friends call me '.$adj.''.$food.' My favorite color is the color of '.$thing.' </br>

my favorite thing to undertake is '.$believe.' My parents were a '.$animal.' in addition to a '.$job.', which is why we lived in '.$place.' ; </br>
You probably know me from my TV commercial advertising '.$buy.' I'm the one who says, '.$phrase.' at the end '.$month.''s newspaper. ; </br>

  } 

 ?>

我是PHP的新手,创建了一个Madlib PHP asignment。但是我一直收到这个错误:

解析错误:语法错误,意外'我'(T_STRING),期待','或';' - 第17行(下)

你好我的名字是'。$ celebrity。',但是我的朋友叫我'。$ adj。''。$ food。'我最喜欢的颜色是'。$ thing'的颜色。

我试图用我迄今为止学到的所有东西来修复它,在2周之内,但没有运气。这可能是我还没有学到的错误。

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

使用字符串引号

对字符串使用单引号或双引号。这是一个很大的差异(插值)。您的示例如下,单引号:

echo 'Hi my name is '.$celebrity.', but my friends call me '.$adj . $food.' My favorite color is the color of '.$thing.' </br>my favorite thing to undertake is '.$believe.' My parents were a '.$animal.' in addition to a '.$job.', which is why we lived in '.$place.'</br>You probably know me from my TV commercial advertising '.$buy.' I'm the one who says, '.$phrase.' at the end '.$month.'s newspaper';

Heredoc

更具可读性

使用heredoc,可以提高字符串的可读性。看看下面:

echo <<<EOT
Hi my name is $celebrity, but my friends call me $adj $food. My favorite color  is the color of $thing
My favorite thing to undertake is $believe My parents were a $animal in addition to a $job, which is why we lived in $place
You probably know me from my TV commercial advertising $buy I'm the one who says, $phrase at the end $month newspaper.
EOT;

对您的输入进行Sanatizing

注意外面的输入!你可以这样简单地进行sanatize:

$input = array_map('strip_tags', $_POST);

如果您正在使用$input,则需要保存。

答案 1 :(得分:0)

我会使用double-quote,因为您已经使用了 I&#39; m 这个词。这只是个人偏好。

echo "Hi my name is $celebrity, but my friends call me $adj $food My favorite color is the color of $thing </br> my favorite thing to undertake is $believe My parents were a $animal in addition to a $job, which is why we lived in $place. </br>You probably know me from my TV commercial advertising $buy I'm the one who says, $phrase at the end $month's newspaper.</br>";