Easier IF Statments

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

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Easier IF Statments

Post 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']);
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
if (isset($_POST['action']) || isset($_GET['action'])) {
      $action = isset($_POST['action']) ? stripslashes($_POST['action']) : stripslashes($_GET['action']);
}
?>
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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;
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Re: Easier IF Statments

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Yes, the ternaray operator is

Code: Select all

if_this ? do_this : do_that
@WaldoMonster: Why the exhaustive use of error suppression?
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The error suppressors slow the script down. Proper handling is better and faster.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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. :wink:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I think the answer to the original post is -- no. I think the initial code posted is the clearest and cleanest.
(#10850)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Easier IF Statments

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I felt left out...

Code: Select all

$action = @$_REQUEST['action'] ? @$_REQUEST['action'] : '';
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I didn't think supressing errors did - I know throwing warnings slows you down like a mofo, though!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The slow down is fairly minor, but often where there's one error suppression, there's fifty more waiting behind it.
Post Reply