17

In Windows (WAMP) I have an index.php like this

<?xml version="1.0" encoding="utf-8"?>
<?php
    // ...
?>

and it works as expected.
Now I've just configured a LAMP on Ubuntu and the same file (when invoked from a browser) gives me an error. Looking in error.log I've found

PHP Parse error: syntax error, unexpected T_STRING in /var/www/test/index.php on line 1

If I remove first line everything works fine.
What's wrong? Why this works on Windows and not on Linux?
Could this be caused from a particular extension?

hakre
  • 193,403
  • 52
  • 435
  • 836
Marco
  • 56,740
  • 14
  • 129
  • 152
  • I think the short tags answers will work; in my experience, I've always echoed the `` string from PHP code so I didn't have to worry about how short tags were set. – WWW May 08 '12 at 20:45
  • 2
    echo ''; – xpredo Feb 19 '16 at 14:14
  • This also happens if you try and run `phpunit` and don't complete a parameter. For example `phpunit --filter -c phpunit.xml` would trigger this same error. – styks Jun 14 '17 at 13:48
  • just echo question mark as string. <{{"?"}}xml version="1.0" encoding="UTF-8"{{"?"}}> – reza jafari Oct 10 '19 at 11:15

3 Answers3

43

It sounds like you have short tags enabled, which will cause PHP to try and parse what comes after <?.

Set the config option short_open_tag in php.ini to 0 or Off and restart Apache.

nickb
  • 59,313
  • 13
  • 108
  • 143
3

It's not a good idea to work with XML as a string.

You should use php XML libraries like http://de.php.net/manual/en/book.dom.php

Dmitry
  • 7,457
  • 12
  • 57
  • 83
1

Did you check if short tags are enabled/disabled on php.ini?

gunnx
  • 1,229
  • 7
  • 16