php常见的难点和解决⽅案_如何解决⼏乎所有常见PHP错误php常见的难点和解决⽅案
There's a good chance you came across this article because you Googled for a PHP error message, but learning how to READ the errors yourself will make you a faster, more efficient developer, so here's a breakdown of the most common errors you'll come across in PHP development, and how to immediately find and fix the problem yourself.
您碰到这篇⽂章的机会很⼤,因为您在Google上搜索了⼀条PHP错误消息,但是学习如何⾃⼰读取错误将使您成为更快,更⾼效的开发⼈员,所以这⾥列出了您遇到的最常见错误在PHP开发中,以及如何⽴即发现并解决问题。
GENERAL SYNTAX ERRORS
⼀般语法错误
Parse error: syntax error, unexpected ';' in your_script_file.php on line 123
解析错误:语法错误,意外的';' 在第123⾏的your_script_file.php中
This error happens all the time, and the "unexpected" character will change depending on your situation. To understand this error, imagine you're reading a book.
此错误始终发⽣,并且“意外”字符将根据您的情况⽽变化。 要了解此错误,请想象您正在读⼀本书。
Without realizing it, your brain is connecting all these different words in the book together to form sentences and ideas. You read a description like "red apple" and your brain says, "These words are in the correct order and 'red' is referring to the color of the 'apple'." This process of reading something and then analyzing the words into purposes ("red" describing "apple", which is an object) is called parsing.
如果没有意识到这⼀点,您的⼤脑会将书中所有这些不同的单词连接在⼀起以形成句⼦和构想。 您读到类似“红苹果”的描述,⼤脑
说:“这些词的顺序正确,'红⾊'指的是'苹果'的颜⾊。” 读取某些内容然后将这些单词分析为⽬的的过程(“红⾊”表⽰对象“ apple”)称为解析 。
Now let's say you were reading this sentence:
现在,假设您正在阅读以下句⼦:
"George stepped out of the shadows and then he purple."
“乔治⾛出阴影,然后他紫了。”
Your brain stops and says, " purple? The way the sentence was going, I expected the next word to be some kind
of action but 'purple' doesn't belong there!"
你的⼤脑停了下来,说道:“什么……紫⾊?句⼦的前进⽅式,我希望下⼀个单词是某种动作或动词……但'紫⾊'不属于那⾥!”
This is exactly what PHP is saying when you get this error. Basically, it's reading and parsing your code, and it's expecting to see something in your code, but instead, it finds some character that it didn't expect - something that doesn't quite make sense, given the code that comes BEFORE the unexpected character.
当您收到此错误时,这正是PHP所说的。 基本上,它正在读取和解析您的代码,并且期望看到您的代码中的某些内容,但是,相反,它到了⼀些意料之外的字符-考虑到之前出现的代码,这并没有多⼤意义字符。
Let's say we get this error:
假设我们收到此错误:
Parse error: syntax error, unexpected ';' in your_script_file.php on line 2
Opening up your_script_file.php, we look at line 2 and see this:
打开your_script_file.php,我们看⼀下第2⾏,并看到以下内容:
$my_variable = ("123";
We then locate the "unexpected" character, which is the semicolon ; and then work BACKWARDS from right to left from there. So PHP is expecting something, and it wasn't a semicolon, so we need to find out what it WAS expecting. We know
the semicolon ends a line of code, so PHP obviously doesn't think the line of code is completely finished just yet. This means we look for things that started but did not end. In this case, we can see that there's an unmatched, opening parentheses before our string. So PHP is expecting that parentheses to close before it can finish the line of code. So the fix would be to add a ")" right before the semi-colon:
然后,我们到“意外”字符,即分号; 然后从右到左从左向后⼯作。 因此,PHP期望的东西不是分号,因此我们需要出它期望的东西。 我们知道分号结束了⼀⾏代码,因此PHP显然不认为代码⾏还
没有完全完成。 这意味着我们寻开始但没有结束的事情。 在这种情况下,我们可以看到在字符串之前有⼀个不匹配的开括号。 因此,PHP期望在完成代码⾏之前括号会关闭。 因此解决⽅法是在分号之前添
加“)”:
$my_variable = ("123");
Or, if the parentheses isn't needed, then another solution would be to remove the opening parentheses:
或者,如果不需要括号,则另⼀种解决⽅案是删除开头的括号:
$my_variable = "123";
Either way, we're ensuring that there is no unfinished code before the line ends.
⽆论哪种⽅式,我们都确保在⾏结束之前没有未完成的代码。
In some cases, PHP might be able to tell you specifically what it is expecting. Let's say we get this common variation of the same error:
在某些情况下,PHP也许可以告诉您具体的期望。 假设我们得到了相同错误的常见变化:
Parse error: syntax error, unexpected '=', expecting ']' in your_script_file.php on line 2
We go to line 2 in that file and see:
我们转到该⽂件的第⼆⾏,然后看到:
$my_array[123 = "Hello World";
So we find the unexpected "=" character and again we work backwards, looking at the code that is leading up to it, and we see that we're in the middle of specifying the index / key of an array using the square brackets. We've got the opening bracket [ and a value of 123 for our index / key. PHP would reasonably expect that you're ready to give it the closing square bracket ], but instead it finds the = sign, so it says, "Hey, you forgot your ']' character before you moved onto the next part of the code!"
因此,我们到了意外的“ =”字符,然后再次向后⼯作,查看导致该字符的代码,然后我们发现我们正在使⽤⽅括号指定数组的索引/键。 我们已经有了左括号[和索引/键的值123。 PHP会合理地期望您已经准备好将其放在⽅括号[]中,但是却到了=号,因此它
说:“嘿,在移⾄代码的下⼀部分之前,您忘记了']'字符。 !!”
The vast majority of these "unexpected" errors can be quickly and easily resolved by locating the unexpected character and then working backwards to see what PHP is expecting instead of the character that it found.
绝⼤多数的这些“意外”的错误可以通过定位意想不到的字符,然后向后⼯作,看看有什么PHP 期待 ,⽽不是它发现的字符快速,轻松地解决。
In a few cases, PHP will sometimes suggest the wrong thing. For example, take this line of code:
在某些情况下,PHP有时会提⽰错误的内容。 例如,采⽤以下代码⾏:
foreach($my_array as $key => val)
It results in:
结果是:
Parse error: syntax error, unexpected ')', expecting '[' in...
If we follow the basic instructions, we can start at the ")" at the end of the line and then work backwar
ds and see that we forgot a $ sign in front of our "val" variable name. However, PHP gave a slightly-misleading error message because it thought that maybe you were trying to write some more advanced PHP code. It's rare that you get a misleading suggestion for the expected character, but it happens.
如果遵循基本说明,则可以从⾏末的“)”开始,然后向后⼯作,然后看到我们忘记了在“ val”变量名前⾯的$符号。 但是,PHP给出了⼀个稍微令⼈误解的错误消息,因为它认为您可能正在尝试编写⼀些更⾼级PHP代码。 很少有⼈会误以为是预期字符,但确实如此。
Let's look at one more common variation of the error:
让我们看⼀下错误的另⼀种常见变化:
Parse error: syntax error, unexpected end of file in your_script_file.php on line 4
If we go look at line 4, it's just the final line in the script - there's no code! But in almost every case, when you get this error, it's because you have an opening "{" curly brace that doesn't end, like this:
如果我们看⼀下第4⾏,那只是脚本中的最后⼀⾏-没有代码! 但是⼏乎在每种情况下,当您遇到此错误时,这是因为您有⼀个没有结束
的“ {”花括号,例如:
foreach($my_array as $key => $val)
{
Alternatively, if we see this error:
或者,如果我们看到此错误:
Parse error: syntax error, unexpected '}', expecting end of
...it almost always means we have too many closing } curly braces, like this:
...⼏乎总是意味着我们有太多的}花括号,例如:
foreach($my_array as $key => $val)
{
}}
Just remember, start by finding the unexpected value, and then work backwards and you'll find the problem every time.
请记住,从到意外的值开始,然后向后⼯作,每次都会发现问题。
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM 解析错误:语法错误,意外T_PAAMAYIM_NEKUDOTAYIM This is one of the most bizarre error messages because it's the Hebrew word for "double colon" (according to what I read once upon a time). Essentially, there's a variety of ways that this error can pop up, but it's usually due to a missing character of some kind. Think of it as another "unexpected character" type of error, because it is usually due to PHP seeing some code that could potentially be valid if the next part of the code was "::" (followed by something else).
这是最奇怪的错误消息之⼀,因为它是希伯来语中的“双冒号”(根据我曾经读过的内容)。 本质上,可以通过多种⽅式弹出此错误,但这通常是由于某种字符丢失所致。 可以将其视为另⼀种“意外字符”错误,因为它通常是由于PHP看到某些代码,如果代码的下⼀部分
为“ ::”,则可能是有效的(后⾯接其他内容)。
More often than not, the scope resolution (the "::" you might use for something like referencing a static class property, like MyClass:$some_property) is actually not really what you were going for, and chances are that you just forgot a $ sign or you didn't close a curly brace around a variable embedded in a string. For example:
实际上,范围解析(可能会⽤于引⽤静态类属性(例如MyClass:$ some_property之类的东西)的“ ::”)实际上并不是您想要的,并且很可能您只是忘记了$符号,或者您没有关闭嵌⼊在字符串中的变量周围的花括号。 例如:
$x = "hello";
echo "{$x world!";
You can see that the valid, expected code should have been echo "{$x} world!", but that the } character was missing. Again, fix this how you would fix the other errors about unexpected characters - find the line number and start working backwards from the end of that line.
您可以看到有效的预期代码应该是echo“ {$ x } world!”,但是缺少}字符。 再次,解决此问题,如何解决与意外字符有关的其他错误-到⾏号并从该⾏的末尾开始向后⼯作。
Let's move onto some other
让我们继续探讨其他⼀些常见错误...
THE UNDEFINED ERRORS
未定义的错误
These are all pretty straightforward errors, so this will be a quick section.
这些都是⾮常简单的错误,因此这是⼀个快速的部分。
Notice: Undefined variable: my_variable_ 注意:未定义的变量:my_variable_name在...
This is an easy one. It simply means you tried to use a variable that doesn't exist. For example:
这是个简单的。 这只是意味着您尝试使⽤不存在的变量。 例如:
$My_variable_name = "Hello world!";
echo $my_variable_name;
Notice that the variable names do not match - the first line has an upper-case "M" in the variable name "My" instead of "my". So the first line is creating one variable and the second line is echo-ing a completely different variable which just happens to have a SIMILAR name. Usually, when you have this error, it's because you have a typo in one of your variable names, or maybe you're incorrectly assuming that a bit of code has been run that defines a variable.
请注意,变量名称不匹配-第⼀⾏在变量名称“ My”中使⽤⼤写字母“ M”,⽽不是“ my”。 因此,第⼀⾏正在创建⼀个变量,第⼆⾏正在回显⼀个完全不同的变量,该变量恰好具有相似的名称。 通常,当您遇到此错误时,这是因为您的⼀个变量名中有⼀个错字,或者您可能错误地假设已经运⾏了⼀些定义变量的代码。
error parse newNotice: Undefined index: my_index 注意:未定义的索引:my_index
This is a similar issue to the undefined variable error, with similar causes. Let's take a look at a code example:
这是与未定义的变量错误类似的问题,原因类似。 让我们看⼀个代码⽰例:
$example_array = array(
"hello" => "world",
"How" => "are you?"
);
echo $example_array["how"];
Again, we see a case-sensitive problem - the echo is trying access an array index called "how", but our array uses "How" with an uppercase "H", so the line with echo will fail and display that notice. If you ever get this error and you are CONVINCED that the index exists, then you can use print_r() to dump the contents of the array, which can often lead to understanding the reason.
再次,我们看到⼀个区分⼤⼩写的问题-echo正在尝试访问名为“ how”的数组索引,但是我们的数组使⽤“ How”和⼤写的“ H”,因此带有echo的⾏将失败并显⽰该通知。 如果您遇到此错误,并且确信该索引存在,则可以使⽤print_r()转储数组的内容,这通常会导致理解原因。
Fatal error: Call to undefined function XYZ 致命错误:调⽤未定义函数XYZ
Usually, this happens because you've stored a function in some file and it hasn't been included, or yo
u were removing some code but didn't clean it all up completely, or maybe you have a typo in your function name. No matter what, it's because
you're calling a function that doesn't exist. Example:
通常,发⽣这种情况是因为您已将函数存储在某个⽂件中,但尚未包含该函数,或者您正在删除⼀些代码但没有完全清除所有代码,或者您的函数名中有错字。 ⽆论如何,这是因为您正在调⽤⼀个不存在的函数。 例:
function do_my_bidding()
{
}
domybidding(); // Throws the error because we're missing the _ characters in the name.
Notice: Use of undefined constant XYZ - assumed 'XYZ' 注意:使⽤未定义的常数XYZ-假定为“ XYZ”
This is almost always the result of a missing $ in front of a variable. For example:
这⼏乎总是导致变量前⾯缺少$的结果。 例如:
$hello = "world";
echo hello; // Notice: Use of undefined constant hello - assumed 'hello' in...

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。