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>
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:
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.