Needed help on this error

Hey everyone! I am a new user. I need help with this issue which is going on today.


Parse error: syntax error, unexpected '$user_name' (T_VARIABLE) in C:\xampp\htdocs\why\scripts\create_user.php on line 3


  $user_name = trim($_REQUEST['user_name']);

Thank you!

Hi @earthgood84 and welcome to the forums!
I don’t see anything wrong with it but it is probably the code before it that has a problem.
It would help if you posted it as well.


My guess is a missing semi-colon on the end of the preceding line - or something else that causes the PHP processor to consider this to be an extension of the preceding line rather than a new line. As @Andres_Vaquero said above, there’s nothing wrong with the line in isolation.

If you have an include or require statement prior to this one, perhaps the problem is in that code. At least on line 3 there aren’t many places to check. :slight_smile:

1 Like

This is a good lesson though when it comes to PHP.

If your error says “Unexpected (anything)”, especially when the thing it’s saying is an error is the first thing on the line, immediately look at the line before it - you’ve either forgotten a semicolon at the end of the line, or you’ve not closed a function (count your ()'s, your []'s, and your {}'s…)

OP, if you use a decent IDE it would show you simple mistakes. If you want to go top dog, get PhpStorm. There are many other good IDE’s and editors out there as well, both free and paid.

Thanks for helping. It’s kinda weird that if I do echo once again it popped out as no errors.

I can still forget the differences between single / double quotes, eg.

$foo = "bar"; 
echo "var $foo"; // var bar - evaluated 
echo 'var $foo'; // var $foo - str literal 
echo 'var'.$foo; // varbar - concatenation 
echo 'O\'brien'; // O'brien - escape 
echo "O'brien"; // O'brien - alternate 

Concatenation can be tricky. eg. what it does can be like putting together
PHP - text string - PHP var val - text string - PHP var val - text string - PHP var val …

It might not look that difficult, but … :blush:

1 Like

I read somewhere about how it is better to use commas instead of periods to concatenate strings.

1 Like

I think it’s preference, at least I can’t recall ever needing to use commas. My priority is to aim for readability and hope for the best. So if commas made code more readable I can’t say I might not use them someday.

When things get messy I often use heredoc / nowdoc (kind of like block level quotation marks)

And I have appended, eg.

$output = "var "; 
$output .= $foo; 
echo $output; 
/* var bar */

A priority next to readability is consistency. For me, using different style in different code can make reading more difficult than it needs to be.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.