开发者

$var == 'string1' || 'string2' always returns TRUE

开发者 https://www.devze.com 2023-04-11 04:34 出处:网络
I want to echo $q2 and $q3 based on the question parameter, but it keeps giving me the same value regardless of the parameter\'s value. What did I do wrong?

I want to echo $q2 and $q3 based on the question parameter, but it keeps giving me the same value regardless of the parameter's value. What did I do wrong?

$q1 = $_GET['question'];
if ($q1 == "doma开发者_开发知识库ina.com"||".domaina.com"||"domaina.co")
{
    $q2 = 'domaina';
    $q3 = 'DomainA';
}
elseif ($q1 == "domainb.com"||".domainb.com"||"domainb.co")
{
    $q2 = 'domainb';
    $q3 = 'DomainB';
}
elseif ($q1 == "domainc.com"||".domainc.com"||"domainc.co")
{
    $q2 = 'domainc';
    $q3 = 'DomainC';
}
else {
    $q2 = 'noquestions';
    $q3 = 'NoQuestions';
} 


Any not empty string evaluates to true, so your first 'or' statement always evaluates to true.

The right to do is:

if ($q1 == "domaina.com"||$q1 == ".domaina.com"||$q1 == "domaina.co")

instead of

if ($q1 == "domaina.com"||".domaina.com"||"domaina.co")


tooooo repetitive.

$q1 = $_GET['question'];
$q2 = 'noquestions';
$q3 = 'NoQuestions';
if (preg_match('~^.?domain([a-c])\.com?$~',$q1,$m)) {
  $q2 = 'domain'.$m[1];
  $q3 = 'Domain'.strtoupper($m[1]);
}

By the way, to let you know, your way of writing a code, with no indents or spaces, is terrible.


You have an error in your == statement.

Should be like this :

$q1 = $_GET['question'];
if ($q1 == "domaina.com"|| $q1 ==  ".domaina.com"|| $q1 == "domaina.co")
{
    $q2 = 'domaina';
    $q3 = 'DomainA';
}
elseif ($q1 == "domainb.com"|| $q1 == ".domainb.com"||$q1 == "domainb.co")
{
    $q2 = 'domainb';
    $q3 = 'DomainB';
}
elseif ($q1 == "domainc.com"||$q1 == ".domainc.com"||$q1 == "domainc.co")
{$q2 = 'domainc';
$q3 = 'DomainC';
}
    else {$q2 = 'noquestions';
    $q3 = 'NoQuestions';
} 

You could also use in_array :

$q1 = $_GET['question'];
if(in_array("$q1", array("domaina.com",".domaina.com",""domaina.co"")))
{
    $q2 = 'domaina';
    $q3 = 'DomainA';
}
    //...


The following will always evaluate to true.

if ($q1 == "domaina.com"||".domaina.com"||"domaina.co")

You probably meant:

if ($q1 == "domaina.com"||$q1 == ".domaina.com"||$q1 == "domaina.co")


Your conditionals in the ifs are wrong. Try fixing those first.

if ($q1 == "domaina.com"||$q1 == ".domaina.com"||$q1 == "domaina.co")
{
    ...
}


you need to format it like so:

if ($q1 == "domaina.com" || $q1 == ".domaina.com" || $q1 == "domaina.co")

What you were doing was interpreted as:

if ($q1 == ("domaina.com" || ".domaina.com" || "domaina.co"))
// interpreted as: 
if ($q1 == (((bool)"domaina.com" || (bool)".domaina.com" || (bool)"domaina.co"))
// interpreted as: 
if ($q1 == ((true || true || true))
// interpreted as: 
if ($q1 == true)
// interpreted as: 
if ((bool)$q1 == true))

Note also that had you use the === operator instead, your last condition would have been the one that always evaluated to true instead of the first one, since you would be comparing a string with the result of a boolean operation.


You could simplify your if statements by using the strpos function like this:

$q1 = $_GET['question'];
if (strpos($q1, "domaina") === true) {
    $q2 = "domaina";
    $q3 = "DomainA";
} elseif (strpos($q1, "domainb") === true) {
    $q2 = "domainb";
    $q3 = "DomainB";
} elseif (strpos($q1, "domainc") === true) {
    $q2 = "domainc";
    $q3 = "DomainC";
} else {
    $q2 = "noquestions";
    $q3 = "NoQuestion";
}


I think you can simplify your code a bit:

<?php

$q1 = $_GET['question'];
$q2 = getDomain($q1);
$q3 = ucfirst(substr($q2, 0, -1)) + strtoupper(substr($q2, -1));

function getDomain($url){
   $arrA = array('domaina.com', '.domaina.com', 'domaina.co');
   $arrB = array('domaina.com', '.domaina.com', 'domaina.co');
   $arrC = array('domaina.com', '.domaina.com', 'domaina.co');

   $q1 = strtolower($url);
   if(in_array($q1, $arrA))
      return "domaina";
   if(in_array($q1, $arrB))
      return "domainb";
   if(in_array($q1, $arrC))
      return "domainc";

   return '';
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号