cgm225 Posted March 22, 2008 Share Posted March 22, 2008 I am working through a tutorial here, and came across these lines of code:: $page = !empty($_GET["page"]) ? $_GET["page"] : "home"; $action = !empty($_GET["action"]) ? $_GET["action"] : "index"; What do they mean? I do not understand what that syntax means, particularly the question mark and colon with the trailing "home" or "index". Will someone explain this code to me in words? Link to comment https://forums.phpfreaks.com/topic/97361-will-someone-explain-this-code-to-me-in-words/ Share on other sites More sharing options...
kenrbnsn Posted March 22, 2008 Share Posted March 22, 2008 This is a Ternary Operator -- a sort of short-hand for if-else. The first <?php $page = !empty($_GET["page"]) ? $_GET["page"] : "home"; ?> is the same as <?php if (!empty($_GET['page'])) $page = $_GET['page']; else $page = 'home'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/97361-will-someone-explain-this-code-to-me-in-words/#findComment-498197 Share on other sites More sharing options...
Orio Posted March 22, 2008 Share Posted March 22, 2008 This is the ternary operator. Explanation in the manual: http://www.php.net/manual/en/language.operators.comparison.php (Scroll down a bit) Basically, this: $page = !empty($_GET["page"]) ? $_GET["page"] : "home"; Is the same as: if(!empty($_GET["page"])) $page = $_GET["page"]; else $page = "home"; Orio. Link to comment https://forums.phpfreaks.com/topic/97361-will-someone-explain-this-code-to-me-in-words/#findComment-498198 Share on other sites More sharing options...
cgm225 Posted March 22, 2008 Author Share Posted March 22, 2008 Thank you both! Link to comment https://forums.phpfreaks.com/topic/97361-will-someone-explain-this-code-to-me-in-words/#findComment-498199 Share on other sites More sharing options...
redarrow Posted March 22, 2008 Share Posted March 22, 2008 quick example <?php //line 1 set $a to redarrow // line 2 set name to $a redarrow // if(?) $a set echo redarrow else echo hello. //delete redarrow your get hello... $a="redarrow"; $name= $a ? $a : 'hello'; echo $name; ?> Link to comment https://forums.phpfreaks.com/topic/97361-will-someone-explain-this-code-to-me-in-words/#findComment-498211 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.