Jump to content

php hacks


Monkuar

Recommended Posts

$orderby = (isset($_GET['@amount'])) ? 'amount' : 'date_time';

 

I heard using ? and : is a hack and

 

? stands for a if function ~ and : stands for else?

 

Can u tell me what this is called is there other ways to do this like this?

How can I add more to this code too

$orderby = (isset($_GET['@amount'])) ? 'amount' : 'date_time' :'id';

 

like see If I add the : 'id' to it i get a syntax error, unexpected ':', possible ?thanks phpfreaks!

 

Link to comment
https://forums.phpfreaks.com/topic/164368-php-hacks/
Share on other sites

It's called a ternary operator.

 

You can find out more about it in the manual

 

Don't know exactly what you're trying to do here

$orderby = (isset($_GET['@amount'])) ? 'amount' : 'date_time' :'id';

But when you have two colons, by your own logic, this would equate to:

if (isset($_GET['@amount'])) {
   $orderby = 'amount';
} else {
   $orderby = 'date_time'
} else {
   $orderby = 'id';
}

which isn't valid PHP at all

Link to comment
https://forums.phpfreaks.com/topic/164368-php-hacks/#findComment-867071
Share on other sites

It's called a ternary operator.

 

You can find out more about it in the manual

 

Don't know exactly what you're trying to do here

$orderby = (isset($_GET['@amount'])) ? 'amount' : 'date_time' :'id';

But when you have two colons, by your own logic, this would equate to:

if (isset($_GET['@amount'])) {
   $orderby = 'amount';
} else {
   $orderby = 'date_time'
} else {
   $orderby = 'id';
}

which isn't valid PHP at all

 

Would i need to put else if in therE?

Link to comment
https://forums.phpfreaks.com/topic/164368-php-hacks/#findComment-867441
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.