在php

时间:2016-10-25 02:20:19

标签: php

请先查看代码并阅读以下内容: -

当输出为“both”时,下一个输出也会出现“text”..

1)我想在$ text和$ img设置时显示输出“both”和“image” 只有在设置“$ text”时才将$ img设置为最后一个“text”

if (!empty($text) && !empty($img) ) { echo "both"; }

if (empty($text) && !empty($img) ) {echo "image";}

if (!empty($text) && empty($img) ) {  echo "text";}

请帮助我老师.....

3 个答案:

答案 0 :(得分:0)

使用isset并使用if else

执行一个条件
<?php
if (isset($text) && isset($img)) { echo "both"; }

if (isset($img)) {echo "image";}

if (isset($text) ) {  echo "text";}

要检查空值,您可以使用

if (!empty($text) && !empty($img)&&isset($text) && isset($img)) {
       echo "both"; 
    } else if (!empty($img)&&isset($img)) {
       echo "image";
     } 
     else if (!empty($text)&&isset($text) ) {
       echo "text";
}

答案 1 :(得分:0)

if(isset($img))
{
    if(isset($text))
    {
        echo "Both";
    }
    else
    {
        echo "Image";
    }
}
else if(isset($text))
{
    if(isset($img))
    {
        echo "Both";
    }
    else
    {
        echo "Text";
    }
}

这对你有用..这也很容易理解

或者你的方式

if(isset($img) && isset($text))
{
    echo "Both";
}
else if(isset($img))
{
    echo "Image";
}
else if(isset($text))
{
    echo "Text";
}

答案 2 :(得分:0)

好又短

echo  (isset($text) && isset($img) ? 'both' :(isset($text) ? $text : (isset($img) ? $img : '')));