PHP - Display No Code if PHP Value is Empty
I am using queries like this to pull in information from a database:
<p><strong>Show Description:</strong><br/><?php echo cimy_uef_sanitize_content(get_cimyFieldValue(1, 'show-description')); ?></p>
Is there any way to put the title 开发者_如何转开发(Show Description) into the PHP string and then, if the PHP field value is empty, to not show anything?
I think it would involve a PHP if/else statement, but I am not sure what the code should look like.
Thanks Zach
It is hard without seeing the code for those functions, but at a guess:
if (get_cimyFieldValue(1, 'show-description') != '')
{
echo "<p><strong>Show Description:</strong></p>" . cimy_uef_sanitize_content(get_cimyFieldValue(1, 'show-description'));
}
Should work
You can use PHP's empty() function for this purpose -
<p>
<?php
$data = cimy_uef_sanitize_content(get_cimyFieldValue(1, 'show-description'));
if( !empty($data) )
{
echo "<strong>Show Description:</strong><br/>";
echo $data;
}
?>
</p>
According to the documentation, this function will return true
if the $data
variable is either -
1. "" (an empty string)
2. 0 (0 as an integer)
3. 0.0 (0 as a float)
4. "0" (0 as a string)
5. NULL
6. FALSE
7. array() (an empty array)
8. var $var; (a variable declared, but without a value in a class)
If I understood You correctly, You need something like this:
<?php $title = cimy_uef_sanitize_content(get_cimyFieldValue(1, 'show-description')); ?>
<?php if(!empty($title)): ?>
<p>
<strong>Show Description:</strong><br/>
<?php echo $title; ?>
</p>
<?php endif; ?>
Not compiled, so there can be errors in code, but you get the point...
精彩评论