문제

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