1

I am trying to do a query like:

If $_GET['page'] == 'items' AND $_GET['action'] == 'new' OR 'edit'

Here's what I have:

if (isset($_GET['page']) && $_GET['page'] == 'items') {
    if (isset($_GET['action']) && $_GET['action'] == 'new' || isset($_GET['action']) && $_GET['action'] == 'edit') {

        // This is what Im looking for

    }
}

Is this correct, and is this the easiest way to make this query?

4
  • Well, it's a perfect valid way… Commented Jun 25, 2012 at 14:02
  • Yes, and any other developer will understand what your doing because your names are on point. Commented Jun 25, 2012 at 14:03
  • When you have more "OR" statements you could do it with a switch inside the first if statement. Commented Jun 25, 2012 at 14:03
  • @OP you already had the right solution in your own wording :) Your second line just needed brackets () at the right places and you could have pasted it straight into your code as for php OR and AND are synonyms for || and && ;) Commented Jun 25, 2012 at 14:17

5 Answers 5

3

You could have done it like this as well:

if (isset($_GET['page']) && $_GET['page'] == 'items') {
    if (isset($_GET['action']) && ($_GET['action'] == 'new' || $_GET['action'] == 'edit')) {

    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

This does not check for the page…
I meant it for the second if of course :) (I edited it, so it's more clear.)
+1 Correct A && B || A && C equals A && (B || C). BTW the OP already had it in words :)
Id still suggest checking that both page and action are set before bothering to check what their values are. As I said in my answer there is no point in checking the value of page if you then find out that action is not set.
@Jon Taylor Well, you could have a default action if none is given but a page is selected: Magento for example uses an action named "index" of none was given.
|
2

Your way is perfectly fine, although I would almost be tempted to do it the following way. The only reason I suggest this is that your code requires that both action and page are set. If action is not set then there isn't much point checking if the page is == 'items'.

if(isset($_GET['page']) && isset($_GET['action'])) {
    if($_GET['page'] == 'items' && ($_GET['action'] == 'new' || $_GET['action'] == 'edit')) {
        //do code here
     }
}

2 Comments

You can do isset($_GET['page'], $_GET['action']) in one shot as well ;)
Ah you learn something new everyday, I did not realise this. Thanks.
1

You may also try in_array like:

if (isset($_GET['page']) && $_GET['page'] == 'items')
{
    if ( !empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'new', 'edit' ) )
    {
        // This is what Im looking for
    }
}

Comments

0

That is one of possible solutions

if ( @$_GET['page'] == 'items' && in_array(@$_GET['action'], array('new','edit')))

2 Comments

Im not sure I would encourage this, although it will work it could produce errors later on. @ just supresses errors and so rather than error handling you are just ignoring (which in my experience is bad).
I used it just to shorten the code. And yes, using @ is not a good practice.
0

Everything is ok, but also you can use function:

function paramIs($param, $values) {
    $result = false;
    foreach ((array)$values as $value) {
        $result = $result || isset($_GET[$param]) && $_GET[$param] == $value;
    }
    return $result;
}

Usage:

if (paramIs('page', 'items') && paramIs('action', array('new', 'edit')))
{
    // your code here
}

It will reduce the number of repetitions in your code and encapsulate logic in one place

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.