<?php if (!empty($box1) && !empty($box2)) { echo ' | content here'; } ?>
<?php if (!empty($box1) && empty($box2)) { echo 'content here'; } ?>
Basically, I want to get rid of the pipe if box2 is empty. Is there a better way to write this?
<?php if (!empty($box1)) { echo (empty($box2) ? '' : ' | ') . 'content here'; } ?>
It's hard to say what's the "best" way to write it elegantly without having a grander scheme of things, but at least you can shorten it as follows:
<?php if(!empty($box1)) { echo (!empty($box2) && ' |') . 'content here'; } ?>
Alternately, if you don't like the && style, you can use a ternary operator:
<?php if(!empty($box1)) { echo (!empty($box2) ? ' |' : '') . 'content here'; } ?>
Or another conditional.
Coarsely, if "most" elegant way would be to take a closer look at what $box1 and $box2 represent and then create a view helper (in the MVC methodology) along the lines of:
class SomeModel {
int $box1;
int $box2;
function make_suffix() {
$suffix = '';
if(!empty($this->box1)) {
if(!empty($this->box2)) {
$suffix .= ' | ';
}
$suffix .= 'content here';
}
return $suffix;
}
}
<?php
if (!empty(&box1)) {
if (!empty($box2) {
echo ' | ';
}
echo 'content here';
}
?>
Using just ternary operators:
<?php echo !empty($box1) ? ( !empty($box2) ? ' | ' : '' ) . 'content here' : '' ?>
Related
<?php
$genre = get_the_term_list($post->ID, 'genre', '<span class="genre">', ', ', '</span>');
$tags = get_the_term_list($post->ID, 'tag-series-movies','<span class="genre">', ', ', '</span>');
if($genre,$tags)
echo '<li>Genre: '. $genre . '</li>';
echo '<li>Tags: '. $genre . '</li>';
else {
echo '';
}
?>
Hi. Guys whats wrong of my code? though the code is working 50%. the $genre is working but the else is not working. please help. i want to show the nothing when no genres is not available.
Change if($genre == $genre) to if($genre){
get_the_term_list returns the list or false so you just need to check that $genre is a truthy value
In your current code when $genre is false you are checking if(false == false) which always returns true
EDIT:
you can simplify your code like this
if($genre){
echo '<li>Genre: '. $genre . '</li>';
}
if($tags){
echo '<li>Tags: '. $tags . '</li>';
}
There's no need for the else statement as you're not echoing anything
in wordpress when I use <?php the_title( '<h3>', '</h3>' ); ?> it works
but if I have a different php output like this one <?php echo $variable['custom_title_option']; ?> how can I do the same as on the_title
also if I use a function like the below example:
function change_hading_titles() {
global $variable;
$heading_tag = $variable['custom_title']; //option name
if ($heading_tag == "h1") {
print '<h1>', '</h1>';
} elseif ($heading_tag == "h2") {
print '<h2>', '</h2>';
} elseif ($heading_tag == "h3") {
print '<h3>', '</h3>';
} elseif ($heading_tag == "h4") {
print '<h4>', '</h4>';
} elseif ($heading_tag == "h5") {
print '<h5>', '</h5>';
} elseif ($heading_tag == "h6") {
print '<h6>', '</h6>';
}
}
add_action('change_hading_titles', 'change_hading_titles');
is it possible to use do_action( 'change_hading_titles' ); to change all my custom titles?
So, I mean to retrieve the function for closing <?php echo $variable['custom_title_option']; ?> with heading tags
Do you mean?
function getTag($tagName, $titleValue){
return "<".$tagName.">".$titleValue."</".$tagName.">";
}
$tag = getTag("h1", "hello");
// $tag = <h1>hello</h1>
..If that is not what your after, please explain a little more clearly, am happy to help further. Either way, this would be a significantly more efficient way of doing that function.
So, I have an input as below:
<input type="text" id="rhc_phone" name="rhc_phone" placeholder="Phone number"/>
Which in return store as a variable:
var data = {
'phone': $('#rhc_phone').val()
};
This then can be extracted via following:
<?php
echo '
<p>Call me at ' . $_POST['phone'] . '.</p>
';
?>
However, the input can be left out.
Now, what is the if statement to check if the variable has value and if not, then show something else like below.
<?php
if (!isset($_POST['phone']) = 0) {
echo '
<p>Call me at ' . $_POST['phone'] . '.</p>
';
}else{
<p>No Number</p>
}
?>
Is it correct?
Thanks!
Use empty
if(!empty($_POST['phone'])
{
echo '<p>Call me at'. $_POST['phone'] . '</p>';
} else {
echo '<p>No Number</p>';
}
you can use empty() function.
php empty() function
if(empty($_POST['phone']))
{
echo '<p>Number is empty</p>';
}
else
{
echo '<p>Your Number is '.$_POST['phone'].'</p>';
}
My PHP is failing me. I'm trying to put a div around an element in a function. Here is the original code:
if ($withCurrency) {
$currency = (isset($GLOBALS['aitThemeOptions']->directory->currency)) ? $GLOBALS['aitThemeOptions']->directory->currency : '';
return $currency . ' ' . $lowestPrice;
}
I want to put a div around the $currency.
I've tried changing this line:
return $currency . ' ' . $lowestPrice;
to this:
return "<div>" . $currency . "</div> " . $lowestPrice;
However that actually outputs the div tags as text on the page. Can someone tell me how to add the divs so they render as html?
UPDATE:
Here is the entire function:
function getTourPrice($tourId, $withCurrency = true) {
$lowestPrice = false;
$offers = getTourOffers($tourId);
// first get price from special offers
foreach ($offers as $offer) {
if ((isset($offer->options['special'])) && ((!$lowestPrice) || floatval($offer->options['price']) < $lowestPrice)) {
$lowestPrice = floatval($offer->options['price']);
}
}
// if special price isn't available then get price from normal offers
if (!$lowestPrice) {
foreach ($offers as $offer) {
if ((!$lowestPrice) || (floatval($offer->options['price']) < $lowestPrice)) {
$lowestPrice = floatval($offer->options['price']);
}
}
}
if (!$lowestPrice) return false;
if ($withCurrency) {
$currency = (isset($GLOBALS['aitThemeOptions']->directory->currency)) ? $GLOBALS['aitThemeOptions']->directory->currency : '';
return $currency . ' ' . $lowestPrice;
} else {
return $lowestPrice;
}
}
The result of that function is called with the following code:
{var $lp = getTourPrice($post->id)}
{if $lp}<div class="item-price"><span><span><span class="from">From</span>{$lp}</span></span></div>{/if}
If I use echo instead of return like this:
echo "<div>" . $currency . "</div> " . $lowestPrice;
I lose all the html above. I.E. What was the following:
<div class="item-price"><span><span><span class="from">From</span>{$lp}</span></span></div>
Becomes this:
<div>R</div> 56200
(where R is $currency and 56200 is $lowestPrice)
Hope that's not painfully convoluted.
This will have something to do with what you are doing with the return
I imagine if you did this:
if ($withCurrency) {
$currency =
(isset($GLOBALS['aitThemeOptions']->directory->currency)) ?
$GLOBALS['aitThemeOptions']->directory->currency :
'';
echo "<div>" . $currency . "</div> ";
}
It would work.
Is your code the end of a function?
Try using the :
{$lp|escape:false}
I would simply place the div in the $currency variable, so it will be added upon return. I would change this line of code:
$currency = (isset($GLOBALS['aitThemeOptions']->directory->currency)) ? $GLOBALS['aitThemeOptions']->directory->currency : '';
to this:
$currency = (isset($GLOBALS['aitThemeOptions']->directory->currency)) ? '<div>'.$GLOBALS['aitThemeOptions']->directory->currency.'</div>' : '';
or another method:
$currency = '<div>'.$currency.'</div>';
This way you are defining the html in the variable and not in the return. This is not tested, but it is the way I tend to code. :) Hope it helps!
Also one more thing, it is a bit of controversy among programmers, but I also recommend using single quotes when defining html in php.
Simple php really, just can't get it for some reason...
<?php
$myvalue = 'yes'
if ( $myvalue == 'yes' ) {
echo 'This is some test content, within which is some more php bits
<?php echo 'Test'; ?> and an if statement
<?php if ( $anothervalue = "test" ) { echo 'Print this'; } else
{ echo 'Print that' ; } ?>
And then some more content
<br />
Test
?>
} else {
echo 'The value didnt match';
}
?>
I need to include the php inside the echo, how can i do this?
As far as I know, you can't. echo is just for outputting.
Refactor instead.
<?php
$myvalue = 'yes'
if ( $myvalue == 'yes' ) {
?>
This is some test content, within which is some more php bits <?php echo 'Test'; ?> and an if statement
<?php
if ( $anothervalue = "test" ) {
echo 'Print this';
} else {
echo 'Print that' ;
}
?>
And then some more content
<br />
Test
<?php
} else {
echo 'The value didnt match';
}
<?php
function dosomething($anothervalue){
if ( $anothervalue = "test" ) {
return 'Print this'; }
else { return 'Print that' ; }
}
$myvalue = 'yes'
if ( $myvalue == 'yes' ) {
echo 'This is some test content, within which is some more php bits'.dosomething('anothervalue'); ?>
And then some more content
?>
I would suggest that you create a function first:
<?php
function dosomething($anothervalue){
if ( $anothervalue = "test" ) {
return 'Print this'; }
else { return 'Print that' ; }
}
then include it in your statement/text;
$myvalue = 'yes'
if ( $myvalue == 'yes' ) {
echo 'This is some test content, within which is some more php bits'.dosomething('anothervalue'); ?>
And then some more content
?>
You have to concatenate strings bits with the . operator:
echo 'This is some test content' . ' chained to other stuff' . someFunction() . 'and some other stuff' . $someVariable . ', right?';
try this
<?php
$myvalue = 'yes';
if ( $anothervalue == "test" ) { $test = 'Print this'; } else { $test = 'Print that' ; }
if ( $myvalue == 'yes' ) {
echo "This is some test content, within which is some more php bits $test
<br />
Test";
}
else
{
echo 'The value didnt match';
}
?>
You can certain express conditional text in the context of an echo. You can use string concatenation (using commas or dots) with inline conditional syntax (ternary operators) so that you don't have to bounce in and out of <?php nor would you need to write multiple echos.
Code: (Demo)
$myvalue = 'yes';
$anothervalue = 'test';
if ($myvalue == 'yes') {
echo 'This is some test content, within which is some more php bits Test ' , ($anothervalue == 'test' ? 'Print this' : 'Print that') , ' And then some more content<br />Test';
} else {
echo 'The value didnt match';
}
Output:
This is some test content, within which is some more php bits Test Print this And then some more content<br />Test
you can try concatenation like
echo "<img src='".$url."' />";
Try this but I suggest you go to w3schools and learn up on the basics before continuing.
<?php
$myvalue = 'yes';
if ( $myvalue == 'yes' ) {
echo 'This is some test content, within which is some more php bits Test and an if statement';
if ( $anothervalue = "test" ) {
echo 'Print this';
}
else {
echo 'Print that';
}
?>
And then some more content
<br />
Test
<?php
}
else
{
echo 'The value didnt match';
}
?>