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.
Thanks

Recommended Answers

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

If you did something like

if(isset($variable))
{
 // do something here
}

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

Jump to Post

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".

Jump to Post

All 5 Replies

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

If you did something like

if(isset($variable))
{
 // 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".

WOW, That's a lot of good information! Thanks a lot brother!

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";

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.