Jump to content

Ordering methods


Mr Nick

Recommended Posts

This isn't really a problem, I'm just looking for the best way to do this.

I have a simple array that allows the different types of orders. What would be the best way to check and execute them? I've written a few and am wondering which would be the best to use.

$orders = array("login", "gm", "email");

//Method 1
if(isset($_GET['order']) && in_array($_GET['order'], $orders)){
$order = $_GET['order'];
} else {
$order = 'login';
}

//Method 2
$order = isset($_GET['sort']) ? in_array($_GET['sort'], $orders) ? $_GET['sort'] : "login" : "login";

//Method 3
$order = isset($_GET['sort']) ? $_GET['sort'] : "login";
$order = in_array($order, $orders) ? $order : "login";

I'll be using the same method to check for ASC and DESC.

(This is sort of a MySQL question, but more on the php side so I'm putting it here).

Link to comment
Share on other sites

Using ? and : is basically the same as using the if command. Although it depends what you're trying to do.

I'll post a code using if first.

$a = 0;
$b = 0;
$c = "";
if($a == $b){
$c = "A equals B";
} else {
$c = "A does not equal B";
}

$a = 0;
$b = 0;
$c = "";
$c = $a == $b ? "A equals B" : "A does not equal B";

 

Anyways, I've already solved this :P

Link to comment
Share on other sites

Using ? and : is basically the same as using the if command. Although it depends what you're trying to do.

I'll post a code using if first.

$a = 0;
$b = 0;
$c = "";
if($a == $b){
$c = "A equals B";
} else {
$c = "A does not equal B";
}

$a = 0;
$b = 0;
$c = "";
$c = $a == $b ? "A equals B" : "A does not equal B";

 

Anyways, I've already solved this :P

 

I'm well aware of the ternary operator, however as I said I find using an if statement improves readability. Depends what your going for.

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.