1

If I want to repopulate a form with the $_POST values for example (there are other applications for this problem, but that's the easiest) I have to always check if the $_POST index is set, before I can use it's value, or I'll get the NOTICE from php.

For example:

echo '<input type="text" name="somefield" value="';
if(isset($_POST['somefield']))
  { echo $_POST['somefield']; }
echo '">';

With complex forms, this seems cumbersome and like a lot of repetition. So I thought, let's extract a function:

function varcheck_isset($vartocheck)
{
  if(isset($vartocheck))
    {return $vartocheck;}
  else
    {return '';}
}

and then do

echo '<input type="text" name="somefield" value="';
echo varcheck_isset($_POST['somefield']);
echo '">';

Makes the code nicer.

But when I do this and $_POST['somefield'] is not set, it says

Notice: Undefined index: somefield

:-(

Anybody got an idea or suggestions how to make this work?


EDIT:

Here's what I ended up doing - I accepted Organgepill's answer, just modified it slightly:

function arraycheck_isset($arraytocheck, $indextocheck)
{
  if(isset($arraytocheck) && is_array($arraytocheck) && array_key_exists($indextocheck, $arraytocheck))
    { return $arraytocheck[$vartocheck];}
  else
    { return '';}
}

The comment below by elclanrs was also pretty good. just write:

echo $_POST['field'] ?: '';

Personally I like the non-shorthand version better though, because I may also have cases where I need to check for other things, besides isset() - for example a regex. This way I keep it consistent by going through a function each time.

oliver_siegel
  • 1,666
  • 3
  • 22
  • 37

2 Answers2

3

You can use the Ternary Operators, it's pretty much a shorthand if/else

echo '<input type="text" name="somefield" value="';
echo isset($_POST['somefield']) ? $_POST['somefield'] : '';
echo '">';
Anujan
  • 928
  • 1
  • 9
  • 20
  • 1
    I was actually thinking of `@echo $_POST['somefield']` but scared getting pounced on for it. This method works perfectly. – Dave Chen May 22 '13 at 03:45
  • @DaveChen : Wait, what? What is that and where can I find more information about it? – oliver_siegel May 22 '13 at 22:52
  • @Anujan: Thank you for the response. I am actually aware of the shorthand, but it still puts me in a position where I have to write the variable name ( $_POST["somefield"] ) twice... Leaving the usual room for errors and such. :-/ – oliver_siegel May 22 '13 at 22:53
  • @olli: suppresses errors http://stackoverflow.com/questions/1032161/what-is-the-use-of-symbol-in-php – Dave Chen May 22 '13 at 23:12
  • oh ... haha ^^ well, it's an idea! treating the symptoms :-P – oliver_siegel May 22 '13 at 23:41
3

try something like this:

function postedVal($vartocheck)
{
  if(array_key_exists($vartocheck, $_POST))
    return $_POST["$vartocheck"];
  return '';
}

Your function was always hitting the first case because you are checking if the parameter is set could have been if (isset($_POST[$vartocheck]))

Orangepill
  • 24,500
  • 3
  • 42
  • 63