Jump to content

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

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.