HOME       >>       Programming

Need Help With Php Echo


hi all


i am new in php and mysql programing ,so i want help to check this code i got this error

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';'

the code is :

echo "<tr><td>" '<a href="login.php?sid='.$d['spot_id'].'" </td><td> ".$d['name']."</a> <br> . "</td></tr>";

Notice from rvalkass:

Do not revert edits made by staff members.

Moved from Tutorials to PHP Programming, and Quote and Code tags added where needed. Topic title changed from "Need Help". Please check out the following links:

Readme ~ Rules ~ AUP


There are some errors in your echo :

echo "<tr><td>" [b]'<a href="login.php?sid='.$d['spot_id'].'" </td><td> ".$d['name']."</a> <br> [/b]. "</td></tr>";
Number of mistakes :
1.You didn't close the <a href tag.
2.You used quotes INSIDE a link. You shouldn't use '. inside, and also, you have a double-quoted echo ( echo " "), and in the sid=, you used '. instead of ". , even though its still wrong.
There are a few more, but lets look at some things here :
Before i tell you the correct code, you can use double quotes in a php echo which starts and ends with already existing double quotes.
Meaning, in echo, whatever you want to embed, should be either in single quotes , or no quotes(for variables only and for extra things).
Example :
<?php//connectionecho "Hi, my name is $name, and my friend's name in the database is $friend[from_db]";?>
^^this method is called Concatenation
Using html stuff in php echos.

<?php//connectionecho "Hello, my website is <a href='link.filetype'>";?>
See, i used single quotes.
Your correct code now, would be :

echo "<tr> <td> <a href='login.php?sid=$d[spot_id]'> </td> <td> $d[name] </a> <br /> </td></tr>";

Someone correct me if im wrong anywhere, just had a hard time reading his code.
Anyways,heres your anwser.


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:


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:


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:


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):


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:

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!


Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.