I'm new to shorthand conditional statements and I can't for the life of me work out how to do it, here's the simple code I have:
<?php
function evolve_nav($vals) {
echo '<'.$vals['type'] !== '' ? ''.$vals['type'].'' : 'darn''>';
}
?>
Does anyone know why this doesn't return anything and results in an error?
You just forgot some brackets:
function evolve_nav($vals) {
echo '<'.(!empty($vals['type']) ? $vals['type'] : 'darn').'>';
}
evolve_nav(array('type' => 'foobar'));
evolve_nav(array('not' => 'showing'));
echo '<' . ($vals['type'] !== '' ? $vals['type'] : 'darn') .'>';
$descriptiveVariableName = $vals['type']!=='' ? $vals['type'] : 'darn';
// View code
echo "<$descriptiveVariableName>";
''.$vals['type'].'' is superfluous, make it $vals['type']
'darn''>' those are two string literals without any operator (or anything) between them -> syntax error.
In this case I'd rather not use string concatenation (i.e. using the dot-operator like 'xyz' . $a ) but "pass" multiple parameters to echo.
echo
'<',
''!==$vals['type'] ? $vals['type'] : 'darn',
'>';
or using printf
printf('<%s>', ''!==$vals['type'] ? $vals['type'] : 'darn');
Related
I need to show the message "N/A" if the $row['gate'] is empty. Is it possible to do this using logical symbols ":","?" ?
Like this?
echo (isset($row['gate']) && !empty($row['gate'])) ? $row['gate'] : 'N/A';
PHP 5.3+ allows you to do this.
echo $row['gate'] ?: 'N/A';
That will essentially 'coalesce' an empty value to 'N/A' but if it has a value, it will echo the value.
Ternary operator is commonly used for this kind of validation.
Example while using phps empty()-function:
$output = (!empty($row['gate'])) ? $row['gate'] : 'N/A';
var_dump($output);
(This ofc only checks if the variable is empty, like asked. If you want to check if the variable is defined, use a isset() in there, too).
Yep, it's possible
<?php
$row = array();
echo (empty($row['gate'])) ? 'N/A' : $row['gate'];
?>
yes it is possible with ternary operator
isset($row['data']) ? "your_value" : "N/A";
This is the simplest way.
I want to evaluate a simple ternary operator inside of a string and can't seem to find the correct syntax.
My code looks like this:
foreach ($this->team_bumpbox as $index=>$member)
echo ".... class='{((1) ? abc : def)}'>....";
but I can't seem to get it to work properly. Any ideas on how to implement this?
You can't do it inside the string, per se. You need to dot-concatenate. Something like this:
echo ".... class='" . (1 ? "abc" : "def") . "'>....";
Well, you can do it actually:
$if = function($test, $true, $false)
{
return $test ? $true : $false;
};
echo "class='{$if(true, 'abc', 'def')}'";
I'll let you decide whether it is pure elegance or pure madness. However note that unlike the real conditional operator, both arguments to the function are always evaluated.
am confused with using this ternary operator, i can easily get 1 level if and else
e.g
($blah == $blah) ? blahblah : blahblahblah;
but what if the condition is like this?
if($blah == blah1)
{
echo $blah1;
}
else if($blah == blah2)
{
echo $blah2;
}
else
{
echo $blah;
}
how to convert this using ternary operator ?
<?php echo $blah == 'blah1' ? $blah1 : ($blah == 'blah2' ? $blah2 : $blah); ?>
Notice how the else is wrapped in parenthesis. This can be done again and again, although it becomes confusing to read.
Nested ternaries are a bad idea. They're provided for brevity. If you have nested conditions, you by definition do not have brevity.
Some folk don't even like their use when you have a concise statement. Personally I find the following more clear and readable than the if-based alternative.
echo $success ? 'Success' : 'Failure';
But I would hesitate to do anything more complex than that.
($blah == $blah1) ? $blah1 : (($blah == $blah2) ? $blah2 : $blah);
It would become:
echo (($blah == $blah1) ? $blah1 : (($blah == blah2) ? $blah2 : $blah);
I'm doing this...
<?php $term = ucfirst($_GET['term']);?>
And doing this multiple times on the page:
<?php if (empty($term)) echo 'X'; else echo $term; ?>
Is there a better way to go about this?
You can specify the placeholder value when you first assign the value:
<?php $term = ucfirst($_GET['term']) or $term = "X"; ?>
(Works because the OR has lower precedence than the assignment.)
Then just print that variable henceforth:
<?= $term ?>
It will contain either the input value, or your X.
ternary operator :
$term = (empty(ucfirst($_GET['term']))) ? echo 'X' : $_GET['term'];
Declare a function
function doTerm()
{
$term = ucfirst($_GET['term']);
if (empty($term)) echo 'X'; else echo $term;
}
so you can call it like
doTerm();
whenever you need it to test and echo.
<?php
$term = isset($_GET['term']) ? ucfirst($_GET['term']) : 'X';
echo $term;
//...
?>
If you don't need it anywhere else than you could make it even shorter :-)
echo (isset($_GET['term']) ? ucfirst($_GET['term']) : 'X');
Ternary syntax can work easily here:
Using short hand tags:
<?=empty($term) ? 'X' : $term ?>
or long hand:
<?php echo empty($term) ? 'X' : $term ?>
you can use Conditional Operator like
echo ($term!=null)? 'x' : $term;
Hello I have read following php statement from a blog but I am unable to understand its meaning. Is it treated as if condition or any thing else? Statement is
<?= ($name== 'abc' || $name== 'def' || $name== 'press') ? 'inner-pagehead' : ''; ?>
You can read this as:
if($name=='abc' || $name=='def' || $name=='press') {
echo 'inner-pagehead';
} else {
echo '';
}
The <?= is the echo() shortcut syntax, then the (test)?true:false; is a ternary operation
It is saying that if $name is any one of those 3 values ("abc","def", or "press"), then display the text "inner-pagehead". Otherwise, don't display anything.
This is what I would call a poorly written Ternary condition. it basically echos 'inner-pagehead' if the $name variable matches any of the three conditions. I would have done it like this:
<?php
echo in_array($name, array('abc', 'def', 'press')) ? 'inner-pagehead' : '';
?>
Or, even better:
// somewhere not in the view template
$content = in_array($name, array('abc', 'def', 'press')) ? 'inner-pagehead' : '';
// later, in the view template
<?php echo $content; ?>