I am aware that I cannot use an echo inside an echo. My php code is:
echo '<img src="img/image1.jpg"
I want to use php variable as the source. Somethink like:
echo '<img src="php-code"
Using .(dot) you can concatenate php variable in echo statement.
echo '<img src="'.$src.'" />';
You have four options:
$url = '...';
//1
echo '<img src="' . $url . '">';
//2
echo "<img src='{$url}'>"; //Note that you have to use double quotes
//3
echo '<img src="';
echo $url;
echo '">';
//4
echo '<img src="', $url, '">'; //I would not recommend this one though
just skip first echo and write the html with an echo in the middle
<img src="<?=$src?>">
All of these works
echo '<img src="', $url, '">'; # This works by sending 3 different parameters to echo
echo '<img src="' . $url . '">'; # This works by concatenating 3 strings before echoing
echo '<img src="'; echo $url; echo '">'; # This works by echoing 3 strings in turn
echo "<img src=\"$url\">"; # All of these works by inserting
echo "<img src=\"${url}\">"; # the value of $url in the string
echo "<img src=\"{$url}\">"; # before echoing. " needs to be escaped.
# And finally, this (called HEREDOC) does the same thing as above
# only without ", so that sign is not needed to be escaped
echo <<<FOO
<img src="$url">
<img src="{$url}">
<img src="${url}">
FOO;
There are many solutions.
I prefer this one: <?php echo '<img src="'.$url.'">'; ?>
$url stands for the Image-Url, I guess you know ist.
You can do it also:
For example: <img src="<?php echo $url; ?>">
But I like the first method, it's a simple way to put out strings.
Related
I'm trying to get this so when I click the image, the image then shows in a new tab so it's able to be saved
I've tried adding the a href tags inside and around.
Code:
echo "<img src=" . random_image('css/avatars') . " />";
This is what you want:
echo '<img src="' . random_image('css/avatars') . '" />';
You should try
echo "<a target='_blank' href='".random_image('css/avatars')."'>"."<img src=" . random_image('css/avatars') . " /></a>";
I think this can help you
The problem is that you are not properly concatenating your string with what you get from random_image function.
You can try following code, here I am assuming that your random_image function returns complete path to your image
function random_image($path)
{
// returning dummmy image
return "https://images.unsplash.com/photo-1535498730771-e735b998cd64?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=623ed05afcc5a3fa1e444ffca95854dc&w=1000&q=80";
}
echo "<a href='".random_image('css/avatars')."' target='_blank'> <img src='".random_image('css/avatars')."' /></a>";
I know this has already been put out on here but I've looked and mine still won't work and I don't know why someone please give me a hand.
echo '<img src="' . Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50); . '"/>';
Remove the semicolon you have after resize
echo '<img src="' . Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50) . '"/>';
Try this
echo "<img src= '".Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50)."'/>";
Please change
echo '<img src="' . Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50); . '"/>';
this line to
echo '<img src="' . Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50) . '"/>';
Because you had put one extra semi column ; after the resize(50,50)
Please change your code and try. Definitely work .
My code in php is
echo $preference[0]."</br>";
echo '<p class="filter_entity">'.$preference[0].'<img src="' . base_url() . 'images/close-icon.png" onClick="filter_close()"></p>';
html out put is
Business
<img onclick="filter_close()" src="http://localhost/AEC/images/close-icon.png">
I want to put $preference[0] in filter_close() of my php code ... dont know how to do it .
Something like this?
$param = $preference[0];
echo '<p class="filter_entity">'.$preference[0].'<img src="' . base_url() . 'images/close-icon.png" onClick="filter_close(\''.echo $param.'\')"></p>';
Just append the variable as a parameter of the js function
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double
echo "Some array item: {$array[0]}";
I have this code:
if ($topic['user']==$_SESSION['display'])
{
echo '<div class="bottomright"><img src="../assets/icons/Comments-edit.png" /><img src="../assets/icons/Lock.png" /><img src="../assets/icons/Trash.png" /></div>';
}
When I hove over the image the link shows as:
?id=%3C?php%20echo%20$topic_id;%20?%3E&part=3
Rather than:
?id=3&part=3
Why is this not working?
I tried this:
if ($topic['user']==$_SESSION['display'])
{
echo '<div class="bottomright"><img src="../assets/icons/Comments-edit.png" /><img src="../assets/icons/Lock.png" /><img src="../assets/icons/Trash.png" /></div>';
}
Now I get:
?id=$topic_id&part=3
Your code is already within PHP tags, you don't have to add an additional <?php to the link's parameters.
echo '<div class="bottomright"><a href="'. $topic_id .'&part=5"...
Simply terminate your string and concatenate the relevant variables into the string.
Here is a very simplified example:
<?php
$user_name = "Anthony";
echo "Hello ". $user_name ."! How are you?";
//----------^ terminating the string
?>
This will result in:
Hello Anthony! How are you?
Here is the relevant documentation dealing with string concatenation.
You are echoing a php statement, it won't be interpreted, but displayed as it is. Instead, you should concatenate the string, like this:
if ($topic['user']==$_SESSION['display'])
{
echo '<div class="bottomright"><a href="?id='
. $topic_id
. '&part=5"><img src="../assets/icons/Comments-edit.png" /></a><a href="?id='
. $topic_id
. '&part=6"><img src="../assets/icons/Lock.png" /></a><a href="?id='
. $topic_id
. '&part=7"><img src="../assets/icons/Trash.png" /></a></div>';
}
You don't need to "echo" within a PHP statement. Just concatenate the string (here id) as shown here:
if ($topic['user']==$_SESSION['display']){ echo '<div class="bottomright"><img src="../assets/icons/Comments-edit.png" /><img src="../assets/icons/Lock.png" /><img src="../assets/icons/Trash.png" /></div>'; }
Also, you should use "& amp;" (without that space, don't know how to format that here) instead of "&" in your links.
I have a table called 'filename'. I try to output <a> tags in a loop from it like this:
<?php
while($sermon = mysql_fetch_assoc($sermonsQ)) {
echo '<a href="admin/'. $sermon ['filename'] . '">';
echo 'download</a></td>';
}
Current problem is, that $sermon['filename'] containts a leading path like path/test.mp3. But I need only the filename without the path, like test.mp3. How can I do this?
Use basename() for that. It will return the filename without the leading path:
basename($sermon ['filename'])
You can use path_info()
<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
?>
also mysql_* functions are depracated so you shouldn't use them for example to PDO or mysqli
<?php
while($sermon = mysql_fetch_assoc($sermonsQ)) {
$filename = explode('/',$sermon ['filename']);
echo '<a href="admin/'. $filename[1] . '">';
echo 'download</a></td>';
}