-3

Why would I be getting Parse error: syntax error, unexpected T_VARIABLE on this line?

$fieldLabel = '<label for=".'$fieldNameStripped'.">.'$fieldName'.</label>';

4 Answers 4

3

Because you're not using PHP properly - syntax errors:

$fieldLabel = '<label for="' . $fieldNameStripped . '">' . $fieldName . '</label>';
                           ^^^^                  ^^^

You had the concatenation operators INSIDE the strings, so you weren't concatenating at all.

Try

$fieldlabel = <<<EOL
<label for="$fieldNameStripped">$fieldName</label>
EOL;

HEREDOCs make such things trivial, and far far easier to read. With a modern syntax highlighting editor, the variables will even stand out for you.

You could also prepare your string like this: $fieldLabel = "{$field->name}";

Here the double quotes surround the outer string mean that PHP will parse variables inside it. You however then have to escape double quotes. I've changed the $fieldName variable to show how you would wrap the variable in {} brackets for items such as object properties (I tend to use them even for regular variables inside strings just because I feel it's better practice to be consistant).

Sign up to request clarification or add additional context in comments.

Comments

1
$fieldLabel = '<label for="'.$fieldNameStripped.'">'.$fieldName.'</label>';

Is how you should do it.

In what you have done, there are two problems:

  1. When using . for concatenation, you should confirm that the strings to both sides of . should be "properly closed".

  2. Also, say if you have $var = 1; and you echo '$var'; you don't get 1. you get $var as output.

Comments

0

your line should be like this

$fieldLabel = '<label for="' . $fieldNameStripped . '">' . $fieldName . '</label>';

1 Comment

-1. You're not answering the question as to why a parse error occurs.
0
$fieldLabel = '<label for="'.$fieldNameStripped.'">'.$fieldName.'</label>';

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.