-3
  <?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>

  } 

 ?>

I'm a newbie to PHP, creating a Madlib PHP asignment. However I keep getting this error:

Parse error: syntax error, unexpected 'my' (T_STRING), expecting ',' or ';' - on line 17 (below)

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

I attempted to fix it with everything I've learned so far, within 2 weeks, but have had no luck. It' probably a mistake I haven't learned yet.

Thank you for any help

Jason Quinn
  • 25
  • 1
  • 6
  • ```echo Hi ``` - you are missing a tick before "Hi". – Matthias W. Jan 28 '16 at 16:04
  • add quotes around any string that you are trying to `echo`. `echo "Hi my name is " . $celebrity . "....."` – Scott Jan 28 '16 at 16:05
  • printing a string should be enclosed withing double or single quotes. – ameenulla0007 Jan 28 '16 at 16:05
  • I presume your string is wrapped in single quotes. As you're using the word `I'm`, you need to escape the single quote for it to be treated literally, eg. `$string = 'I\'m a string';`. Alternatively you could use double quotes, that way you wouldn't need to escape single quotes. Variables within double quotes are interpolated, so you wouldn't need to concatenate the variables. – billyonecan Jan 28 '16 at 16:16
  • I believe I was able to fix the original error however now I get "syntax error, unexpected end of file in" line 22.. all that I have in the closing PHP ?> – Jason Quinn Jan 28 '16 at 16:19

2 Answers2

0

Use quotes for strings

Use single or double quotes for strings. It's a large difference (interpolation). Your example below, with single quotes:

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

More readable with Heredoc

With heredoc, you increase the readability of your string. Take a look below:

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 your input

Be care with input from outside! You can simple sanatize, like so:

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

If you're using the $input, you're save.

schellingerht
  • 5,726
  • 2
  • 28
  • 56
0

I would use double-quotes since you've used the word I'm. It's just a personal preference.

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>";
Jamie Harding
  • 383
  • 3
  • 22