I am trying to display a PHP variable within a <p> tag but I either keep getting errors or the name of the variable is shown but not it's content.
Here is the code:
echo '<div class="item" style="background-color:'.$row[colour].';width:'.$row[width].'px;"><p>$row[name]</p></div>';
Where I have written:
<p>$row[name]</p>
will not show up in the browser correctly and instead will just show the name of the variable. If I surround the variable with '' I get an error regaurding syntax.
I have also tried to echo the variable:
<p><?php echo $row[name] ?></p>
But on the website nothing is displayed at all and when I look in the FireFox inspector I see this:
The code has automatically been commented out?
You need to add '. $row[name] . '
like this
echo '<div class="item" style="background-color:'.$row[colour].';width:'.$row[width].'px;"><p>' . $row[name]. '</p></div>';
You have to concatenate the php variable.
Replace
echo '<div class="item" style="background-color:'.$row[colour].
';width:'.$row[width].'px;"><a href="'.$row[link_url].
'" title="'.$row[name].'"><p>$row[name]</p></a></div>';
with
echo '<div class="item" style="background-color:'.$row[colour].
';width:'.$row[width].'px;"><a href="'.$row[link_url].
'" title="'.$row[name].'"><p>'.$row[name].'</p></a></div>';
Notice '"><p>'.$row[name].'</p></a></div>';
When interpolating array values in strings you need to use curly brackets and double quotes.
echo ...."\"><p>{$row['name']}</p></a></div>";
When you use single quotes, php does not interpolate variable names inside the string. Instead, it just prints the names, which is what you're seeing in the source code.
And your array keys should be strings, so ['name'] instead of [name]
Not sure if this is 100% as I am half asleep but at least it will help you with your array a bit.
echo "<div class=\"item\" style=\"background-color:".$row['colour'].";width:".$row['width']."px;\"><p>".$row['name']."</p></div>";
Related
The following line of code is supposed to echo the current season and a data in a php echo:
<?php echo "<h5>" $_SESSION['username'] '<span class="chat_date">'Dec 25"</span></h5>" ?>
Is the session call true? And how can I fix this code?
if you want echo a string you should use concatenation operator ('.')
<?php echo '<h5>' .$_SESSION['username']. '<span class="chat_date">Dec 25</span></h5>' ?>
or use double-quotes (") to passing data
$usename=$_SESSION['username'];
<?php echo '<h5>"$usename"<span class="chat_date">Dec 25</span></h5>' ?>
There is no need to concatenate a string before echoing it in PHP.
The parameters can be passed individually to echo as multiple arguments for a slight performance (speed) increase. In other words, you can use , instead of ..
For example:
<?php echo '<h5>' , $_SESSION['username'] , '<span class="chat_date">Dec 25</span</h5>'; ?>
I would recommend reading the Official Documentation.
<?php
echo "<h5> $_SESSION[username] <span class=\"chat_date\">Dec 25</span></h5>";
?>
Variables are evaluated inside double quoted strings.
To echo double quotes inside double quotes, you add a backslash before the double quote so PHP knows it is not the end of the quoted segment.
For arrays, do not quote the key inside a string.
I am trying to pass a variable from one page to another by appending it to the URL like this-
<a href='single.php?name=".$m['Title']." ' class='movie-beta__link'>
and in the next page(single.php), I am getting the url as-
$title= $_GET['name'];
echo $title;
But nothing is displayed and my URL looks like this-
http://localhost/mdb/single.php?name=
Can someone please point out if I am missing something here.
Try this
<a href='single.php?name=<?php echo $m['Title']; ?>' class='movie-beta__link'>Title</a>
You're getting your single and double quotes mixed up. With single quotes you have to append variables and with double quotes you can put them inside. But when you're outside of <?php tags you can use <?= to echo a variable.
<a href="single.php?name=<?= $m['Title'] ?>" class="movie-beta__line">
This will work above:
<a href='single.php?name=<?php echo $m['Title']; ?>' class='movie-beta__link'>Title</a>
if all this is in php echo, use <a href='single.php?name='".$m['Title']."' class='movie-beta__link'>Title</a>
And its good to avoid underscores on you class names
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 the following code in order to get the tag value to both in anchor and title. but code is ok with anchor text but showing only single char in title..
$tag=$info['name']." from ".$info['city'];
echo' <td class="title1" bgcolor="#F7F7F7"> <a title='.$tag; echo' href=details/';
echo $info['friendly_url'];
echo' >';
echo $tag;
echo'</a></td>';
please note that the tag value is something like "David from NW";
Thanks for your help.
You need quotes around the title value, otherwise the parts after the space will be interpreted as a (malformed) HTML attribute.
echo '<td class="title1" bgcolor="#F7F7F7">';
echo '<a title="'.$tag.'" href="details/' . $info['friendly_url'] . '">';
echo $tag;
echo'</a></td>';
It is good practice to use quotes to surround your HTML attributes to avoid situations like this.
That is butt-ugly code. Repeated echoes get to be impossible to maintain in short order. You could use a HEREDOC and make it pretty/legible at the same time:
echo <<<EOL
<td class="title1" bgcolor="#F7F7F7">
<a title="$tag" href="details/{$info['friendly_url']}">$tag</a>
</td>
EOL;
Any modern PHP-aware IDE will properly color the variables. And note how you can use quotes and variables within the heredoc, without having to do any nasty string concatenation.
I am trying to pass a JavaScript function with an onclick event in php. The problem I am facing is that the function that I need to pass has a parameter that needs to be in double quotes as follows:
onclick="removeElement("div8")"
Now when I use JavaScript to generate the parameter it comes out fine, but whenever I use an echo function in php, the following happens when I look at the function in the browser
onclick="removeElement(" div8")"
the code I am using to generate this is:
echo '<div><img src="img.png" alt="image" onclick="removeElement("div'.$x.'")" /></div>';
where $x is the number to be added to the parameter.
Is there a way that the function is returned as a whole and not get the space in between?
This is happening because you have quotes inside quotes. This will not work, and breaks the HTML parser. It is seeing the onclick as removeElement(, and then it sees an attribute called div8")".
Try this:
echo '.....onclick="removeElement("div'.$x.'")"...';
HTML entities are parsed inside attributes, so the result will be your working code.
Change your echo to this:
echo '<div><img src="img.png" alt="image" onclick="removeElement(\'div'.$x.'\')" /></div>';
You must escape quotes in javascript. Instead of
onclick="removeElement("div8")"
you should write
onclick="removeElement("div8")"
try escaping your single-quotes
echo '<div><img src="img.png" alt="image" onclick="removeElement(\'div'.$x.'\')" /></div>';
As long as you do not use any spaces in the attribute value you can ommit the quotes around the html attribute values. All browsers will handle that fine. So you can write:
onclick=removeElement("div8")
You could also use the single quotes:
onclick="removeElement('div8')" or
onclick='removeElement("div8")'
Or you can escape the double quote:
echo '<div><img src="img.png" alt="image" onclick="removeElement(\"div'.$x.'\")" /></div>';
But a simpler solution would be to write in html directly:
?>
<div>
<img src='img.png' alt='image' onclick='removeElement("div<?php echo $x; ?>")' />
</div>
when you using php echo try this code
<a href="javascript:add_cota(<?php echo $value->ID .','.$k.', \''.$st.'\'';?>)">
$st
is the string param to avoid the ReferenceError: Active is not defined error