Better Understanding of Strings


Could somebody please tell me why if I write my code like this:

$question_type = $_POST['question_type'];
if ($question_type == "Numeric") {
blah blah blah

PHP correctly compares the text from my posted question type to the word “Numeric”, but if I do it like this:

if ($_POST['question_type'] == "Numeric") {
blah blah blah

it behaves “badly”??? What’s the difference between the two codes?

Thanks so much…


There isn’t a difference between the two snippets, so I’m guessing your real code has something else which is causing it to behave ‘badly’ (?).

Thanks so much for quick reply…

Would it make any difference know that what I’m posting looks like this?:

<input name="question_type" type="hidden" id="question_type" value="<?php echo $row['question_type'] ?>" />

Any additional thoughts would be appreciated as I didn’t think there was a difference, but when making the change mentioned, my program went from not working to working (very troubling!).


Wh not use is_numeric?

if (is_numeric($_POST['question_type']))


Because $_POST[‘question_type’] isn’t a number, it’s the word ‘Numeric’.

Eric, you will need to post more information so that we can help. What you’ve posted so far isn’t enough to determine the source of the error.


Thank you for your continued responses to this thread; I fear that now I can’t even replicate the error. The good news is that is seems to be working; though it always makes me nervous when this sort of thing happens as there’s usually something else going on that I’m not aware of.

So, for now, I’m going to move on in my project, but I may very well revisit this topic at a future date when I notice something else wacky going on with this particular piece of the puzzle.

Enjoy your week!


:L I used to get that all the time. Something’s not working, no matter what you try. So you give up on it and work on another part of the system, come back and the initial issue is working fine! I always thought it was a sign that I didn’t know what was going on with my code, so I looked up everything I could about it - bug reports related to the functions, how the functions worked at a lower level (that bit was rather interesting at the time) and all sorts of things. Turned out I was missing a backslash. Brain fart!

Those problems are generally caused by one or two factors, and it seems you must have corrected them :slight_smile:

I hope so! :slight_smile: