Solved

How to phrase php if statement as ternary

Posted on 2013-06-21
6
369 Views
Last Modified: 2013-06-22
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!
0
Comment
Question by:Jonathan Greenberg
6 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 39267599
try like this,

$get_invoice = $_GET['invoice'];
$invoice = (!empty($get_invoice)) ? $get_invoice : 1;
0
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 125 total points
ID: 39267600
From the example, it looks like you should do this:
$invoice = (!empty($_GET['invoice'] )) ? $_GET['invoice'] : 1;
                                  

Open in new window


See Ternary Operator on this page:
http://php.net/manual/en/language.operators.comparison.php
0
 
LVL 36

Assisted Solution

by:Loganathan Natarajan
Loganathan Natarajan earned 125 total points
ID: 39267601
This works fine without warning,

$invoice = (!empty($_GET['invoice'])) ? $_GET['invoice'] : 1;
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 58

Accepted Solution

by:
Gary earned 125 total points
ID: 39268063
Missing closing bracket
$invoice = !empty($_GET['invoice']) ? $_GET['invoice'] : 1;
                                  ^

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 39268087
The original example had a PHP parse error.  This tests out correctly.
http://www.laprbass.com/RAY_temp_foo.php?invoice=3

<?php // RAY_temp_foo.php
error_reporting(E_ALL);

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

Open in new window

Best to all, ~Ray
0
 

Author Comment

by:Jonathan Greenberg
ID: 39268396
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
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now