Page 1 of 1
Easier IF Statments
Posted: Mon Mar 05, 2007 5:31 pm
by tecktalkcm0391
Is there anyway to rewrite this to something simplier:
Code: Select all
if(isset($_POST['action'])){
$action = stripeslashes($_POST['action']);
} elseif(isset($_GET['action'])){ {
$action = stripeslashes($_GET['action']);
}
Posted: Mon Mar 05, 2007 5:34 pm
by RobertGonzalez
Code: Select all
<?php
if (isset($_POST['action']) || isset($_GET['action'])) {
$action = isset($_POST['action']) ? stripslashes($_POST['action']) : stripslashes($_GET['action']);
}
?>
Posted: Mon Mar 05, 2007 5:42 pm
by tecktalkcm0391
Thanks, but whats the template for it...is it
Code: Select all
$variable = if_this ? do_this : otherwise_do_this;
Also, what would this be simplier:
Code: Select all
if((is_numeric($_POST['id']) || is_numeric($_GET['id'])) && (isset($_POST['id']) || isset($_GET['id'])){
if(isset($_POST['id'])){
$id= stripslashes($_POST['id']);
} elseif(isset($_GET['id'])){ {
$id= stripslashes($_GET['id']);
}
} else {
$id = '';
$action = '';
}
EDIT: Nevermind I got the above code, but the is the format:
Code: Select all
$variable = if_this ? do_this : otherwise_do_this;
Re: Easier IF Statments
Posted: Mon Mar 05, 2007 6:55 pm
by WaldoMonster
tecktalkcm0391 wrote:Is there anyway to rewrite this to something simplier:
Code: Select all
if(isset($_POST['action'])){
$action = stripeslashes($_POST['action']);
} elseif(isset($_GET['action'])){ {
$action = stripeslashes($_GET['action']);
}
I think your example is strait forward.
But it is possible top compress it to something like this:
Code: Select all
$action = @$_GET['action'] or $action = @$_POST['action'] or $action = '';
if (get_magic_quotes_gpc()) $action = stripslashes($action);
But I would make a function that can be called everywhere in your script.
Posted: Mon Mar 05, 2007 6:59 pm
by RobertGonzalez
Yes, the ternaray operator is
@WaldoMonster: Why the exhaustive use of error suppression?
Posted: Mon Mar 05, 2007 7:12 pm
by WaldoMonster
@WaldoMonster: Why the exhaustive use of error suppression?
If an error occurs it will return a false and go to the next one.
It is just an example that other constructions are also possible.
Posted: Mon Mar 05, 2007 9:21 pm
by feyd
The error suppressors slow the script down. Proper handling is better and faster.
Posted: Mon Mar 05, 2007 10:19 pm
by RobertGonzalez
WaldoMonster wrote:@WaldoMonster: Why the exhaustive use of error suppression?
If an error occurs it will return a false and go to the next one.
It is just an example that other constructions are also possible.
So scripts that generate errors are ok to ignore?
This is just my opinion, but it is cleaner coding, more efficient and is more proper to handle errors rather than ignore them. It is my suggestion to not shut up errors but instead handle the situations that would cause the error.
And FYI, the error still fires... you just don't see it.

Posted: Tue Mar 06, 2007 12:43 am
by Christopher
I think the answer to the original post is -- no. I think the initial code posted is the clearest and cleanest.
Re: Easier IF Statments
Posted: Tue Mar 06, 2007 3:40 am
by onion2k
WaldoMonster wrote:Code: Select all
$action = @$_GET['action'] or $action = @$_POST['action'] or $action = '';
That's completely unreadable code. It's really not obvious at all what's happening. If I saw that in a script I'd rewrite it.
Posted: Tue Mar 06, 2007 4:49 am
by Kieran Huggins
I felt left out...
Code: Select all
$action = @$_REQUEST['action'] ? @$_REQUEST['action'] : '';
Posted: Tue Mar 06, 2007 6:48 am
by WaldoMonster
feyd wrote:The error suppressors slow the script down. Proper handling is better and faster.
I didn't know that suppressing an error slows down a script.
Good to know.
Posted: Tue Mar 06, 2007 9:33 am
by Kieran Huggins
I didn't think supressing errors did - I know throwing warnings slows you down like a mofo, though!
Posted: Tue Mar 06, 2007 9:46 am
by feyd
The slow down is fairly minor, but often where there's one error suppression, there's fifty more waiting behind it.