![]() |
TIP: Quotes / Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in..
Many new PHP coders will come across this error and will be a bit confused by what it means so I thought I would supply a quick topic to cover the issue in more detail.
Lets suppose we have the following code: PHP Code:
First there seems to be some confusion about the quotation marks so I will go into those here. Single ' quotes are handled by PHP literally. That means what you see inside the ' marks is exactly what you get. Example: PHP Code:
Quote:
Double " quotes are where our magic happens. Double quotes tell PHP to examine the string inside it and if there are any variables found, replace them with their values. There is however a trade-off: PHP checks double quoted strings for variables it needs to replace - this uses more CPU cycles. Example: PHP Code:
Quote:
When you need to use the single ' or double " inside a single quoted string you need to tell PHP to ignore it. The way this is done is to escape the quote within the string. To escape a character we use the \ slash. Example: PHP Code:
Quote:
Now what about using the \ within a string? In single quoted strings you don't need to worry. Sometimes in double quoted strings you may need to escape it. PHP Code:
Quote:
So, lets return to the SQL above: PHP Code:
Quote:
If you look at the SQL after the word VALUES it becomes clear that there is some complex quoting going on: Code:
//We're already inside double quotes at the beginning and end of the SQL.So, what do we do with this? Well there are 3 ways really: PHP Code:
PHP Code:
There may also be times when you wish to use functions or PHP commands in conjunction with your quoted text. The general thing you must realise is that a string (thats quoted text) is just a string of text. It is seen by PHP as just that - text. PHP will check double quoted strings for variables and replace them but it will not run PHP commands or functions inside a string. You should realise that single quoted strings will not to anything magical so the following example uses double quoted strings. Example: PHP Code:
|
Quote:
PHP Code:
|
Quote:
I find the easiest and quickest way to check what the actual query about to be run is, is to echo it to the browser and syntax errors should then be fairly obvious. For more complex queries a good practice imo is to first get the query working in the SQL GUI of your choice and then transfer the query to your server side script. Debugging an sql statement in an sql gui is usually a lot easier than trying to debug it in the server side script. |
Quote:
|
Quote:
It was rather late at night when I created this tip for the noobs so it's not perfect and the SQL was taken from another topic here as it looked like a good example to demonstrate with. Obviously in hindsight I may have got that wrong but hey ho I tried! |
Quote:
I wasn't trying to suggest anything you posted was wrong. You made very valid points which should help at least noobies. The purpose of my post was basically to add to your tips but it may not have come across as so :(. |
No, no, you got it right, it was a bit daft of me in hindsight to use SQL as an example but I found it in another topic and it looked perfect for a tip topic! In hindsight SQL, strings and quotes are entirely different but there we go, thats the kind of silly things i do at 1am!
When I first started learning php I never had access to a reliable connection so I had to learn a lot the hard way using localhost and a local copy of the php manual. I had to figure out a lot of stuff myself with no-one to ask (you know, the silly things that take hours to figure out like the IE bug in my sig) so I figured these tip topics could be pretty useful in helping others and answering those little silly things that aren't always immediately obvious. |
Good.
Added to the FAQ thread. |
Thanks for your concise explanation.
Seems I was wrong but that is nothing new....was just trying to be helpful....so thanks for pointing me in the right direction. Regards Trevor |
You accidentally threw an extra double quote in on your last example.
PHP Code:
PHP Code:
|
Yes I'm aware of that but unfortunately I can't edit the post. The forum locks it after a few minutes.
It's not the only one, if you look closely there is also a few ' marks too but I did submit the article at 1:56am ;) |
Fixed.
|
Thanks Inigoesdr, Any chance you could fix these two as well? - They're quotes:
Comment written by $Author' Comment written by Joe Bloggs' Both have an ' at the end. Sorry about that, no idea how I missed those when I previewed so many times :confused: |
Quote:
|
Thanks Fou ;)
|
| All times are GMT +1. The time now is 06:19 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.