This question already has answers here:
if block inside echo statement?
(4 answers)
Closed 8 years ago.
I've attempted to add an if/else statement within an already opened echo function, but page comes up blank. Not too sure if I am concatenating properly.
echo '<span class="year-tab-'.$x.' '.if ($single_year==$year_selected) { echo "" } else { echo "display-none" }.' ">';
You cannot have control structures inside concatenation
$style = ($single_year==$year_selected) ? '' : "display-none";
echo '<span class="year-tab-'.$x.' '.$style.'">';
That concatenation is a bit messy. printf() might be a bit cleaner
printf('<span class="year-tab-%s %s">',
%x,
($single_year==$year_selected) ? '' : "display-none"
);
Or you can do this:
echo '<span class="year-tab-'.$x.' '.($single_year==$year_selected ? "" : "display-none").' ">';
See Ternary Operator on this page.
Using the ternary operator:
echo '<span class="year-tab-'.$x;
echo ($single_year==$year_selected)? '>' : ' style="display:none">';
Note that for display value, you have to enclose it inside the display tag.
Related
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/
This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 7 years ago.
Hi I am trying to produce a <td> which also contains an <a> link which will redirect to a function in my controller which also echos the id of my data. Here is my code so far:
<?php
if ($this->session->userdata("username")==$info->U_username) {
echo '<td>EDIT</td>';
}
?>
This code produces an error Disallowed Key characters. Any help or comment is highly appreciated.
For concatenating strings PHP has . operator.
echo '<td>EDIT</td>';
You need to add the result of base_url() to the string you want to output, e.g.:
echo '<td>EDIT</td>';
Either you need to concatinate instead to use php multiple time .use like this
<?php
if ($this->session->userdata("username")==$info->U_username){
echo "<td><a href='".base_url()."'/gamestalker/edit_content/'".$info->C_id."'>EDIT</a></td>";
}
?>
or don't include html into php tags like this
<?php
if ($this->session->userdata("username")==$info->U_username){ ?
<td>EDIT</td>
<? }
?>
You need string concatenation. In PHP you use . for that.
Also codeigniter's base_url can take an argument:
<?php
if ($this->session->userdata("username")==$info->U_username){
echo '<td>EDIT</td>';
}
?>
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...
I'm trying to do a simple task, insert a second function name under "onmouseover" but probably something is wrong with the syntax.
echo '<div onmouseover="changeText(); <?php if($title==""){echo changeViz();}; ?> ">';
I probably need to escape some quotes and add some, but i can't figure out the correct solution.
Thanks
Nothing it's working. Let me give you the complete code...:
echo '<div class="frame" onmouseover="changeText(\''.$text[$i].'\'); <?php if($title[$i]==""){echo changeViz();}; ?>">';
You are nesting <?php ?> inside existing php code, which is a syntax error. Instead, concatenate in the javascript function changeViz() as a quoted string.
This version uses a ternary operator to duplicate the if() statement you had originally.
echo '<div onmouseover="changeText(); ' . ($title == '' ? 'changeViz();' : '') . '">';
The ternary operation here will concatenate changeViz(); onto the echo string if $title == "", or otherwise just concatenate on an empty string.
Update after seeing full code:
You have the quote escaping correct in the first part.
echo '<div class="frame" onmouseover="changeText(\'' . $text[$i] . '\'); ' . ($title == '' ? 'changeViz();' : '') . '">';
You can make your code much more readable if you do not try to do everything in one line:
$onmouseover_action = "changeText(); ";
if($title==""){
$onmouseover_action .= "changeViz(); ";
}
echo '<div onmouseover="'.$onmouseover_action.'">';
It makes your code easier to maintain, and you have less need to comment it, because it describes itself quite much better.
Try this:
echo '<div class="frame" onmouseover="changeText(\''.$text[$i].'\'); '. ($title[$i]=="")?'changeViz();':'').'">';
I'm programming in PHP, I'm using a 'shorthand if' to echo some HTML code on to the page, but it is behaving in odd ways.
echo '<div id="filter_bar">
<ul>';
echo '<li>Trending</li>' : '>Trending</a></li>';
echo '<li>Most Picked</li>' : '>Most Picked</a></li>';
echo '<li>Newest</li>' : '>Newest</a></li>';
echo '</ul></div>';
The resulting code that I get as a result is this
class="filter_selected">Trending</a></li> class="filter_selected">Most Picked</a></li> class="filter_selected">Newest</a></li>
As you can see, the opening list tags are not showing... but they do if I replace the first period '.' on each line with a ',' comma.
So this works, with the commas
Should I be using a comma here? Everywhere online sheems to show a period '.'
The reason is:
echo '<li>' . true ? 'aaa' : 'bbb'; will give you aaa,
because it is same with '<li>1' ? 'aaa' : 'bbb'
And you should do like this:
echo '<li>' . (true ? 'aaa' : 'bbb');
Maybe you can make your life a bit easier:
echo '<div id="filter_bar"><ul>',
'<li>' : '>', 'Trending</li>',
'<li>' : '>', 'Most Picked</li>',
'<li>' : '>', 'Newest</li>',
'</ul></div>';
Which will use commas (no need to repeat echo that much) and you don't need to repeat strings that much if you only want to insert the class attribute.
Then you made use of parenthesis where they were not needed (see Operator Precedence Docs) but at the place where those are needed, you didn't use them (the last case).
Additionally it's better to fill those values to variables upfront, so you can debug stuff easier (and you don't mix $_GET with output, so to prevent mixing output logic with input variables).
That done you could have learned that your issue is not within the echo but just the expression you've formulated with the ternary operator.
$class_trending = $_GET['select'] == "trending" ? ' class="filter_selected"' : '';
$class_most_picked = $_GET['select'] == "most_picked" ? ' class="filter_selected"' : '';
$class_newest = ($_GET['select'] == "newest") || empty($_GET['select']) ? ' class="filter_selected"' : '';
echo '<div id="filter_bar"><ul>',
'<li><a href="?select=trending"', $class_trending, '>Trending</a></li>',
'<li><a href="?select=most_picked"', $class_most_picked, '>Most Picked</a></li>',
'<li><a href="?select=newest"',$class_newest , '>Newest</a></li>',
'</ul></div>';
dunno your problem but this code looks an ugly mess for me.
I'd make it this way:
in the PHP code I'd prepare variables first.
$sections = array(
'newest' => 'Newest',
'trending' => 'Trending',
'most_picked' => 'Most Picked',
);
if (empty($_GET['select']) OR !$choice = array_search($sections,$_GET['select'])) {
$choice = 'newest';
}
and then in the template run smooth and short loop:
<div id="filter_bar">
<ul>
<? foreach ($sections as $sect => $name): ?>
<li>
<a href="?select=<?=$sect?><? if ($choice == $sect) ?>" class="filter_selected"<? endif ?>><?=$name?></a>
</li>
<? endforeach ?>
</ul>
</div>
One possible solution is
echo "<li><a href='?select=newest'";
echo ($_GET['select'] == "newest" || empty($_GET['select'])) ? ' class="filter_selected">Newest</a></li>' : '>Newest</a></li>';
Change your parentheses to the following:
echo '<div id="filter_bar">
<ul>';
echo '<li>Trending</li>' : '>Trending</a></li>');
echo '<li>Most Picked</li>' : '>Most Picked</a></li>');
echo '<li>Newest</li>' : '>Newest</a></li>');
echo '</ul></div>';
If you do not do this, PHP does not know what exactly your condition is. Also have a look at operator precendence as this explains why it works using commas.
By the way: The ?: is called ternary operator.