- Saved searches
- Use saved searches to filter your results more quickly
- PHP Parse error: syntax error, unexpected ‘class’ (T_CLASS) #49
- PHP Parse error: syntax error, unexpected ‘class’ (T_CLASS) #49
- Comments
- Синтаксическая ошибка в class
- Syntax error, unexpected ‘class’ (T_CLASS) on line 1 after upgrade to php 5.6.4
- 2 Answers 2
- Fix the line endings from your editor, for your project via regex replace.
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PHP Parse error: syntax error, unexpected ‘class’ (T_CLASS) #49
PHP Parse error: syntax error, unexpected ‘class’ (T_CLASS) #49
Comments
I’m testing the wse-rest-library-php with the Example configuration given in documentation (index.php, config.php).
When testing the page, I get the following error to Apache error log:
PHP Parse error: syntax error, unexpected ‘class’ (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or ‘
Is there something, I’m doing wrong? A quick peek to the Server.php -file did not reveal anything obvious.
The text was updated successfully, but these errors were encountered:
I’m using the example from the Configuration Example, just changed the config.php name to cfg.php and used different path for it.
Centos 7.4.1708, apache and php version is:
PHP 5.4.16 (cli) (built: Mar 7 2018 13:34:47)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
registerDebugHandlers(); //$framework->processHttpSapiRequest(); require_once("../config/cfg.php"); // make sure this exists and is similar to the below - move it where it needs to be
setHost(WOWZA_HOST); $setup->setUsername(WOWZA_USERNAME); $setup->setPassword(WOWZA_PASSWORD); // Connect to the server or deal with statistics NOTICE THE CAPS IN COM AND WOWZA $server = new Com\Wowza\Server($setup); $sf = new Com\Wowza\Statistics($setup); $response = $sf->getServerStatistics($server); var_dump($response);
Синтаксическая ошибка в class
Добавлено через 10 минут
Parse error: syntax error, unexpected ‘class’ (T_CLASS) in C:\xampp\htdocs\site.local\dvich\lib\checkvalid_class.php on line 4 -в в\dw уже нет ошибки но браузер все ровно показывает ошибку почему?
Синтаксическая ошибка
Всем привет я зациклился в строках знаю что проблема в грамматике выдает что Parse error: syntax.
Синтаксическая ошибка
В чем может быть ошибка? <!doctype html> <html lang="ru"> <head> <meta charset="UTF-8"> .
Синтаксическая ошибка
Помогите пожалуйста: Parse error: syntax error, unexpected ‘function’ (T_FUNCTION) in.
Синтаксическая ошибка
Парни всем добра! Подскажите пожалуйста что не так у меня в коде. $bmw = ; Создал массив в.
есть она закрыта в самом низу!
Добавлено через 3 минуты
Вот вообще весь код
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
require_once "config_class.php" /*класс для проверки данных */ class CheckValid { private $config; public function __construct() { $this->config = new Config(); } public function validID($id) { if (!$this->isIntNumber($id)) return false; if ($id 0) return false; return true; } public function validLogin($login) { if ($this->isContainQuotes($login)) return false; if (preeg_match("/^\d*$", $login)) return false; return $this->validString($login, $this->config->min_login, $this->config->max_login); } public function validHash($hash) { if (!$this->validString($hash, 32, 32)) return false; if (!$this->isOnlyLettersAndDigits($hash)) return false; return true; } public function validTimeStamp($time) { return $this->isNoNegativeInteger($time); } /*проверка на целые числа*/ private function isIntNumber($number) if (!is_int($number) && !is_srting($number)) return false; if (!preg_match("/^-?((25* /*проверка не отрецательного числа*/ private function isNoNegativeInteger($number) { if (!$this->isIntNumber($number)) return false; if ($number 0) return false; return true; } /*проверка в строке только букв и цифр*/ private function isOnlyLettersAndDigits($string) { if (!is_int($string) &&(!is_string($string))) return false; if (!preg_match("/[a-za-я0-9]*/i",$string)) return false; return true; } /*проверка строки на валидность*/ private function validString($string, $min_length, $max_length) { if (!is_string($string)) return false; if (strlen($string) $min_length) return false; if (strlen($string) >$max_length) return false; return true; } /*наличие в строке ковычек защита*/ private function isContainQuotes($string) { $array("\"", "'", "`", """, "'"); foreach ($array as $key => $value) { if (strpost ($string, $value) !== false) return true; } return false; } } ?>
Syntax error, unexpected ‘class’ (T_CLASS) on line 1 after upgrade to php 5.6.4
After upgrading to php 5.6.4 my widgets file causes: Parse error: syntax error, unexpected ‘class’ (T_CLASS) in /home/path/fss-widgets.php on line 1 I’m mystified — several hours research has yielded nothing. The problem doesn’t appear to be quoting, escaping, or anything else you would expect in this situation and the code looks ok to me.
2 Answers 2
An error about something on line 1 that is actually on a later position means that PHP doesn’t recognize your line endings.
There are three ways to encode a line ending, and PHP understands only two of them:
- LF, or \n , Line Feed, U+000A
- CR, or \r , Carriage Return, U+000D
- CRLF, or \r\n , the combination of the first and the second
LF is the default on systems like UNIX, Linux, and Mac OS X (since 2001).
CRLF is the default in Windows, inherited from CP/M. Just LF works on Windows too nowadays, there is no need to use CRLF anymore.
CR was the default in Classic Mac OS until 2001.
PHP doesn’t understand 2., CR only, which is understandable, because no one is using that anymore. Well, almost no one. There are still some editors out there that not only allow that obsolete line ending encoding, they don’t even warn their users when they are using it.
Set your editor to use LF only, and you are safe. Unfortunately, the WordPress Coding Standards are silent about this.
@tosho’s answer is too good.
What just happened is PHP parsers tries to parse that file and creates tokens first, and gets confused with the first token class , becuase the parser for that version of PHP cannot work well with \r characters.
Happens that also other invisible Unicode characters may break the parser. The main thing you just cannot understand the problem from the editor unless you open Hex editor.
BOM character also (props @MarkKaplun) can make problems. There are some Unicode n-dash characters that may look just like — (minus), and you can scratch your head for days.
In this case, as you confirmed \r carriage return character was your problem.
But this is dependent on PHP version. I tested your code on PHP 7 and no problem found.
I noticed that WPSO editor will trim the \r character, and I added it manually when tested.
Fix the line endings from your editor, for your project via regex replace.
But also fix endings for non-PHP files.
define('CR', "\r"); // Carriage Return: Classic Mac define('CRLF', "\r\n"); // Carriage Return and Line Feed: Windows define('LF', "\n"); // Best: Unix, Linux, Mac, Windows function fix_endings($s) < // Convert all line-endings to the Best format. $s = str_replace( CRLF, LF, $s ); $s = str_replace( CR, LF, $s ); // Don't allow out-of-control blank lines $s = preg_replace( "/\n/", LF . LF, $s ); return $s; >
The line endings are very important and may also have security implications. If the PHP parser fails on some characters you may guess some PHP functions that read files may also.