In PHP, is conditional variable substitution possible?
I was wondering if something like the following would be possible in PHP:
<?php echo "Hello {isset($name) ? $name : 'World'}!\n"; ?>
or do I have to do the following?
<?php echo "Hello " . ( isset($name) ? $name : 'World' ) . "!\n"; ?>
In general I prefer substitution over concatenation as I find that string concatenation can make strings harder to read by making them overly long.
The context of which is that I'm deciding which option in a select tag should be selected by default based on a database query.
The full context:
<?php
echo "
<form action='add_demographics.php' method='post'>
<table>
<input type=hidden name=userId value='{$_SESSION['username']}'/>
<tr><td>User ID:</td><td>{$_SESSION['username']}</td></tr>";
foreach ($inputLabels as $i => $value)
{
echo "
<tr>
<td>{$value['text']}</td>
<td><select name=$i>";
foreach ($value["options"] as $optionName => $optionValue)
{
echo "
<option value=$optionValue {($result[$i]==$optionValue ? ' selected=true' : '')}>$optionValue</option>";
}
echo "
</开发者_如何学编程select></td>
</tr>";
}
echo "
<tr><td><input type='submit' name='submit' value='Submit' /></td></tr>
</table>
</form>";
?>
nope, cannot do that. You need to do it outside of the string and concat. The only thing php will do inside a string is look for and evaluate a single $var (no expressions) - if you use double quotes or equivalent heredoc (php does not evaluate vars in single quotes)
No, it isn't possible to do it the way you're asking.
A solution to your specific problem, however, could be to assign a variable at the top of the foreach loop to hold whether or not that option should be selected, then echo that variable as part of your output:
foreach ($value["options"] as $optionName => $optionValue)
{
$selected = $result[$i] == $optionValue ? "selected='selected'" : "";
echo "
<option value=$optionValue $selected>$optionValue</option>";
}
<?
function is($var,$sub){
return isset($var)?$var:$sub;
}
echo 'Hello, '.is($name,'World').'!\r\n';
$name = 'Bob';
echo 'Hello, '.is($name,'World').'!\r\n';
?>
Maybe for short-handing it?
You could do something like this:
$default = "World";
$name = "Dunes";
echo "Hello {${isset($name) ? 'name' : 'default'}}";
UPDATE from webbiedave:
"Don't ever do this as it's absolutely atrocious coding practice to use the ternary operator to reference a throw away variable variable simply to avoid concatenation!
Given what you want to do. I'd say
$string = isset($name) ? $name : 'World';
$result = "Hello $string!\n";
echo $result;
It is never actually required.
Learn to use templates.
HTML should be HTML, not some abstract data hardcoded in PHP
<form action='add_demographics.php' method='post'>
<table>
<input type=hidden name=userId value='<?=_h($_SESSION['username'])?>'/>
<tr><td>User ID:</td><td><?=_h($_SESSION['username'])?></td></tr>
<? foreach ($inputLabels as $i => $value): ?>
<tr>
<td><?=_h($value['text'])?></td>
<td><select name="<?=$i?>">
<? foreach ($value["options"] as $optionName => $optionValue): ?>
<option value="<?=$optionValue?>" <? if ($result[$i]==$optionValue): ?> selected=true<? endif ?>>
<?=$optionValue?>
</option>
<? endforeach ?>
</select></td>
</tr>
<? endforeach ?>
<tr><td><input type='submit' name='submit' value='Submit' /></td></tr>
</table>
</form>
精彩评论