| How to write false $ REQUEST, please help
|
basketmen

msg:4292694 | 8:53 pm on Apr 5, 2011 (gmt 0) | This is if statement $_REQUEST if the language translated if ($_REQUEST['language']) { } |
| i need to set the opposite, if the language not translated, what its looks like, please help which one is correct? or no one of them if (!$_REQUEST['language']) { } |
| if ($_REQUEST!['language']) { } |
| if !($_REQUEST['language']) { } |
|
|
Matthew1980

msg:4292706 | 9:05 pm on Apr 5, 2011 (gmt 0) | Hi there BasketMen, Ok, not far off try this:- if ((!isset($_POST['language'])) || (empty($_POST['language']))){ } This states, if NOT set var name (OR) is empty var name You get the idea.. Only reason I changed it is because $_REQUEST is not an advisable way off accessing your data, if this is from a for either $_GET or $_POST will be done (I would guess as your example is working off post, just change to suit) there are a few ways to approach this, but understand this, if the $_POST/GET hasn't been given state, then that particular element WILL NOT EXIST - ideally you need to see if it isset() (has state) and is empty... Hope that makes sense. Cheers, MRb
|
rocknbil

msg:4293241 | 4:38 pm on Apr 6, 2011 (gmt 0) | if (! isset($_REQUEST['language']) or (isset($_REQUEST['language']) and empty($_REQUEST['language']))) { } or, as wisely pointed out, :-) if (! isset($_GET['language']) or (isset($_GET['language']) and empty($_GET['language']))) { } The thing about REQUEST is it contains both get and post. The more specific you are, the more secure you can be.
|
Matthew1980

msg:4293281 | 5:14 pm on Apr 6, 2011 (gmt 0) | >>The thing about REQUEST is it contains both get and post. The more specific you are, the more secure you can be. Amen to that - and nice addition there Rocknbil... Cheers, MRb
|
halfbrown

msg:4293578 | 6:28 am on Apr 7, 2011 (gmt 0) | Assuming that $_REQUEST['language'] actually has a value such as a boolean "true" or "false" (since you're looking to see if a language is set or not), I'd use this:
if (array_key_exists('language', $_REQUEST) && (false === $_REQUEST['language'])) { // STUFF GOES HERE } This makes sure that the array element actually exists (otherwise you'll have problems if it doesn't exist yet):
array_key_exists('language', $_REQUEST) And this makes sure that "language" is a boolean false:
(false === $_REQUEST['language']) It was mentioned that $_REQUEST contains both $_GET and $_POST (among others), which is true, but I'm hesitant to write a full-on set of code to handle that correctly. :) You can just replace what I suggested with $_GET or $_POST, as appropriate.
|
Matthew1980

msg:4293588 | 7:16 am on Apr 7, 2011 (gmt 0) | Hi there Halfbrown, Welcome to the forums! And this makes sure that "language" is a boolean false: (false === (boolean)$_POST['language']) |
| Your logic is sound, but as this data is being retrieved from either the URL or submitted form, the default data type being sent is string, so to use your logic, I would think that you would have to type hint/cast before checking for either true or false. But yes, array_key_exists() is another method in checking for set data/element. Then my preference is to use the call back function in array map:- $_POST['name'] = array_map('strip_tags', $_POST['name']); Very useful. Cheers, MRb
|
halfbrown

msg:4293594 | 7:49 am on Apr 7, 2011 (gmt 0) | Matthew1980 - Thanks! Long time lurker, first time poster. :) You're 100% right about the typecasting. I'm kicking myself for not checking for that. I should have stuck with the 0/1 I originally had in my preview & adjusted accordingly. Hehe. I hadn't thought about using array_map before. Interesting. I'll have to keep that in mind when I crack open Subversion at work tomorrow for this project I'm working on. I'm pretty persnickety about (not) throwing warnings or errors, so I like using array_key_exists beforehand in a case like this and shoot off an email, log entry, or *something* to let me know I've been silly and fixing it accordingly. Thanks again for the welcome. :)
|
|
|