2014-01-14 107 views
0

我有3个变量,分别命名为$a,$b$c。这些变量保存来自数据库的动态值。php中变量的比较

我想比较这些变量,如果$a$b$c保持空间的手段“”然后打印no message如果变量$a$b保持空间和$c保持信息打印消息,如果$a$c保持空间和$b保持信息打印消息,如果$b,$c保持空间并且$a保持消息打印消息。请帮帮我。由于

我的代码是

if(($_GET['ms1']== " ") && ($_GET['ms2'] == " ") && ($_GET['ms3']== " ")) 

    { 
     print"<p style=color:red;text-align:center;font-family:Times New Roman,Georgia,Serif;>"; 
     echo "No Message From Court."; 
     print"</p>"; 
     print"<p style=color:green;text-align:center;font-family:Times New Roman,Georgia,Serif;>"; 
     echo "Thank You"; 
     print"</p>"; 

    } 

elseif(($_GET['ms1']==' ') && ($_GET['ms2']==' ')) 


     { 
      print"<p style=color:red;font-family:Times New Roman,Georgia,Serif;text-align:center;>"; 
      echo '3'.$_GET['ms3']; 
      print"</p>"; 


     } 

elseif(($_GET['ms1']== ' ') && ($_GET['ms3']==' ')) 


     { 
      print"<p style=color:red;font-family:Times New Roman,Georgia,Serif;text-align:center;>"; 
      echo '2'.$_GET['ms2']; 
      print"</p>"; 


     } 

    elseif(($_GET['ms2']==' ') && ($_GET['ms3']==' ')) 


     { 
      print"<p style=color:red;font-family:Times New Roman,Georgia,Serif;text-align:center;>"; 
      echo '1'.$_GET['ms1']; 
      print"</p>"; 


     } 
+1

问题是什么?顺便说一下,如果你想检查一个单引号,你至少应该使用'==='来引入使用单引号作为设置的问题。 – jeroen

+0

问题是,变量包含空间,所以我根本没有比较,请帮助我先生 – user3181994

+0

我想,如果所有变量包含空格然后打印没有消息,但在这三个,如果任何一个包含字符串消息,然后打印消息。 – user3181994

回答

0
<style> 
    .p_red { color:red;text-align:center;font-family:Times New Roman,Georgia,Serif; } 
    .p_green { color:green;text-align:center;font-family:Times New Roman,Georgia,Serif; } 
</style> 

<?php 
function isEmpty($string) { 
    return ($string === ' '); 
} 

$a = $_GET['ms1']; 
$b = $_GET['ms2']; 
$c = $_GET['ms3']; 

if (isEmpty($a) && isEmpty($b) && isEmpty($c)) { 
    echo "<p class='p_red'>No Message From Court</p>"; 
    print"<p class='p_green'>Thank You</p>"; 
} 

if (!isEmpty($a) && isEmpty($b) && isEmpty($c)) { 
    echo "<p class='p_red'>1. $a</p>"; 
} 

if (isEmpty($a) && !isEmpty($b) && isEmpty($c)) { 
    echo "<p class='p_red'>2. $b</p>"; 
} 

if (isEmpty($a) && isEmpty($b) && !isEmpty($c)) { 
    echo "<p class='p_red'>3. $c</p>"; 
} 
+0

谢谢你,先生好吧 – user3181994

0

这里是一个非常简单的实现:

<?php 
    foreach ($_GET as $key => $val) { 
    if (strpos($key, 'ms') { 
     if (!strpos($val, ' ')) { 
     echo '<p style="color:red;font-family:Times New Roman,Georgia,Serif;text-align:center;">'; 
     echo str_replace('ms', '', $key).$_GET[$key]; 
     echo '</p>'; 
     } 
    } 
    } 

希望这有助于

0

我会建议复制$ _GET项局部变量($ a,$ b,$ c)检查那些条目(isset call)。如果任何/全部不是,请考虑将局部变量默认为“(单个空格)”。那么你可以安全地检查if ($a == ' ')...等,知道你可以使用的值。

你应该考虑把常见的HTML代码放到一个地方,而不是重复3次。

$a = (isset($_GET['ms1'])? $_GET['ms1']: ' '; 
$b = (isset($_GET['ms2'])? $_GET['ms2']: ' '; 
$c = (isset($_GET['ms3'])? $_GET['ms3']: ' '; 
echo '<p style="color:red;font-family:Times New Roman,Georgia,Serif;text-align:center;">'; 
if ($a == ' ' && $b == ' ' && $c == ' ') { 
    echo "No Message From Court."; 
    print"</p>"; 
    print"<p style=color:green;text-align:center;font-family:Times New Roman,Georgia,Serif;>"; 
    echo "Thank You"; 
} elseif ($a == ' ' && $b == ' ') { 
    echo '3'.$c; 
} elseif ($a == ' ' && $c == ' ') { 
    echo '2'.$b; 
} elseif ($b == ' ' && $c == ' ') { 
    echo '1'.$a; 
} 
echo '</p>'; 

请注意,您应该警惕注射讨厌的HTML(iFrame等)时,简单地呼应了用户输入(这是很容易弥补自己的GET字段)的用户。

+0

谢谢你,先生,这对我很有帮助 – user3181994