Seems right to me, i cant be bothered to test it but it looks right!
The reason why we have to use single or no quotes within double quotes is this. In the echo command we are telling PHP this:
QUOTE
PHP: echo "Hello, this is what i want to say";
English: Say what is in between the following quotes "Hello, this is what i want to say" END
So if i were to add some more quotes into the equation:
QUOTE
echo "Hello this "the internet" it is great";
What PHP understands is that it has to say what is in between the following quotes, and that is "Hello this " then it expects the ; as the end command but it doesnt get it, it gets some more random stuff that it doesnt understand and so it gives you an error.
I dont know if that makes sense but it does to me! It is the same with any PHP command that involves quotes, you cant use quotes inside them, because PHP basically reads from the first " to the second " and then stops, anything that comes after that last " that isnt a ; (end command) makes it confused.
One way of getting around it is to add a backslash (\) this is known as the escape character and essentially it tells PHP to just ignore whatever comes after it. For example:
CODE
echo "<a href=\"index.htm\">Home page</a>";
PHP will simply ignore the middle two " because they are escaped by \ Just remember it only works for one character, so if i had this (although theres no reason to have two double quotes like this):
CODE
echo "<a href=""index.htm""> Home page </a>";
I would need to add TWO slashes: \"\"
I think that's all except one thing, this error:
QUOTE
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';'
Whenever you get this error (i get it a lot because i dont realize i accidentally deleted a quote or something) always check for double quotes and more importantly take a look at the part in bold. That illustrates what i mean about PHP expecting to hear the END command ; Im always leaving that out too! If you need some tutorials check out Tizag.com brilliant tutorials there from beginner to mid level.
Sorry this post is so long!
Reply