Variable class names [duplicate] - php

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 8 years ago.
I want my link class to be a variable (I am using the filtering system Isotope).
$categories = get_categories($args);
foreach($categories as $category) {
$name = '.'.$category->name;
echo '<a href="#" data-filter=$name>'; //$name does not work here
echo $category->name;
echo '</a>';
echo ' / ';
}
My $name is only displayed as $name in the browser and not the category name I want. Outside the data-filter echoing $name gives all the categories as expected. Is there a way to solve this problem with putting the $name in the data-filter? If the answer is too difficult, please point me to the right direction of what I should do to fix this problem myself please.
Thanks!

Strings in single quotes do not do variable expansion. You have to change it to something like this:
echo '<a href="#" data-filter='.$name.'>';
An alternative without the need for concatenation would be:
echo "<a href=\"#\" data-filter=$name>";
Or, more elegant in my eyes:
echo sprintf('<a href="#" data-filter=%s>', $name);
Also I guess you have to add double quotes around that data-filter attributes content...

Related

select option showing 10 blank values after mysql [duplicate]

This question already has answers here:
php - insert a variable in an echo string
(10 answers)
Closed 3 years ago.
All I want to do is echo out a PHP variable inside a <li> tag. Refer to the code below. What's the proper format? Mine is not working.
$row['cat_title'];
echo "<li> {$cat_title} </li>";
If you need to output a value of a particular array key (e.g. $row['cat_title']) in HTML, you have quite a few options.
You can use the concatenation operator (.):
echo '<li>' . $row['cat_title'] . '</li>';
You can use variable interpolation with simple string parsing (note that in this syntax, quotes around the array key must be omitted):
echo "<li>$row[cat_title]</li>";
You can use variable interpolation with complex string parsing (using {}) which is actually not necessary here, but useful for more interpolating more complex expressions. With this syntax, quotes around the array key should be included.:
echo "<li>{$row['cat_title']}</li>";
You can output plain HTML and use the echo shortcut syntax <?= to output the value (only do this if you are already outputting HTML, not if you are currently in a <?php tag; that would be a syntax error.):
<li><?= $row['cat_title'] ?></li>
You can use printf (thanks to Elias Van Ootegem's comment for reminding me of this; I should have included it to begin with). sprintf can be used if you want to save the result to a variable instead; printf will output it immediately:
printf('<li>%s</li>', $row['cat_title']);
The first argument of printf is a format string, where %s is a string conversion specification that will take the value of $row['cat_title'] when printf is executed.
There are other ways, but these are the most common.
You need to reference the right variable. You're on the right track, though.
echo "<li> {$row['cat_title']} </li>";
echo '<li>'.$var.'</li>';
OR
echo "<li>$var</li>";
OR
<li><?=$var?></li>
If the file is mostly PHP in the file then:
<?php echo "<li>" . $row['cat_title'] . "</li>"; ?>
Or conversely if it's mostly HTML in the file then:
<li><?php echo $row['cat_title']; ?></li>
It sounds to me that it's most likely the second option you are looking for.
Another alternative:
<li><?php echo $row['cat_title']; ?></li>
Either of these methods will work...
// Use single quotes and periods like this...
echo '<li>'. $row['cat_title'] .'</li>';
OR
// Use double quotes like this...
echo "<li> $row['cat_title'] </li>";

How to get variables to output in HTML via an echo in PHP [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 2 years ago.
<?php
$request_uri = $_SERVER['REQUEST_URI'] ;
$path=explode("?",$request_uri);
$pname=basename($path[0]);
if ($pname == "blood-facts-for-kids.html") { $p1 = 'Human Body Facts'; $p1u = 'https://www.factsjustforkids.com/human-body-facts.html'; $p2 = 'Blood Facts'; $p2u = 'https://www.factsjustforkids.com/human-body-facts/blood-facts-for-kids.html'; }
echo '<script type="application/ld+json">{"#context":"https://schema.org/","#type":"BreadcrumbList","itemListElement":[{"#type":"ListItem","position":1,"name":"{$p1}","item":"{$p1u}"},{"#type":"ListItem","position":2,"name":"{$p2}","item":"{$p2u}"}]}</script>';
?>
I'm having issues getting the variables to appear in my echo. Everything works as it should, the variables are set IF the web page name is correct and if I echo out the variables by themselves using
echo "{$p1}, {$p1u}, {$p2}, {$p2u},";
The correct data is shown. I'm obviously doing something wrong in the echo code.
For reference, this is a crude method to inject structured data dynamically.
Either use echo with double quotes "":
echo "<script type=\"application/ld+json\">{\"#context\":\"https://schema.org/\",\"#type\":\"BreadcrumbList\",\"itemListElement\":[{\"#type\":\"ListItem\",\"position\":1,\"name\":\"{$p1}\",\"item\":\"{$p1u}\"},{\"#type\":\"ListItem\",\"position\":2,\"name\":\"{$p2}\",\"item\":\"{$p2u}\"}]}</script>";
or use concatenation:
echo '<script type="application/ld+json">{"#context":"https://schema.org/","#type":"BreadcrumbList","itemListElement":[{"#type":"ListItem","position":1,"name":"' . $p1 . '","item":"' . $p1u . '"},{"#type":"ListItem","position":2,"name":"' . $p2 . '","item":"' . $p2u . '"}]}</script>';
Notice that with double-quotes, you need to escape any other double quote inside the string.
You can use
echo $variable_name //to display the variable
And if you want to display it in a HTML tag then
<p>Your age is <?php echo $age ?>.</p>
Im providing a link for more detailed information
https://www.dummies.com/programming/php/how-to-display-php-variable-values/

How to include a variable value as a part of url in php [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 4 years ago.
$imagefilename=$row['uimage'];
i.e storing the url of an image from table to variable imagefilename
now i wish to use that file name and fetch the image from its location by using the command
echo '<img id="unregistered" src="php/customer/customer_images/{$imagefilename}"/>';
but its not considering "$imagefilename" as a part of the url could some one please tell me how to make this happen thanks!
Append the php variable using single quote like below:
echo '<img id="unregistered" src="php/customer/customer_images/'.$imagefilename.'/>';
TRY THIS
<?php
$imagefilename = "file1.jpg";
echo '<img id="unregistered" src="php/customer/customer_images/'.$imagefilename.'/>';
echo "\n";
echo "OR";
echo "\n";
$imagefilename = "php/customer/customer_images/file1.jpg";
echo "https://localhost/folderName/update/update.php?$imagefilename";
?>
Here all the Comments in one Answer:
//Your image url
$var = 'my%20text';
// echo with ' and urldecode
echo 'sometext '.urldecode($var).' someothertext';
//lambda function with urldecode
$myfunc = function($text) {
echo urldecode($text);
};
// working echo with {} (needs lambda function)
echo "somtext{$myfunc($var)}";

How can I echo PHP and HTML in the same line? [duplicate]

This question already has answers here:
php - insert a variable in an echo string
(10 answers)
Closed 3 years ago.
All I want to do is echo out a PHP variable inside a <li> tag. Refer to the code below. What's the proper format? Mine is not working.
$row['cat_title'];
echo "<li> {$cat_title} </li>";
If you need to output a value of a particular array key (e.g. $row['cat_title']) in HTML, you have quite a few options.
You can use the concatenation operator (.):
echo '<li>' . $row['cat_title'] . '</li>';
You can use variable interpolation with simple string parsing (note that in this syntax, quotes around the array key must be omitted):
echo "<li>$row[cat_title]</li>";
You can use variable interpolation with complex string parsing (using {}) which is actually not necessary here, but useful for more interpolating more complex expressions. With this syntax, quotes around the array key should be included.:
echo "<li>{$row['cat_title']}</li>";
You can output plain HTML and use the echo shortcut syntax <?= to output the value (only do this if you are already outputting HTML, not if you are currently in a <?php tag; that would be a syntax error.):
<li><?= $row['cat_title'] ?></li>
You can use printf (thanks to Elias Van Ootegem's comment for reminding me of this; I should have included it to begin with). sprintf can be used if you want to save the result to a variable instead; printf will output it immediately:
printf('<li>%s</li>', $row['cat_title']);
The first argument of printf is a format string, where %s is a string conversion specification that will take the value of $row['cat_title'] when printf is executed.
There are other ways, but these are the most common.
You need to reference the right variable. You're on the right track, though.
echo "<li> {$row['cat_title']} </li>";
echo '<li>'.$var.'</li>';
OR
echo "<li>$var</li>";
OR
<li><?=$var?></li>
If the file is mostly PHP in the file then:
<?php echo "<li>" . $row['cat_title'] . "</li>"; ?>
Or conversely if it's mostly HTML in the file then:
<li><?php echo $row['cat_title']; ?></li>
It sounds to me that it's most likely the second option you are looking for.
Another alternative:
<li><?php echo $row['cat_title']; ?></li>
Either of these methods will work...
// Use single quotes and periods like this...
echo '<li>'. $row['cat_title'] .'</li>';
OR
// Use double quotes like this...
echo "<li> $row['cat_title'] </li>";

using ' and " in php syntax [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get useful error messages in PHP?
Ive started on part of my new year resolution and decided to learn php, as part of it im trying to parse in an xml feed, and echo out the name of the events wrapped in <a> tags linking them back to the events page on the xml feed's site.
I think ive got it all in but i cant seem to see why this isnt working im just getting a blank page, if some one could point me in the right direction it would be much appreciated, cheers
<?php
// F1 W/H xml feed
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N');
foreach ($xml->response->williamhill->class->type as $type) {
$type_attrib = $type->attributes();
echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship
} ?>
<ul>
<?php
foreach($type->market as $event) {
echo "<li>";
echo "<a href="$event_attributes['url']">";
echo $event_attributes['name'];
echo "</a>";
echo "</li>";
}
?>
</ul>
echo "<a href="$event_attributes['url']">";
try changing that line to
echo "<a href=\"".$event_attributes['url']."\">";
The Php parser is pretty funny about this. Usually you pick one and just stick to it, or use both single quotes and double quotes as you please. Just remember that strings with double quotes are parsed for variables.
$hello = "Hello";
echo "$hello master";
is the same as
$hello ="Hello";
echo $hello.' master';
When you are testing your PHP scripts, you'll find it useful to switch on errors - then PHP will actually tell you why it isn't showing you anything:
error_reporting(E_ALL);
Normally you will have missed a ; or mis-typed a variable name.
in your case the error is here:
echo "<a href="$event_attributes['url']">";
You have accidentally ended the string with a double quote, so PHP thinks the string ends here:
echo "<a href="
This is where using single-quotes can be very handy because your double quotes won't then close the string.
echo '<a href="' . $event_attributes['url'] . '">';
The main difference between single and double quotes in PHP is that double quotes has special clever parsing rules and single quotes doesn't. For example:
$myVar = "BLAH";
echo "Example $myVar"; // Example BLAH
echo 'Example $myVar'; // Example $myVar
In your unordered list, you should use a dot to concatenate your string, and escape your double quotes like this:
echo "<a href=\"".$event_attributes['url']."\">";
Instead of
echo "<a href="$event_attributes['url']">";
Your example throws and error because you haven't used proper string concatenation. However, even with correct concat, it would render as <a href=http://someurl>, and you'd need to add the double quotes according to html standard. Hence you have to double quote.
if you want to not be troubled by having to switch between using a ' or a " then i suggest using the php alternative syntax php alternative syntax
with the given code it would look like
<?php
// F1 W/H xml feed
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N');
foreach ($xml->response->williamhill->class->type as $type) {
$type_attrib = $type->attributes();
echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship
} ?>
<ul>
<?php foreach($type->market as $event):?>
<li>
<a href="<?php echo $event_attributes['url']; ?>">
<?php echo $event_attributes['name']; ?>
</a>
</li>
<? endforeach;?>
</ul>
one advantage this would bring is that it would produce cleaner code since you can clearly distiguish your php code from your html which is the presentational part at the price writing all those other <?php ?> and as what others would claim a performance degradation. the choice is yours
Change
echo "<a href="$event_attributes['url']">";
for
echo "<a href=".$event_attributes['url'].">";
You are missing the periods in your second echo, where you have your $event_attributes['url']
<?php
foreach($type->market as $event) {
echo "<li>";
echo "<a href=".$event_attributes['url'].">";
echo $event_attributes['name'];
echo "</a>";
echo "</li>";
}
?>
I would recommend you to enable your error log, it would allow you to know the line with problems in any of your scripts.

Categories