Zeo
Zeo

Reputation: 109

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!

Upvotes: 3

Views: 78

Answers (4)

syndicate_software
syndicate_software

Reputation: 154

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!";
    }

Upvotes: 0

CollinD
CollinD

Reputation: 7573

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!"

Upvotes: 4

user557846
user557846

Reputation:

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!";
}

Upvotes: 7

JimL
JimL

Reputation: 2541

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!";
}

Upvotes: 0

Related Questions