简体   繁体   中英

PHP : Is there a way, to write this if/elseif/else statement shorter?

So I have been actively learning PHP for the past days and, for practise, I wrote PHP script, which takes HTML Form values.

I tried writing script, where I have 5 persons, and you have to input your name and last name. If it matches with one of the five persons, it sends one message, if not, other. Here it is (Without form part, only PHP):

    $first = $_POST["first"];
    $last = $_POST["last"];

    if ($first == "Jeb") {
        if ($last == "Jeb") {
            print "Hello!";
        } else {
            print "I dont know you!";
        }
    } elseif ($first == "Bob") {
        if ($last == "Bob") {
            print "Hello!";
        } else {
            print "I dont know you!";
        }
    } elseif ($first == "Bill") {
        if ($last == "Bill") {
            print "Hello!";
        } else {
            print "I dont know you!";
        }
    } elseif ($first == "Annie") {
        if ($last == "Annie") {
            print "Hello!";
        } else {
            print "I dont know you!";
        }
    } elseif ($first == "Hugo") {
        if ($last == "Hugo") {
            print "Hello!";
        } else {
            print "I dont know you!";
        }
    } else {
        print "I dont know you!";
    }

It works great, but I have question- can this be done easier / written shorter? And can this be done using switch statement?

Any respone will be apprecieted!

i can see a way using arrays

$people=array('Bob Bob', 'Bill Bill');//etc

if(in_array($first.' '.$last,$people)){
 print "Hello!";
}else{
   print "I dont know you!";
}

A solution using arrays. Assumes all known names are pairs of the same name (bob bob, jeb jeb, etc)

$knownNames = array("Jeb", "Bob", "Bill", "Annie", "Hugo");
if (in_array($first, $knownNames) && $first == $last)
  print "Hello!";
else
  print "I don't know you!"

I'd use arrays instead (avoids duplicating code/ifs)

$name = array(
  'first' => $_POST["first"],
  'last'  => $_POST["last"]
);

$knownNames = array(
  array(
    'first' => 'Bob',
    'last'  => 'Bob'
  ),
  array(
    'first' => 'Bill',
    'last'  => 'Bill'
  ),
  array(
    'first' => 'Annie',
    'last'  => 'Annie'
  ),
  array(
    'first' => 'Hugo',
    'last'  => 'Hugo'
  )
);

if (in_array($name, $knownNames)) {
    print "Hello!";
} else {
    print "I dont know you!";
}

In your example, you are making a decision based on two tests:

Do I recognize the submitted first name?

Do I recognize the submitted last name?

Then you are responding based on whether the above tests are BOTH true.

I would suggest that you separate each test into it's own function:

    function isValidFirstName($name)
    {
        $valid_names = array('Jeb','Bob','Bill','Annie','Hugo');
        return in_array($name, $valid_names);
    }

    function isValidLastName($name)
    {
        $valid_names = array('Jeb','Bob','Bill','Annie','Hugo');
        return in_array($name, $valid_names);
    }

    $first = $_POST['first'];
    $last = $_POST['last'];

    if( isValidFirstName($first) && isValidLastName($last) )
    {
        print "Hello!";
    } else 
    {
        print "I don't know you!";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM