Hello and welcome. This is more of an explanation to video.
I will go through some of the common errors you might encounter when you are programming in PHP.
Most of them are self explanatory.
I would say a good 50% of errors that you encounter are when you either don't see something you have accidentally typed or you have missed out something.
Everyone makes such mistakes - missing either a semicolon or adding an extra bracket or something like that.
Now I have created a few pages here. These are some of the errors you are likely to encounter.
There are more. This list isn’t exhaustive. This will just cover some of the basics.
I have got a context editor ready to take you through each error one by one.
So the first one I am going to explain is this "html".
I used a lot of html embedded in our "echo" command here.
If I try to run this page here, we get this error here.
It says "Parse error" and we get this message here.
You really need to look for these - expecting either a comma or a semicolon.
It gives us a line number. So this will always return a line no. when we get one of these parse errors.
It says here line 5.
So if we come down to line 5; you can see here that is line 5 column 19 (Ln5, Col19); we get all the information.
Line 5 is here.
Now there isn’t anything visually wrong with this.
The way php interprets a command like "echo" is that we get the starting point, our double quotes here and our ending point here. So this will be the start and this will be our end.
Now what’s really happening here is that because we are embedding the html, we are using double quotes in between and this means that the echo command would be read as starting here and ending here.
I think I have explained this in my echo function tutorial.
Okay so the reason that we are getting an error in this line is because this is the first occurrence of a double quotes where it shouldn’t be.
Now technically, php hasn’t picked this up, so this shouldn’t be here.
But what it was saying about expecting a semicolon was that when we end an echo, we use a semicolon. So what it was looking for is that there.
But then even after this, it becomes absolute rubbish.
So what we need to do is use our single quotation marks instead.
Now if I were to save that, we will now get an error on line 6 because the error has come down to it, that changes to 6.
See you know you need to change something on line 6 or nearer line 6. You’ll see some of the other ones we will be doing, some don’t return the actual line error.
And there you go. So you have got that after running our html code. There’s obviously no functionality. But we have covered that.
Okay so the next one I'm going to do is the semicolon. This is one other common error.
We go back here and click on semicolon. We have got a parse error here "expecting a semicolon".
Now why are we expecting a semicolon? This code looks okay. We have got a variable here with "Alex". We have another variable here with "Alex".
We are comparing these variables. We need double equals in there.
If this condition is true, we will echo this message.
Now if we go here - line no. 9.
So obviously, this is a very simple code. If it were a bit more complex, you came to line 9, but this is in fact line 9.
Now looking at that, there is nothing wrong with line 9.
But in a way, there is something wrong with that line. What we have is the way php interprets the pages; it’s on a single line basis.
So the code we see here is the same as that.
That would actually compile and work but because this is down here; and this is the same as up here; we are still expecting a semicolon after that.
If we do this, this is still a valid code. So I have added a semicolon there, although to the human eye visually it should be there.
We can bring it down here. So let me just re-run this code.
There you go. So we get a successful page.
Obviously if I were to put that up here, that would also be a valid code, as would that.
So php doesn’t work on the basis that "there’s an error on this line".
It usually says the current line can’t be run may be because of an error on previous line.
So because of this error on this line without the semicolon, this line cannot run. Therefore, the line has been returned as an error on line 9. You can see that there.
So that’s two basic errors that we have covered.
If you ever get things like that, just check and don’t necessarily check the line that the error has been returned on.
Check before, check after. No, not after but check before and see if you can fix anything.
Just scan through every single character if you have to.
I get a lot of emails from people who have made mistakes like that and I don’t mind helping people.
Feel free to ask, but make sure you check your work once, twice or even thrice before you send me anything.
Okay. So in the next parts, we will cover the rest of the error pages. See you soon. This is Evan Varkey dubbing for the Spoken Tutorial project. (Script contributed by Antara Atrey)