Jul 20, 2008

Need Help With Php Echo

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > PHP Programming

free web hosting

Need Help With Php Echo

bondoq
hi all

i am new in php and mysql programing ,so i want help to check this code i got this error
QUOTE
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';'

the code is :
CODE
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

 

 

 


Reply

ewcreators
There are some errors in your echo :
CODE
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 :
CODE
<?php
//connection
echo "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.

CODE
<?php
//connection

echo "Hello, my website is <a href='link.filetype'>";
?>

See, i used single quotes.
--
Your correct code now, would be :

CODE
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.

 

 

 


Reply

shadowx
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



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : php echo

  1. Complex Php Echo Statement Assistance - Can't quite get this one, help appreciated (3)
  2. Question For The If And Echo Function - (2)
    I'm not that good with PHP, and I tried this code: CODE if (
    $_SERVER['REQUEST_URI'] == ('/') )/*'/' is the
    domain root*/ { echo('<img src="{I_ONEURL}" border="0"
    alt="{T_SOMETHING}" />'); } else { echo('<img
    src="{I_ANOTHERURL}" border="0" alt="{T_SOMETHING}" />'); }
    However, it doesn't work. So, basically, I want that if the request is at the root (actually
    mysubdomain.domain.com), it will show {I_ONEURL...
  3. Simple Shoutbox? - I only know the echo part of php... (19)
    Well, does anyone have a nice simple shoutbox php code? It's just I've been looking for one
    for a while but they all seems to have millions of files attached with them and everything. Just
    wondering if theres anything simpler out there?...
  4. I Will Provide You With A Php Code That.. - ..enable you to use the 'echo' command. (0)
    **YOU DO NOT NEED ANY HTML TAGS FOR THIS SCRIPT**** Enter all values between the ' ' quotes
    //Define The Variables $cfg = array('sitename' => '', //Your Sites Name
    'footer' => '', //OPTIONAL, if you want your footer stored so that the
    echo command can display it. Ex: Copright ©2005 - Michael Fratello (the © is will turn
    into the copyright 'c') 'board_dir' => '', //Link to your
    message boards. (If you have any) ); ?> I recommend saving this file ...



Looking for php, echo

Searching Video's for php, echo
advertisement



Need Help With Php Echo



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE