alvinchua Posted May 8, 2007 Share Posted May 8, 2007 hmm .. how to compare the string value ??? $_POST['select']; is value obtain from the form and i need it to compare with a string and the code below cant works... $payee = $_POST['select']; if ($payee === 'USA'); { echo $payee; } echo wtf; Quote Link to comment https://forums.phpfreaks.com/topic/50512-how-to-compare-2-string/ Share on other sites More sharing options...
taith Posted May 8, 2007 Share Posted May 8, 2007 what is select supposed to have? print_r($_POST); what does it have? Quote Link to comment https://forums.phpfreaks.com/topic/50512-how-to-compare-2-string/#findComment-248151 Share on other sites More sharing options...
obsidian Posted May 8, 2007 Share Posted May 8, 2007 hmm .. how to compare the string value ??? $_POST['select']; is value obtain from the form and i need it to compare with a string and the code below cant works... $payee = $_POST['select']; if ($payee === 'USA'); { echo $payee; } echo wtf; What's wrong with the code you posted? You may need to change the triple equal sign to double, but otherwise, it seems fine. I would recommend a couple clean ups on it, though: <?php $payee = trim($_POST['select']); if ($payee == 'USA') { // It matches echo $payee; } else { // Do something else } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50512-how-to-compare-2-string/#findComment-248152 Share on other sites More sharing options...
thedarkwinter Posted May 8, 2007 Share Posted May 8, 2007 Hi Also, you must use a semi-colon at the end of an if statement. It sometimes works, i think ifs its only 1 line but im not sure. just try <?php if ($payee === 'USA') // no ; { echo $payee; } cheers, tdw Quote Link to comment https://forums.phpfreaks.com/topic/50512-how-to-compare-2-string/#findComment-248162 Share on other sites More sharing options...
obsidian Posted May 8, 2007 Share Posted May 8, 2007 Also, you must use a semi-colon at the end of an if statement. It sometimes works, i think ifs its only 1 line but im not sure. just try ??? You never use a semicolon after an if statement in PHP. If statements are used as any other loops or conditionals. The never require nor allow semicolons without parse errors. The semicolons go after the statements within the if clause. If you are referring to single line conditionals, the case is the same: the punctuation only follows the statement... consider the following: <?php // The following are all valid: if ($var == TRUE) { echo "Congrats!"; } if ($var == TRUE) { echo "Congrats!"; } if ($var == TRUE) echo "Congrats!"; if ($var == TRUE) echo "Congrats!"; ?> By comparing these 4 examples, you can clearly see that the semicolon has nothing to do with the if declaration itself. Quote Link to comment https://forums.phpfreaks.com/topic/50512-how-to-compare-2-string/#findComment-248180 Share on other sites More sharing options...
redbullmarky Posted May 8, 2007 Share Posted May 8, 2007 obsidian, if i'm correct, i think darkwinter posted an accidental typo rather than what he meant, as his code cleared up: if ($payee === 'USA'); { } vs if ($payee === 'USA') { } the first will terminate after the semicolon, and execute the contents between the braces anyway. the second will only conditional execute the contents between the braces. Quote Link to comment https://forums.phpfreaks.com/topic/50512-how-to-compare-2-string/#findComment-248190 Share on other sites More sharing options...
obsidian Posted May 8, 2007 Share Posted May 8, 2007 obsidian, if i'm correct, i think darkwinter posted an accidental typo rather than what he meant Ah, thanks for the clarification... I seem to be a bit rushed today Sorry 'bout that. Quote Link to comment https://forums.phpfreaks.com/topic/50512-how-to-compare-2-string/#findComment-248200 Share on other sites More sharing options...
thedarkwinter Posted May 11, 2007 Share Posted May 11, 2007 yeah sorry, i meant "must not" Quote Link to comment https://forums.phpfreaks.com/topic/50512-how-to-compare-2-string/#findComment-250378 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.