Home > Backend Development > PHP Tutorial > 解决PHP Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$'

解决PHP Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$'

王林
Release: 2023-08-26 17:14:02
Original
2594 people have browsed it

解决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:

  1. Incorrect use of strings: Usually in PHP, strings should be wrapped in quotes (single quotes or double quotes). This error occurs if an unquoted string is used in an assignment statement or function call. For example:
$name = John; // 错误的写法,字符串应该被引号包裹起来
echo $name;
Copy after login

The correct way to write it is:

$name = "John"; // 引号包裹起来的字符串
echo $name;
Copy after login
  1. Wrong nesting of quotes: In PHP, if you need to include quotes in a string, you can Use different types of quotes to nest. For example:
$message = "He said, "Hello World!""; // 错误的引号嵌套
echo $message;
Copy after login

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;
Copy after login

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;
Copy after login

The correct writing should be:

$name = "John"; // 添加分号作为结束符
echo $name;
Copy after login
  1. Forgot to use the "." symbol when splicing strings: In PHP, if you want to concatenate multiple strings in Together, you need to use the "." symbol for splicing. If you forget to use this symbol when concatenating strings, this error will occur. For example:
$name = "John" "Doe"; // 错误,忘记使用"."进行字符串拼接
echo $name;
Copy after login

The correct way to write it should be:

$name = "John" . "Doe"; // 使用"."进行字符串拼接
echo $name;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template