Am echoing php variables which works fine but when i tried to output image, nothing seems to work
working.php
echo ("addMarker($lat, $lon,'<b>$name</b>$address<br><br>$desc');\n");
not_working.php
for image display, i added
<img src='http://localhost/services/status/" .$pic. "'>
hence
echo ("addMarker($lat, $lon,<img src='http://localhost/services/status/" .$pic. "'>,'<b>$name</b>$pic<br><br>$desc');\n");
Any Help
The php documentation about strings should clarify your issue, i hope. In simple words, variables are not expanded (parsed) in single quotes.
Best solution is to use sprintf:
sprintf('<img src="http://localhost/services/status/%s">', $pic);
OK solution:
echo '<img src="http://localhost/services/status/' . $pic . '">'
Not so ok solution:
echo "<img src=\"http://localhost/services/status/$pic\">"
Related
How do I include a php tag in an HTML attribute? I still find it quite tricky.
This is the HTML attribute:
src="https://customer.site.com/?language=en_US&portal=Default"
The thing is, ?language should accept a dynamic value. That value is stored in $lingos[$wmpl_langcode].
So I've been trying many variations and I'm still stuck.
I've got this now but it doesn't seem right.
src=<?php echo "https://customer.site.com/?language=" . $lingos[$wmpl_langcode] . "&portal=Default" ?>"
I don't want to waste any more time on it. Any tips would be great.
you was just missing one double quote and the ; at the end of the echo to end the instruction:
src="https://customer.site.com/?language=<?php echo $lingos[$wmpl_langcode]; ?>&portal=Default"
You are now missing quotes for the attribute.
This however would be perfectly fine:
src="<?php echo "https://customer.site.com/?language=" . $lingos[$wmpl_langcode] . "&portal=Default" ?>"
If you like it a bit more clean without confusing quotes:
<img src="https://customer.site.com/?language=<?php echo $lingos[$wmpl_langcode] ?>&portal=Default"/>
You should also think about urlencode():
<img src="https://customer.site.com/?language=<?php echo urlencode($lingos[$wmpl_langcode]) ?>&portal=Default"/>
To make it "more clean", you should use a templating engine.
When you directly use echo to print the dynamic link, the quotes are not present there.
Try this:
src="https://customer.site.com/?language=<?=$lingos[$wmpl_langcode] ?>&portal=Default"
OR
src="<?php echo "https://customer.site.com/?language=" . $lingos[$wmpl_langcode] . "&portal=Default" ?>"
I am using php and conditional code to give a dynamic url to a photo. The result should read as http://example.com/biophotos/1.jpg. But instead I am getting
http://example.com/%22http://example.com/biophotos/1.jpg%22
How can I force it to just give the one url and without the %22 space on the end?
if ($emresult[0]['photo'] = "y") {
echo '<img class=\"alignright\" src=\"http://example.com/biophotos/' .
$theID . '.jpg" width=\"150\" height=\"150\">';
}
else {
echo 'There is no author photo.';
}
There is no reason in escaping double quotes when your using single quotes.
echo '<img class="alignright" src="http://example.com/biophotos/' . $theID . '.jpg" width="150" height="150" />';
As much as I agree with #slik, you also might want to look into %22 (double quotes) added to url out of nowhere
Check if magic quotes is on in your php.ini file. You can look into html_entity_encode() to encode the %22 as a slash.
I have this php line which works fine:
echo "<p>" . $post['message']. "</p>";
But I want to change it so it will link to my page (not to a single post). So it should look like that.
echo "<p>" . $post['message']. "</p>";
I have tried a lot many proposition gathered on different website, but each time I am getting an error.
Any idea ?
Thanks a lot!
Using single and double quotes, you avoid escaping issues. Try this:
echo '<p>'. $post['message']. '</p>';
i see that you didn't escaped from double quote that closes href attribute:
echo "<p><a href=\"https://www.facebook.com/rscmovement\" target=\"_blank\">"
I guess You have missed the back slash () before " after www.facebook.com/rscmovement.
"https://www.facebook.com/rscmovement\" "\"target=\"_blank\">" will
Oh boy! I cant get this to work. Any ideas on what the heck I'm doing wrong? Here's the code.
I'm trying to echo the script but use a php function to get the directory of the js file!!
Any help would be appreicated!!
echo '<script src="<?php get_some_function();?> . /js/main.js"></script>';
I've tried dif scenerios with escaping but cant get this to output correctly.
Since you're already in the PHP context, you can simply concatenate the strings, like so:
echo '<script src="' . get_some_function() . '/js/main.js"></script>';
Using sprintf() looks more cleaner, though:
echo sprintf('<script src="%s/js/main.js"></script>', get_some_function());
Instead of opening another script tag inside the string, concat the string and echo. The <?php within your string will not be evaluated.
echo '<script src="'. get_some_function() . '/js/main.js"></script>';
Simple string concatenation:
echo '<script src="' . get_some_function() . '/js/main.js"></script>';
Don't forget to properly escape the output of your function!
try doing this:
echo '<script src="'.get_some_function().' /js/main.js"></script>';
or this:
$value = get_some_function();
echo '<script src="'.$value.' /js/main.js"></script>';
Remember that any variable echoed in single quotes ( ' ' ), the value of that variable will be not printed, and if a variable is echoed in double quotes ( " " ) it will be printed.
Similar is true for returned data from a function stored in a varaible. If you are using single quotes, then every php code (variable, or a method call of a class) should be concatenated using dot operator ( . , :P ) . If you are using double quotes, then no need to use . .
Like in all above answers, they have used . to append the php function call, your code may be fine as below also (not tested by me, so you will need to do adjustment) :
$src = get_some_function();
echo "<script src=$src/js/main.js></script>";
But please note that it is a best practice to use single quotes for any kind of html etc echoed in php code, because HTML attributes are using double quotes.
Hope this will help...
I am trying to create a code which obtains uploaded images, stores them and also displays the image preview and confirms that the image was successfully uploaded.
<?php
$name=$_FILES['myfile']['name'];
$tmp=$_FILES['myfile']['tmp_name'];
$error=$_FILES['myfile']['error'];
$path='myweb/';
if(move_uploaded_file($tmp,$path.$name)==1){echo 'success';}else{echo $error;};
echo ('<img src="$path.$name" height="100px" width="100px"/>');
<?php
The problem is that images are not displaying.
I have also tried
echo ('<img src="$path$name" height="100px" width="100px"/>');
but it still doesn't work.
How can I get the images to display?
problem was with single and double quotes.
echo '<img src="' . $path.$name . '" height="100px" width="100px"/>';
You have used single quote and because of that it was not taking the variable name.
You have to include the variable in the printed string like this:
echo ('<img src="'.$path.$name.'" height="100px" width="100px"/>');
You can read more about it in the documentation.
If you don't do so, PHP will think that you want to print the text $path.$name instead the variables content.
<img src="<?php echo $path,$name; ?>" height="100px" width="100px"/>
OR
echo '<img src="' . $path.$name . '" height="100px" width="100px"/>';
There is a difference between using double "" and single '' quotes.
Double quotes are getting parsed, which means that
$variable = 10;
echo "$variable";
will output:
10
Single quotes don't get parsed:
$variable = 10;
echo '$variable';
will output:
$variable
you use variables within '' which means they don't get parsed.
i replaced line 13 in above code like so.
echo (" <img src=$path.$name height=100px width=100px/>");
And its working now.
Seems like double quotes were the problem here.
I'l read m0re about it.
Thank u s0 much guys u all've been very helpful.:)