Nick20 Posted August 20, 2012 Share Posted August 20, 2012 Hey people! I've just come across something when using Larry's config script in PHP Advanced. Now I have the error handler running, it flags up all these undefined variable errors wherever I have this type of thing: $content = $_GET['var'] is says that $_GET['var'] isn't defined. I can get round it like this: if(isset($_GET['z_'])){ $content = $_GET['z_']; } else { $content = FALSE; } But seems like a really long way around it do to every time I want to rename a GET variable. I have tried sticking it all in a function, but of course, when I pass the GET function into the function, I get the same error. I also seem to need an ELSE, otherwise, I get undefined errors on $content. Is there a better way round this?? CHeers, Nick Link to comment Share on other sites More sharing options...
Antonio Conte Posted August 20, 2012 Share Posted August 20, 2012 You can use a shorter syntax called the ternary operator. It has three parts. The expression, the true value and the false value. The expression is the if-statement, the value/variable after the question mark is used if the expression is true, and the value/variable after ':' is used when the expression is false. // Basic $var = isset($_GET['var']) ? $_GET['var'] : false; // Validation is also fine $var = isset($_GET['var']) ? mysqli_real_escape_string($_GET['var']) : false; // You can use several expressions too $var = ( isset($_GET['var']) && is_numeric($_GET['var']) ) ? 'This is a numeric value' : 'Not a numeric value'; You can even develop a function for this. Let's take an example with Integers. $var = get_intval($_GET['var']; // Returns value if it's not null, else false function get_intval( $var ) { return ( $var != null && is_numeric($var) ) ? (int) $var : false; } 2 Link to comment Share on other sites More sharing options...
Nick20 Posted August 20, 2012 Author Share Posted August 20, 2012 Thanks Antonio - great tip! Job done Link to comment Share on other sites More sharing options...
Recommended Posts