4

This can be done in Javascript with isNAN instead of !isset. Using the example below - both forms post to my script, one without a value and one with a value. Is the below code a correct way to do this in PHP to assign a value if the post var is not present?

$mycheck = !isset($_POST['value']) ? 0 : $_POST['value'];

<? 
if($_POST) :
  $mycheck = !isset($_POST['value']) ? 0 : $_POST['value'];
  echo $mycheck;
endif;
?>

<!-- send value-->
<form action="" method="post">
  <select name="value">
    <option value="0">0</option>
    <option value="5">5</option>
  </select>
  <input type="submit" name="submit">
</form>

<!-- doesn't send value-->
<form action="" method="post">
  <input name="different_var">
  <input type="submit" name="submit">
</form>
Laurence Tuck
  • 410
  • 2
  • 8
  • 30

2 Answers2

6

Yes, that's an acceptable way of setting default values.

An alternative, introduced in PHP 5.3, is to omit the middle argument of your ternary expression:

$mycheck = $_POST['value'] ?: 0;

However, this will likely throw notices for trying to access an array with a non-existant key if $_POST['value'] isn't set. Therefore your original method is more-accepted.

Mike B
  • 31,886
  • 13
  • 87
  • 111
  • It should be `$_POST['value']` instead of `isset($_POST['value'])`, as per the OP's intentions. – Palladium Jul 20 '12 at 16:13
  • I think that `isset()` is perfectly acceptable for this solution. It will always give true or false, and will do exactly what the OP wants... – VictorKilo Jul 20 '12 at 16:16
  • so if the posted value is a string your method is acceptable, but if its an array its better to use my original method? – Laurence Tuck Jul 20 '12 at 16:17
  • I would think so. Ternary operators are looking for a true/false response and `isset()` will always give you that, even with an array. – VictorKilo Jul 20 '12 at 16:18
  • @Innate Sort of but not quite. It depends on whether the value is already defined (hence, if you had an undefined string `$value`, the abbreviated notation will still throw notices). However, notices are rarely ever worth paying attention to and almost certainly not worth writing longer code to avoid; a case of useless notices is where `session_start()` will throw notices at you if a session is already declared, even though you need `session_start()` on every page to extend the session. – Palladium Jul 20 '12 at 16:20
  • Take a look at F. Orvalho's answer though, he brings up a valid point. – VictorKilo Jul 20 '12 at 16:20
  • something to note here is that the posted value is a numeric value. this post: http://stackoverflow.com/questions/7191626/isset-and-empty-what-to-use make me wonder if !empty() is the better way to go - re F. Orvalho's answer. empty throws an error however - undefined index. – Laurence Tuck Jul 20 '12 at 16:33
1

It works, but there's a cleaner way of writing it.

As of PHP 5.3, you can shorten ternary operations down to two expressions, like so:

$mycheck = $_POST['value'] ?: 0;

It means basically what you already have.

Palladium
  • 3,723
  • 4
  • 15
  • 19