2

I have many isset checkings:

if (isset($_POST['name']) && isset($_POST['day']) && isset($_POST['month']) && isset($_POST['year']) && isset($_POST['email']) && isset($_POST['email2'])&& isset($_POST['pass']) && isset($_POST['pass2']))
{

Is there a way to short it?

$isset = array
(
    'name', 'day', 'month', 'year',
    'email', 'email2', 'pass', 'pass2'
);

foreach ($isset As $set)
{
    if (!isset($_POST[$set]) || empty($_POST[$set]))
    {
        echo 'error';
        break;
    }
}

Is that correct?

user2391753
  • 25
  • 1
  • 6
  • it is the right way of checking each post – ROMMEL May 16 '13 at 21:42
  • You could always use `!empty()` however this will return false positives for falsy values – Bojangles May 16 '13 at 21:43
  • Is it the same as isset()? – user2391753 May 16 '13 at 21:44
  • unless you name everything the same with an increasing number at the end; the way you got is the shortest way to check these variables. – Josh Balcitis May 16 '13 at 21:45
  • don't use `empty()`, especially you're dealing with numbers. `empty(0)` is actually TRUE in php. – Marc B May 16 '13 at 21:49
  • instead of `empty()` you might want to use `$set == NULL` or `$set == ''` – Josh Balcitis May 16 '13 at 21:50
  • @JoshBalcitis - other than warnings about undefined indices, `empty()` and `$foo == NULL` will return the exact same thing in all cases. – Sam Dufel May 16 '13 at 21:54
  • @Sam Dufel: empty does not do any warnings. – hakre May 18 '13 at 18:13
  • Before asking a question please take a look for existing solutions for your problem and then - if they don't work for you - ask more specifically for your case and telling what didn't work for you so far. E.g. with your edited question, it's not clear what your issue is even. What is your *concrete programming problem*? – hakre May 18 '13 at 18:18

5 Answers5

4

You could use a loop and empty only:

$keys = array('name', 'day', 'month');  // ...

foreach ($keys as $key) {
  if (empty($_POST[$key])) {
    // fail
    break;
  }
}

Or you could use array_diff_key():

if (array_diff_key(array_flip($keys), $_POST)) {
  // fail (some keys not present in $_POST)
}
hakre
  • 193,403
  • 52
  • 435
  • 836
nice ass
  • 16,471
  • 7
  • 50
  • 89
  • Look at my edit, is that correct? – user2391753 May 16 '13 at 21:47
  • Yes, but beware that `empty` will match `0` too (may fail with checkboxes). If that's a valid value you should stick to `isset()` only and do individual checks later. Also, `isset()` is redundant if you use `empty` – nice ass May 16 '13 at 21:48
  • So that means I can just use empty() instead? – user2391753 May 16 '13 at 21:51
  • Yes, but I suggest you check the manual page for [`empty`](http://php.net/manual/en/function.empty.php) first to see what values are considered "empty" – nice ass May 16 '13 at 21:54
  • To use `empty` with the 2nd method: `array_diff_key(array_flip($keys), array_filter($_POST))` (`array_filter` will drop items that are empty) – nice ass May 16 '13 at 22:04
  • Does this method really shorten it though? What if you had 1000 keys? Are you going to write them all out in the array $keys? That doesn't seem shorter. You're just transferring the length from one place to the other. – Ryan Mortensen May 18 '13 at 00:35
  • It's still shorter than writing 1000 issets – nice ass May 18 '13 at 01:29
3

isset() can take multiple arguments, so you can shorten it simply like this.

if (isset($_POST['name'], $_POST['day'], $_POST['month'], $_POST['year'], $_POST['email'], $_POST['email2'], $_POST['pass'], $_POST['pass2']))

PHP Docs: http://php.net/manual/en/function.isset.php

Chris Bier
  • 14,183
  • 17
  • 67
  • 103
3

Define a function like this:

function getPost($key, $default = null) {
    if (isset($_POST[$key])) {
        return $_POST[$key];
    }
    return $default;
}

Then you can skip the isset verification. If there's no sucho value, by default, the function will return null.

Henrique Barcelos
  • 7,670
  • 1
  • 41
  • 66
  • If you did this you would only get one return value for each key/function pair. How would you then use this? Foreach($_POST as $key) $SomeArray[$key] = getPost($key, $default)? The thing that is missing here, is that even if a $_POST value is "" it still is technically set. So literally every post value would return true in that function. All you'd really be doing is changing $_POST to $Something, and the keys would be identical, with identical values. – Ryan Mortensen May 18 '13 at 00:41
2

If you are sending these from an input form and your default value attribute is value="" then it will still be set in $_POST.

For example, if the previous page has:

<input type="text/css" id="email" name="email" value="" />

Then if the user leaves it blank, isset($_POST['email']) will return true, and $_POST['email'] will have a value of "". That's useless, right?

Try this.

$c = 0;
foreach($_POST as $key => $value)
{
$value = trim($value);//Makes sure there's no leading, or ending spaces.  Safe to guard against a string that is " " instead of "".
if(strlen($value) > 0)
    {
    $c++;
    }
else
    {
    echo "$_POST['" . $key . "'] has a problem.";
    }
break;
}

Then your new if statement for whatever conditions you had in mind could be:

if($c == 8)//8 being the number of keys you're expecting to not be "" or null.
{
//Your conditions.
}

This is good to keep in mind. You are only testing 8 array keys, but what if you had 800? Something like this would be a necessity.

Ryan Mortensen
  • 2,307
  • 3
  • 22
  • 27
1

Depends on what you are doing, if it is to set a value, the ternary operator works wonders:

isset($_POST['day'])?$day=_POST['day'] :$day='';

after that line, $day is always set and you only test with if($day).

If there are many values, you can always run this assignment in a loop:

foreach(array('day','month','name') as $var)
{
  isset($_POST[$var])?$$var=$_POST['$var']:$$var='';
}
Youn Elan
  • 2,379
  • 3
  • 23
  • 32