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
Related
This question already has an answer here:
Difference in accessing arrays in PHP 5.3 and 5.4 or some configuration mismatch?
(1 answer)
Closed 6 years ago.
I a, trying output photo, but can't.
when i write this
echo "<p>Name: <b>".$rows['photo']."</b></p>";
it say:
img/car.jpg
but i need a image, so I write
echo '<p><img src=img/$rows['photo']/></b></p>';
it say
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';'
Try this:
echo "<p><img src=img/$rows['photo']/></b></p>";
or
echo '<p><img src=img/'.$rows['photo'].'/></b></p>';
This is correct string manipulation to get what you want:
echo "<p><img src=\"{$rows['photo']}\" /></p>";
Use "" as string wrapper to inject variables using curly braces notation.
Also if you have $rows['photo'] = img/car.jpg you will not need extra img/ in src attribute.
And i removed </b> tag, since there was no opening tag for it.
if you want to display the image fetch the image in the separate file and use that file path in the img tag
<img src="fetch_image.php?id='.$id.'"/>
$id is a primary key value
The fail is caused by the bad syntax. If we can assume that $rows['photo'] holds a filename within /img directory then you should use it like this:
double quotes here and at the end
↓
echo "<p><img src='img/{$rows['photo']}'/></b></p>";
↑ ↑
curly braces to distinguish variables in a string
Here's a manual that you need to read in order to understand how it works http://php.net/manual/en/language.types.string.php
And last but not least - you have a possible XSS attack vector here because you don't escape value of $rows['photo']. That means that if a value of photo can be changed by user then user could put, for example this string: image.jpg' onclick='window.location="my.fishing.website.com"; - that is being the most simple form of this kind of attack (he could also steal cookie values or any other sensitive data).
To mitigate this kind of security hole you need to escape values, which can be influenced by user in any way, with htmlspecialchars() function. Or even better use templating engine that would it for you: http://www.phptherightway.com/#templating
I'm trying to add wikiplayer to my SMF Forum but I'm having a little trouble.. I'm trying to put this in my index.template.php:
<!-- Wikplayer http://www.wikplayer.com -->
<script type="text/javascript" src="http://www.wikplayer.com/code.js"
data-config="{'skin':'skins/wikfull/plainBlack/skin.css','volume':50,'autoplay':true,'shuffle':true,'repeat':1,'showcomment':false,'marqueetexton':true,'placement':'bottom','showplaylist':false,'playlist':[{'title':'It%27s%20The%20Most%20Wonderful%20Time%20Of%20The%20Year','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DgFtb3EtjEic'},{'title':'All%20I%20Want%20For%20Christmas%20Is%20You','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DyXQViqx6GMY'},{'title':'Let%20It%20Snow','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DmN7LW0Y00kE'},{'title':'Jingle%20Bell%20Rock','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DitcMLwMEeMQ%23t%3D36'},{'title':'Driving%20Home%20For%20Christmas','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DczhZbqpyBm8%23t%3D27'},{'title':'Merry%20Christmas%20Everybody','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D0A8KT365wlA'},{'title':'Winter%20Wonderland','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DSHE0Jo82_34%23t%3D56'},{'title':'Santa%20Baby','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dg7T0IK99ELs'},{'title':'It%27s%20Beginning%20To%20Look%20Alot%20Like%20Christmas','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DcHh7pW29Fy8'},{'title':'Christmas','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D5aw2yuYAKoE'},{'title':'Jingle%20Bells','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DVKtOBLvPs5Q'},{'title':'Run%20Run%20Rudolph','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dl849CulLhGQ'},{'title':'Baby%20It%27s%20Cold%20Outside','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D2Rhvtzzmq-Y'},{'title':'Rocking%20Around%20The%20Christmas%20Tree','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DbnIqLlBwzrc%23t%3D15'},{'title':'Silent%20Night','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DPGjByOI9Iqw'},{'title':'Holly%20Jolly%20Christmas','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DKjiMMDZoKmY'},{'title':'Have%20Yourself%20A%20Merry%20Little%20Christmas','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DnZ6yQgBvuoI'},{'title':'Santa%20Claus%20Is%20Coming%20To%20Town','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DSnA52s7qceM'},{'title':'Last%20Christmas','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DE8gmARGvPlI'},{'title':'Merry%20Christmas%20Everyone','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D5PkUWkxGWj0'},{'title':'Shake%20Up%20Christmas','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DJ-8VCL4uSUc'},{'title':'Deck%20The%20Halls','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D3-ykZUb3AEE'},{'title':'Carol%20Of%20The%20Bells%20(Pentatonix)','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DVOGByYObUqE'},{'title':'Rudolph%20The%20Red%20Nose%20Reindeer','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D0byH9h1ClBY'},{'title':'The%20Christmas%20Song','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DSOszvL9lgSs'},{'title':'I%20Wish%20It%20Could%20Be%20Christmas%20Everyday','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DZoxQ4Ul_DME'},{'title':'Do%20They%20Know%20It%27s%20Christmas','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D1AcD7hho9fs'},{'title':'Feliz%20Navidad','url':'http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DQ6ziiLE92Zg'}]}" ></script>
<!-- Wikplayer code end -->
But I'm getting this parse error all the time:
Template Parse Error!
There was a problem loading the /Themes/zone99/index.template.php template or language file. Please check the syntax and try again - remember, single quotes (') often have to be escaped with a slash (\). To see more specific error information from PHP, try accessing the file directly.
You may want to try to refresh this page or use the default theme.
syntax error, unexpected T_STRING, expecting ',' or ';'
If I add this code outside the ?> right at the end it will work but then all the forum fonts seem bigger, too big... Can anyone guide me in simple terms to solve this problem? Thank you!
Also I will include my index.template.php # https://www.mediafire.com/?vj0jpbn7cae9of0
As you are getting parse error. That means you are missing colon ; or apostrophe ' somewhere. look for these if the problem persists try writing the script inside an echo statement.
echo '<script> your script here</script>'
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 "
example link
returns PHP error:
syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';'
Any ideas? (Hope this question makes sense. I'm trying to fix someone else's code.)
Check that there's not a "<?" or "<?php" at the beginning of the file. This code assumes you're not already within a PHP block. If you are, then you'll have to adjust (remove it). Or, you'll need to add close ?> somewhere before this html, and a <?php after it...
Sorry about that guys, the problem was <?php echo val;?> instead of <?php echo $val;?>. Sometimes the eyes betray the mind...
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.