newbie alert: how to write this in a shorter way

This is a discussion on newbie alert: how to write this in a shorter way within the alt.comp.lang.php forums, part of the PHP Programming Forums category; hackajar@gmail.com wrote: > I was thinking about that when I woke up this morning ;) Good catch! > > ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #21 (permalink)  
Old 12-26-2006
Ken Robinson
 
Posts: n/a
Default Re: newbie alert: how to write this in a shorter way

hackajar@gmail.com wrote:
> I was thinking about that when I woke up this morning ;) Good catch!
>
> Maybe this would be more creative (not to complicate this even more;):
> <?php
>
> function checkA($a) {
> switch($a) {
> case 1:return true; break;
> case 2:return true; break;
> case 3:return true; break;
> case 4:return true; break;
> case 17:return true; break;
> case 30:return true; break;
> }
> return false;
> }
>
> if(checkA($a))echo "bingo!";
> ?>


If you're going to make this into a switch statement, group the "true"
values together:

<?php
function checkA($a) {
$retVal = false;
switch($a) {
case 1:
case 2:
case 3:
case 4:
case 17:
case 30:
$retVal = true;
break;
}
return ($retVal);
}

if (checkA($a)) echo 'Ok!';
?>

Ken
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:32 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0