Solved

How to phrase php if statement as ternary

Posted on 2013-06-21
6
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 83

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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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 110

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

751 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