PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Mon Mar 05, 2007 5:31 pm
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']);
}
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Mon Mar 05, 2007 5:34 pm
Code: Select all
<?php
if (isset($_POST['action']) || isset($_GET['action'])) {
$action = isset($_POST['action']) ? stripslashes($_POST['action']) : stripslashes($_GET['action']);
}
?>
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Mon Mar 05, 2007 5:42 pm
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;
WaldoMonster
Forum Contributor
Posts: 225 Joined: Mon Apr 19, 2004 6:19 pm
Contact:
Post
by WaldoMonster » Mon Mar 05, 2007 6:55 pm
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.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Mon Mar 05, 2007 6:59 pm
Yes, the ternaray operator is
@WaldoMonster: Why the exhaustive use of error suppression?
WaldoMonster
Forum Contributor
Posts: 225 Joined: Mon Apr 19, 2004 6:19 pm
Contact:
Post
by WaldoMonster » Mon Mar 05, 2007 7:12 pm
@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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Mar 05, 2007 9:21 pm
The error suppressors slow the script down. Proper handling is better and faster.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Mon Mar 05, 2007 10:19 pm
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.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Tue Mar 06, 2007 12:43 am
I think the answer to the original post is -- no. I think the initial code posted is the clearest and cleanest.
(#10850)
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Tue Mar 06, 2007 3:40 am
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.
Kieran Huggins
DevNet Master
Posts: 3635 Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:
Post
by Kieran Huggins » Tue Mar 06, 2007 4:49 am
I felt left out...
Code: Select all
$action = @$_REQUEST['action'] ? @$_REQUEST['action'] : '';
WaldoMonster
Forum Contributor
Posts: 225 Joined: Mon Apr 19, 2004 6:19 pm
Contact:
Post
by WaldoMonster » Tue Mar 06, 2007 6:48 am
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.
Kieran Huggins
DevNet Master
Posts: 3635 Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:
Post
by Kieran Huggins » Tue Mar 06, 2007 9:33 am
I didn't think supressing errors did - I know throwing warnings slows you down like a mofo, though!
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Mar 06, 2007 9:46 am
The slow down is fairly minor, but often where there's one error suppression, there's fifty more waiting behind it.