0

I have been racking my brain for a bit and getting nowhere. I'm probably missing something really simple, but what I effectively need is.

if string 1 or 2 is set then compare with string 3 providing string 3 is not null.

if($_SESSION['code'] || $_GET['id'] <> $_POST['code'] && isset($_POST['code']))
1
  • What are you comparing to string 3? Also, what are you doing with the comparison? Commented Nov 28, 2012 at 1:01

5 Answers 5

1

I'm not sure I follow, but you're logic seems to state:

if(isset($string3)){
    if(isset($string1)){
         strcmp($string1,$string3);
    }elseif(isset($string2)){
         strcmp($string2,$string3);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I want it to be all in the same if statement if possible.
if you're looking to do a different strcmp based on the conditions, you couldn't technically have everything within a single if. If you're looking to just have everything on the same line, you could go ternary. i.e. $res = (isset($string3))? (isset($string1))?strcmp($string1,$string3):strcmp($string2,$string3): null;
0
if(isset($string3) && ((isset($string1) && $string1 == $string3) || (isset($string2) && $string2 == $string3)))

Something like this would work. First see if isset $string3. Then a wrapped or operator individually checking isset $string1 or 2 and if they equal string 3.

Comments

0
if( ($a = $_SESSION['code'] ? $_SESSION['code'] : $_GET['id'])
 && isset($_POST['code']) && $a == $_POST['code']) {

If you want $_GET['id'] to override $_SESSION['code'] in the case that both are set:

if( ($a = $_GET['id'] ? $_GET['id'] : $_SESSION['code'])
 && isset($_POST['code']) && $a == $_POST['code']) {

More correctly:

if( (isset($_SESSION['code']) || isset($_GET['id']))
 && ($a = isset($_GET['id']) ? $_GET['id'] : $_SESSION['code'])
 && isset($_POST['code']) && $a == $_POST['code']) {

Comments

0
if((!empty($_POST['code']) || !empty($_SESSION['code'])) && $_GET['id']<>$_POST['code']){

1 Comment

Please explain why your answer would be good, not only a line of code. See FAQ item 9
0

This could be

if(!empty($_SESSION['code']) && !empty($_GET['id']) && !empty($_POST['code'])
    ($_SESSION['code'] == $_POST['code'] || $_GET['id'] == $_POST['code']))

This means all the variables must be set and have value then it compares if the $_SESSION['code'] or $_GET['id'] is equal with the $_POST['code']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.