Hey fellas,
My prof gave us a php example in class and I'm having some trouble understanding a peice of code:

isset($_POST["item"])? $item = $_POST["item"]:$item = "Not Set";
isset($_POST["quantity"])? $quantity = $_POST["quantity"]:$quantity = "0";

The problems:
Does isset just check if "item" is equal to something?
The format of the code is also giving me some trouble. I think this is some sort if statement santax, however, I'm not too sure on how to break down the code. How would I write it in a if-statement format?

Any help would be greatly appriciated.

Edited by milkman93

4 Years
Discussion Span
Last Post by jkon

isset is checking if a variable has been declared or exists and is not NULL.

If you did something like

 // do something here

It is saying, if $variable is set and not NULL do something.


The example your professor gave you is pretty much the same but in a short hand version.

isset($_POST["item"])? $item = $_POST["item"]:$item = "Not Set";

If $_POST['item'] has been set and is not null then set variable $item to equal $_POST['item'] else set $item to equal "Not Set".


Of course it is just my opinion but:
isset($_POST["item"])? $item = $_POST["item"]:$item = "Not Set";
Just mean bad and messy code, there is no reason why the same can’t be written cleaner e.g.:
$item = isset($_POST["item"]) ? $_POST["item"] : "Not Set";

Edited by jkon

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.