I can't really find good guidelines through Google searches of the proper way to escape variables in URLs. Basically I am printing out a bunch of results from a MySQL query in a table, and I want one of the entries in each row to be a link to that result's page. I think this is easy, that I'm just missing a apostrophe or backslash somewhere, but I can't figure it out. Here's the line that's causing the error:
echo " Who Owns It? ";
and this is the error I'm getting:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
How do I fix this error?
In addition, what are some general guidelines for working with echo and variables in URLs?
echo " Who Owns It? ";
There are two things here that should be changed. For one thing there shouldn't be a / after movies.php. The second is that there aren't any apostrophies around url variables. It should be movie_id=$row['movie_id']. Whenever I use a php variable I usually concatonate it instead of embed it in the quotations. So in the end I'd do something like this:
echo " Who Owns It? ";
The $row['movie_id'] inside the double quoted string is not allowed (especially the single quotes). Either write it without ' or use the curly braces syntax:
echo " Who Owns It? ";
echo " Who Owns It? ";
See variable parsing for further information.
This is a better way:
$movie_id = urlencode($row["movie_id"]);
echo ' Who Owns It? ';
Good luck!
This is a better(er) way:
$movie_id = urlencode($row["movie_id"]);
echo " Who Owns It? ";
Easier to read. Besides the single and double quote speed thing is not much of an issue any more.
Echo or URLs have nothing to do with your problem. It's PHP strings syntax.
Related
UPDATE !!!
It seems that the website I used to test my code has issues and therefore I had problem with executing my thing. I still leave this post up, so someone else, with similar problem can learn something.
website to avoid: http://phptester.net
ORIGINAL POST !!!
I want to add a string to my variable that uses a double qouted word. Is that possible? I only get error.
FATAL ERROR syntax error, unexpected 'Old' (T_STRING) on line number 4
Here is the code:
<?php
$var = "name: ";
$biography = "\n I'm Robert and I came from \"Old\" Europe.";
echo $biography;
?>
I was checking it on this website:
http://phptester.net
It must be smg to do with the new line character in the beginning, or perhaps its not even possible to add double qouted words to a variable. I am not sure.
Your help is greatly appreciated !
I'm trying to redirect to another php page from sql server fetch array with WHILE loop.
All records are fetched well, but i want to add to the table "delete" link to every fetched record.
I tried the code below separatedly and it works just fine.
The problem is because the code is inside PHP tag and i'm confused with all of the ' ' signs.
while compailing the code below i just can't see nothing (without the "delete" line everything is OK):
while ( $record = sqlsrv_fetch_array($rs, SQLSRV_FETCH_NUMERIC) )
{
$o .= '<tr><td><center>'.$record [0].'</center></td><td><center>'.$record [1].'</center></td>
<td><center>'.$record [2].'</center></td><td><center>'.$record [3].'</center></td>
<td><center>'.$record [4].'</center></td><td><center>'.$record [5].'</center></td>
<td><center>'.$record [6].'</center></td><td><center>'.$record [7].'</center></td>
<td>**<a href="JavaScript:if(confirm('delete record?')==true)
{window.location='phpSQLServerDeleteRecord.php?EventID=<?php echo $record[0];';}">delete</a>**
</tr>';
}
$o .= '</tbody></table>';
echo $o;
Thanks a lot for your help!
The use of single-quotes is creating a syntax error:
'</center></td><td>**<a href="JavaScript:if(confirm('delete record?')==true)
{window.location='phpSQLServerDeleteRecord.php?EventID=<?php echo $record[0];';}">delete</a>**
</tr>'
Since the server-side string literal began with a single quote, as soon as the string contains a single-quote then the PHP engine will interpret that as the end of the string, which then results in a couple of syntax errors here as things intended to be part of the string are interpreted as code.
There are a couple of ways to go about this. One quick fix would be to "escape" the single-quotes which are meant to be part of the string rather than a boundary of the string:
'</center></td><td>**<a href="JavaScript:if(confirm(\'delete record?\')==true)
{window.location=\'phpSQLServerDeleteRecord.php?EventID=<?php echo $record[0];\';}">delete</a>**
</tr>'
Of course, there's still the error that now PHP code (the echo statement) is being emitted as part of a string. You can terminate the string boundaries around that and concatenate the value to the string like any other string concatenation:
'</center></td><td>**<a href="JavaScript:if(confirm(\'delete record?\')==true)
{window.location=\'phpSQLServerDeleteRecord.php?EventID=' . $record[0] . '\';}">delete</a>**
</tr>'
(Note that immediately after the reference to $record[0] there are two single-quotes. One to begin the new string literal and then an escaped one to be the first character in that string literal, since the client-side code will need that single-quote character to terminate its string literal. That's probably the root of your confusion in the matter... These server-side strings aren't just HTML, they're also JavaScript which has its own client-side strings.)
You'll also want to URL-encode that value being emitted as part of a query string (even though it should just be a numeric identifier I suspect, it's still good form):
'</center></td><td>**<a href="JavaScript:if(confirm(\'delete record?\')==true)
{window.location=\'phpSQLServerDeleteRecord.php?EventID=' . urlencode($record[0]) . '\';}">delete</a>**
</tr>'
There are a number of other ways to potentially clean this up. Take a look at the PHP documentation on strings, particularly around the "heredoc" syntax. My PHP is to rusty to whip up an example of that, but I suspect it will end up looking a little cleaner in the resulting code. Either way, it's good to get some practice in the differences between the various ways PHP handle string literals.
I will echo the comment from #mario above but give you an example of how to use Heredoc syntax to make large blocs of text much more readable and easy to work with, as you will not need to mess with escaping quotes at all.
while (...)
{
$o .= <<<EOT
<tr>
<td><center>{$record[0]}</center></td>
<td><center>{$record[1]}</center></td>
<td><center>{$record[2]}</center></td>
<td><center>{$record[3]}</center></td>
<td><center>{$record[4]}</center></td>
<td><center>{$record[5]}</center></td>
<td><center>{$record[6]}</center></td>
<td><center>{$record[7]}</center></td>
<td>**<a href="JavaScript:if(confirm('delete record?')==true)
{window.location='phpSQLServerDeleteRecord.php?EventID={$record[0]}}">delete</a>**</td>
</tr>
EOT
}
Now doesn't that look a whole lot nicer?
Note: brackets around PHP variables are optional. I just like them when using Heredoc as I find it makes it easier to see where your variables are.
Read here for more information on Herdoc syntax:
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
For future readers, please look to answer by #David as well. He does good job of explaining why the OP was having his problem. I really look at this answer as complementary to that one, giving the reader an example of what might be a good way to go about solving the problem.
What Im trying to do is write a script that grabs the url of thumbnails attached to posts in wordpress. It sounds really easy(as I'm sure the solution is) but I can't seem to get it to work, I keep getting syntax errors no matter what I try. The problem line is the second echo(Img src...). Any help would be greatly appreciated.
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'archive-thumb');
$image_url = $image_url[0];
echo "<li class=\"recent-img-widget-li\"><a href='".get_permalink()."'>;
echo "<img src=\"".$image_url."\" width=\"120\" height=\"120\">";
echo "</a></li>";
Simply enough, you're not closing your first string after get_permalink(). Yo need another quote after the >.
You never close the first string. You just need a quote before the greater than on the first line (and possibly the second?). Look at the syntax highlighting that SO has.
A general guideline is to always look at the row above the one that is giving the error.
In this case you have forgotten to end the string in the last part of the first echo statement.
...ermalink()."'>;
Should be
...ermalink()."'>";
For one you should close that first echo. Missing the closing "
The following line is supposed to print a list item which is a variable picture that points to a variable link
echo '<li><a href='http://www.twitter.com/'".$person."'>
<img src ="'.$person.'.jpg'.'"/></a></li>';
I am getting the following error though
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';'
I am pretty sure I've screwed up the syntax in the a href part as I'm not quite sure how to handle the variables in the URLs...
Help is appreciated....Thanks!
Just use variable interpolation there.
echo "<li><a href='http://www.twitter.com/$person'><img src='$person.jpg'/></a></li>";
Why did you change the quote lol
echo "<li>
<a href='http://www.twitter.com/'".$person."'>
<img src ="'.$person.'.jpg'.'"/>
</a>
</li>";
Try using printf/sprintf; these two functions will make it a lot easier to deal with modifications to your code in the future as well as make it so that you're not "jumping in" and "jumping out" of quote sections a lot. It's a great way to avoid these kind of syntax problems with inline variables in double-quoted strings.
EDIT: The doc pages:
http://us3.php.net/manual/en/function.sprintf.php
http://us3.php.net/manual/en/function.printf.php
I seriously suck at getting the quotes and quotation marks correctly. is there a rule that would help me easily remember?
echo "<option value='".$data['recordc']."' . "selected=selected>" '". data['recordc'] ."' . "</option>";
You could easily fix the quotes problem with a decent editor that has syntax highlighting (even StackOverflow's basic highlighting quickly shows where you went wrong) but that is still a horrible mess to read.
Most of the time if you find yourself outputting too much HTML in an echo you are probably best served to do it in a cleaner way, like breaking out of PHP. I would do at the very least:
printf('<option value="%1$s" selected=selected>%1$s</option>', $data['recordc']);
In general you can use this instead:
echo "<option value=\"$data[recordc]\" selected=selected>$data[recordc]</option>";
which is a little easier to read. Array keys don't need to be quoted when used inside a string.
In this case, paolo's printf method is nice since the variable is reused