Home > Article > Backend Development > What to do if you encounter a php error
Solutions to encounter a PHP error: 1. Check the syntax format; 2. Check the function name; 3. Convert the data type; 4. Check whether the variable or function is named correctly; 5. Modify the SQL statement; 6 , Check the definition of the variable; 7. Check whether the array key name is spelled incorrectly or is undefined; 8. Declare the character set.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
What should I do if I encounter a php error?
Several common mistakes and solutions for php beginners:
Error 1:
Parse error: syntax error, unexpected …
This This error is PHP
Cause and solution:
Wrong syntax format.
For example: each statement should end with an English semicolon. If the semicolon is not written or is typed in a Chinese semicolon, such an error will be reported. This can be avoided as long as we pay attention to the syntax format when writing code.
Error 2:
Fatal error: Uncaught Error: Call to undefined function…
This error indicates that the code encountered a fatal error during execution, literally translated as: Uncaught Error: Called Undefined function
Cause and solution:
An undefined function was called. When encountering this kind of error, we first need to check the error message, locate the line where the error code is located, and check whether the function name is spelled incorrectly or whether the custom function is really not defined.
Solution:
Error three:
Notice: Array to string conversion in…
This error is a very easy problem for beginners. It is just an error message and does not affect the execution of the entire code. Literally translated as: The array is converted into a string at line...
Solution:
This error is usually caused by an array being output after echo, or the array is spliced as a string caused during operation. (php is a weakly typed language and will automatically convert data types according to environmental needs during operation)
We only need to find the line of code prompted, process the array in advance, and then perform string operations.
Error four:
Parse error: syntax error, unexpected '1' (T_LNUMBER), expecting variable
Cause and solution:
Locate the error code and check whether the variable or function The naming is correct.
Error five:
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given …
The literal translation of this error is:
Warning: the mysqli_fetch_assoc function expects parameter 1 to be the result of mysqli set, but given a boolean value. This error is a warning error and does not affect program execution.
This error usually occurs when the database reads the result set data. For example, when we write a SQL statement to read data from the database, we should return the result set and then read the data into the result set. However, the result set is a Boolean value, so it is very likely that the SQL statement was written incorrectly, causing an execution error, and the result set becomes the execution result false.
Solution:
Check the SQL statement carefully and confirm that there is no problem before submitting it to the server for execution.
Error six:
Notice: Undefined variable:…
The literal translation of this error is: Tip: Undefined variable a
Cause and solution:
This error generally occurs when undefined variables are used. When encountering this kind of problem, we only need to double-check whether the definition or spelling of the variable is correct.
Error 7:
Notice: Undefined index: username in …
The literal translation of this error is: Tip: Undefined index: username
Cause and solution Method:
This error generally occurs when undefined array elements are used. Associative arrays in PHP can use key names to access values. At the same time, they are also prompted by indexing the array, so many beginners will misunderstand.
We only need to carefully check whether the array key name is spelled incorrectly or is undefined.
Error 8: Garbled characters
Cause and solution:
Character set and browser when php generates html code This is caused by the different character sets displayed by default. We can add a response header at the top of the php script to declare the character set. For example: [header('Content-Type: text/html; charset=utf-8')].
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What to do if you encounter a php error. For more information, please follow other related articles on the PHP Chinese website!