Jump to content

HOW TO COMPARE 2 STRING ????


alvinchua

Recommended Posts

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
}
?>

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.