0

Something is wrong with my print "<option value..."; line of php code.

It keeps generating error messages: an attribute value specification must be an attribute value literal unless SHORTTAG YES is specified

<option value = Addition>Addition</option>
<and so on...>

Okay, so how do i fix this line of code:

//foreach loop to cycle through the array
      foreach ($testFiles as $myFile)
      {
        $fileBase = substr($myFile, 0, strlen($myFile) - 5);
       **//Problem here:**
        print "<option value = $fileBase>$fileBase</option>\n";
      } // end foreach

such that's it's html compliant, the php code works fine, I just need validation on the html, cause you can't validate php, and the variable $fileBase references an html file, in this case Addition would be one of the files allotted to $fileBase.

3 Answers 3

3
print "<option value = \"$fileBase\">$fileBase</option>\n";

Should do it

2
  • 2
    ..as all attributes must be enclosed in (double) quotes according to the XHTML spec. Dec 12, 2009 at 1:36
  • @Tatu Ulmanen: Can you point me to the XHTML spec document about double quotes? I was under the impression single quote is acceptable as well.
    – o.k.w
    Dec 12, 2009 at 1:50
0
print "<option value='$fileBase'>$fileBase</option>\n";
0

There are a few other options:

You could use printf:

printf('<option value="%s">%s</option>', $fileBase, $fileBase);

A here-doc:

echo <<<HTML
    <option value="$fileBase">$fileBase</option>
HTML;

You could drop out of PHP temporarily (generally a good technique, but not very pretty here):

<?
foreach ($testFiles as $myFile) {
    $fileBase = substr($myFile, 0, strlen($myFile) - 5);
    ?>
    <option value="<?= htmlentities($fileBase) ?>"><?= htmlentities($fileBase) ?></option>
    <?
}
?>

But really, what you should be doing is using one of the many templating systems out there, and not mixing HTML in with your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.