Home >Backend Development >PHP Tutorial >解决PHP Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$'
Solution to PHP Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$'
In the process of writing PHP code, we often encounter various All kinds of errors. One of the common errors is "Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$'". This error message means that an unexpected string was encountered in the code, where it should be a variable name or a "$" symbol.
This error is usually caused by the following reasons:
$name = John; // 错误的写法,字符串应该被引号包裹起来 echo $name;
The correct way to write it is:
$name = "John"; // 引号包裹起来的字符串 echo $name;
$message = "He said, "Hello World!""; // 错误的引号嵌套 echo $message;
In the above example, the double quotes are nested incorrectly. The correct nesting of quotation marks should be:
$message = 'He said, "Hello World!"'; // 正确的引号嵌套 echo $message;
3. The semicolon terminator is ignored: PHP needs to use a semicolon as the terminator of a statement. This error occurs if a semicolon is not added after the end of a statement. For example:
$name = "John" // 错误,缺少分号作为结束符 echo $name;
The correct writing should be:
$name = "John"; // 添加分号作为结束符 echo $name;
$name = "John" "Doe"; // 错误,忘记使用"."进行字符串拼接 echo $name;
The correct way to write it should be:
$name = "John" . "Doe"; // 使用"."进行字符串拼接 echo $name;
The above are some common causes of "Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$'" errors and solutions. When this error occurs, you can review your code to find and fix problems such as incorrect use of quotes, missing semicolon terminators, or incorrect concatenation of strings. Hope this article can help you solve this error so that your PHP code can run normally.
The above is the detailed content of 解决PHP Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$'. For more information, please follow other related articles on the PHP Chinese website!