I am trying to streamline my php for my current website. Typically, I have treated (successfully) submitted form values using the == operator, as follows;

<?php
if (isset($_POST['yes_or_no'])) {
    if (($_POST['hidden']) == 'yes') {
        echo "<script>
        alert('Yep');
        </script>";
    }
    else {
        echo "<script>
        alert('Nope');
        </script>";
    }
}
else//etc.
?>

<html><head></head><body>

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" id="YesNoForm">
<button name="yes_or_no" type="submit">Click</button>
<input type="hidden" name="hidden" value="yes">
</form>

</body></html>

However, should I be treating the hidden value of the form (the value equals "yes" in the form above) as having the same type and value using the === operator (and therefore use === 'yes' in the code above)? Is the hidden value a text string?

有帮助吗?

解决方案 2

The only reason you wouldn't is if you were debating between numbers as strings (currency, for example) or BOOLEAN phrases ("true" vs TRUE).

"yes" is always going to be a string so yes, please use ===

其他提示

PHP doesn't require explicit variable type definition. so if you assign it a string it will consider it as string if you assign an integer it will be considered as integer value and so on. So yes the value here will be string. For better understanding you can refer

http://www.php.net/manual/en/language.types.type-juggling.php. Hope that's helpful :)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top