Welcome to WebmasterWorld Guest from 54.163.68.15

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to write false $ REQUEST, please help

   
8:53 pm on Apr 5, 2011 (gmt 0)

5+ Year Member



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']) {
}
9:05 pm on Apr 5, 2011 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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
4:38 pm on Apr 6, 2011 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.
5:14 pm on Apr 6, 2011 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



>>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
6:28 am on Apr 7, 2011 (gmt 0)

5+ Year Member



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.
7:16 am on Apr 7, 2011 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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
7:49 am on Apr 7, 2011 (gmt 0)

5+ Year Member



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. :)