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
https://forums.phpfreaks.com/topic/207472-ordering-methods/
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
https://forums.phpfreaks.com/topic/207472-ordering-methods/#findComment-1084718
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
https://forums.phpfreaks.com/topic/207472-ordering-methods/#findComment-1084852
Share on other sites

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.