Question

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?

Was it helpful?

Solution 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 ===

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top