Echo foo based on if $bar is isset
Normally when I retrieve an array from the database, I check to see i开发者_如何学JAVAf a certain element exists or is set to a certain value and echo text out accordingly:
if (isset($bar)) // or is true
{
echo "something";
}
else // elseif is set to false
{
echo "not something";
}
But once you lots of variables, it can make your code quite an eyesore. I know you can easily create a function to do this, but does php provide an functionality built in to do this? What do fellow SOs do to get around this?
I find the ternary operator to be less of an eye-sore in these situations.
So
echo (isset($bar) ? 'something' : 'not something');
Define your own function and use it. Something like this
function echo_if_isset($foo) {
if (isset($foo))
echo $foo;
}
echo_if_isset($bar);
I think you problem is a bit more complex that what you present here but I'll give it a shot.
1st) I normally pull records out of the db using mysql_fetch_assoc()
$q = "SELECT * FROM something";
$qr = mysql_query( $q );
$qrow = mysql_fetch_assoc( $q );
2nd) Use a for loop
foreach( $qrow as $k => $v )
{
if( strlen( $v ) )
{
echo "the value is: $v\n";
}
else
{
echo "the value is blank\n";
}
}
精彩评论