PDA

View Full Version : help with breaking down isset statement



Jacquih
08-12-2006, 04:26 AM
I'm not sure what this statement is supposed to do:


$action = (isset($_GET['action']) && $_GET['action'] != '') ? $_GET['action'] : 'view';

Immediately after that there is a switch function:


switch ($action) {
case 'add' :
addToCart();
break;
case 'update' :
updateCart();
break;
case 'delete' :
deleteFromCart();
break;
case 'view' :

What does the isset statement do here?

Thanks for any help!

Twey
08-12-2006, 09:43 AM
a ? b : cis read as
if(a) b; else c;although the end result is slightly different, because it can be used in expressions.
It checks to see if $_GET['action'] exists and has content. If so, it sets $action to it. Otherwise, it sets $action to "view".

mwinter
08-12-2006, 09:56 AM
I'm not sure what this statement is supposed to do:



$action = (isset($_GET['action']) && $_GET['action'] != '') ? $_GET['action'] : 'view';


As you should be aware, the $_GET superglobal (associative) array contains name/value pairs present in the query string, and attempting to access an array element that doesn't exist generates a warning.

The isset function checks to see if a given element has been set to a value, returning true if it has. The following expression checks to make sure that the value isn't an empty string. Finally, the conditional expression (the ? : operator) evaluates to the value of $_GET['action'] if the other two expressions evaluate to true, or the string 'view' otherwise.

Hope that helps,
Mike

Jacquih
08-15-2006, 02:18 AM
It helps alot! Thanks guys! :D