Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not grasping this use of ternary operators

Status
Not open for further replies.

jswannabe

Programmer
Aug 10, 2007
8
US
I am very new to php (but not to programming) and have purchased an open source code shopping cart that I am trying to dissect.

I've run into the following section of code which deals with several variables and also uses ternary operators - problem is I not figuring out what is happening. Here's one of the code snippets:

Code:
if (isset($_GET["order_without_billing_address"]) || isset($_POST["order_without_billing_address"]))
		$order_without_billing_address = isset($_GET["order_without_billing_address"])?
				$_GET["order_without_billing_address"]:$_POST["order_without_billing_address"];

Can someone explain in layman's terms what I've got going on here? Help Appreciated!

Thanks!!!
 
ternary is just if true then something else someother thing

so essentially in pseudo code
Code:
if 
  there is a query variable called order_without_billing_address
  OR
  there is a form variable with the same name
  THEN
  assign the variable $order_without_billing_address to the query var (if is set) or the POST var (if not).

BUT ... this code is really really bad. very sloppy coding style.

it would have been better to use this code

Code:
$order_without_billing = (!empty($_GET['order_without_billing'] ? $_GET['order_without_billing] : (!empty($_POST['order_without_billing'] ? $_POST['order_without_billing'] : FALSE);
or even
Code:
if (empty($_POST['order_without_billing)){
 if (empty($_GET['order_without_billing'])){
   $order_without_billing = false;
 } else {
   $order_without_billing = $_GET['order_without_billing'];
 }
} else {
  $order_without_billing = $_POST['order_without_billing']
}

or even more adventurous
Code:
import_request_variables('pg', 'tmp_');
$var = 'order_without_billing';
$$var = (isset(${tmp_$var})) ? ${tmp_$var} : false;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top