Link to home
Start Free TrialLog in
Avatar of Jonathan Greenberg
Jonathan GreenbergFlag for United States of America

asked on

How to phrase php if statement as ternary

I'm not even sure I'm saying this correctly, but I want to express the following php if statement as a ternary operator.  I'd be grateful if someone would show me how.

if (!empty($_GET['invoice'])) {
	$invoice = $_GET['invoice'];
}
else { $invoice = 1; }

Open in new window

I'm inclined to do this

$invoice = !empty($_GET['invoice'] ? $_GET['invoice'] : 1;

Open in new window

But that appears to be wrong.

Thanks!
Avatar of Loganathan Natarajan
Loganathan Natarajan
Flag of India image

try like this,

$get_invoice = $_GET['invoice'];
$invoice = (!empty($get_invoice)) ? $get_invoice : 1;
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jonathan Greenberg

ASKER

Thank you logu, Dave, Gary and Ray.  I appreciate all the responses!  It looks like the bottom line is, as Gary pointed out, I neglected to include a closing parenthesis.

And Ray, special thanks to you for pointing me to the php -l command line prompt!  I can see that as being very useful.

Regards,
Jon