Better way to write this????

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
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Better way to write this????

Post by neophyte »

Code: Select all

<?php
 if ( $temp['type'] == "input")
						 {
					   		 $label = "";
						}	 
						elseif ( $temp['type'] == "submit" )
						{
							$label ="";
						}
						 elseif ( $temp['type'] == "reset" )
						 {
						 	$label="";
						 }
						 
						else
						{
							$label = $prepkey;  
						}		  
?>
Isn't there a better way to do this?

You know like maybe:

Code: Select all

<?php
 if ( $temp['type'] == ("input" || "submit" || "reset"))
{
$lablel="";
}
else
{
$label = $prepkey;  
}
?>
is that a correct way of re-writing it? If not can some one suggest a tighter more compact or simpler way of coding that? Thanks
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Maybe:

Code: Select all

switch($temp['type']){
   case 'input':
   case 'submit':
   case 'reset':
      $label = "";
      break;
   case default:
      $label = $prepkey;
}
You would have to test to see what was fastest.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i probably would do it with a switch

Code: Select all

switch ($temp&#1111;'type']) {
      case "input":
      case "submit":
      case "reset":
           $result = "";
            break;
       default:
            $result = $prepkey;
}
or with in_array

Code: Select all

$values = array('submit', 'input', 'reset');

if (in_array($temp['type'], $values)
{
        $result = "";
}
else
{
        $result = $prepkey;
}
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Great guys thanks for the suggestions! That oughta do it.
Post Reply