Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to phrase php if statement as ternary

Posted on 2013-06-21
6
Medium Priority
?
383 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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 500 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 500 total points
ID: 39267601
This works fine without warning,

$invoice = (!empty($_GET['invoice'])) ? $_GET['invoice'] : 1;
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 58

Accepted Solution

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

Open in new window

0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

772 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