Jump to content

if to loop?


karimali831

Recommended Posts

Hey can someone use their intelligence and teach me how to change the below if statements to a loop?

 

       if($h_rows) {
           $rl_rows = 8;
       }
       elseif($g_rows) {
           $rl_rows = 7;
       }
       elseif($f_rows) {
           $rl_rows = 6;
       }
       elseif($e_rows) {
           $rl_rows = 5;
       }
       elseif($d_rows) {
           $rl_rows = 4;
       }
       elseif($c_rows) {
           $rl_rows = 3;
       }
       elseif($b_rows) {
           $rl_rows = 2;
       }
       elseif($a_rows) {
           $rl_rows = 1;
       }

 

Thanks  8)

Link to comment
https://forums.phpfreaks.com/topic/260952-if-to-loop/
Share on other sites

Here you go:

<?php
for($i=8; $i>0; $i--){
if(${chr($i+96).'_rows'}){
	$rl_rows = $i;
	break;
}
}
?>

 

It does exactly what you want, but it's really bad coding, not because of what I did, but because of what you wanted. Should probably have been an array in the first place. If they are all set on the server side, and they are all booleans, I guess its okay, but else you probably want to use isset.

Link to comment
https://forums.phpfreaks.com/topic/260952-if-to-loop/#findComment-1337418
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.