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;

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.