Just wondering about a simple improvement I want to make

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
gogglez
Forum Newbie
Posts: 2
Joined: Fri Oct 08, 2010 5:48 pm

Just wondering about a simple improvement I want to make

Post by gogglez »

I was sitting and looking at my code a second ago when it hit me that there must be a better way to write it, but I just don't know how to. I decided I'd ask a community of more experienced developers to see what kind of input you might have... For now I'm just glancing through docs to get an idea, but I'd love an outside opinion.

Like I said, I'm not great with php and I really only use it for very basic needs. But I'd like to move past that. Programming has become really fascinating to me lately.

Anyways, here's the code:

Code: Select all

<?php
	$dp = $_GET['dp'];
	echo ($dp == (c) ? $this->render( 'casualButtons' ) : '');
	echo ($dp == (s) ? $this->render( 'seriousButtons' ) : '');
	echo ($dp != (c) && $dp != (s) ? $this->render( 'buttons' ) : '');
?>
So yeah, it finds out what $dp is, then determines what to do accordingly using some ternary operators. There has to be a way to condense this though... It seems so redundant.

Thanks for any input!
loCode
Forum Newbie
Posts: 9
Joined: Fri Oct 08, 2010 2:24 am

Re: Just wondering about a simple improvement I want to make

Post by loCode »

pretty condensed already but, one fix may be:

Code: Select all

<?php
   $dp = $_GET['dp'];
   echo ($dp == (c) ? $this->render( 'casualButtons' ) : $this->render( 'buttons' ));
   echo ($dp == (s) ? $this->render( 'seriousButtons' ) : $this->render( 'buttons' ));
?>
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Just wondering about a simple improvement I want to make

Post by flying_circus »

If there are more than 2 possibilities, I typically use a switch statement. It always seems a bit more clear when I come back in a few months to make updates.

Code: Select all

<?php
# Validate $_GET['dp'] before we get here.

# Take action on $_GET['dp']
   switch($_GET['dp'])
  {
    case '(c)': $this->render('casualButtons');   break;
    case '(s)': $this->render('seriousButtons');  break;
    default:    $this->render('buttons');         break;
  }
?>
gogglez
Forum Newbie
Posts: 2
Joined: Fri Oct 08, 2010 5:48 pm

Re: Just wondering about a simple improvement I want to make

Post by gogglez »

loCode, your version of the code displays both the c or s value buttons, as well as the regular buttons. I'd need it to say 'if c or s, show c or s buttons, else show regular buttons'. Thanks though!

flying_circus, that's what I just noticed while looking around - It seems like a better alternative. It's slightly more characters of code, but it's far more... Clear? It seems more like the intended statement for what I'm trying to do. Thanks!

edit: the switch statement actually uses about 30 less characters (Not that it matters that much), so it is more efficient all around. Cool! That's exciting to learn.
loCode
Forum Newbie
Posts: 9
Joined: Fri Oct 08, 2010 2:24 am

Re: Just wondering about a simple improvement I want to make

Post by loCode »

I thought it might do something weird like that. I tried to rationalize it as one would superimpose the other, but not in this case.

I would also use the switch for readability, plus its easier to debug.

EDIT: And, you wont need to use *break;* in the default case. So, now minus another 6 chars from that total. ;)
Post Reply