Posts

Showing posts with the label for loop

print triangle of star using php

1) first loop considering $i as row and $j as column ,where total number of row is 4 and column is 7. 2) second is print * as passing the condition as following. CODE: for($i=1;$i<=4;$i++){     for($j=1;$j<=7;$j++){             if($i==1 && $j==4)             {                 echo "*";             } else if(($j==3 || $j==5)&& $i==2 )             {                 echo "*";             }else if(($j==2||$j==6)&&$i==3)             {                 echo "*";             } else if(($j==1||$j==7)&&$i==4){                 echo "*";         ...

find second highest value in array using php

1) First step is to sort the array in desc. 2) create two variable  like $fh as first highest and $sh as second highest. 3) loop the array in foreach ,we will check the if condition to first val is greater then $fh variable ,where first iteration first highest value will be assigned to $fh value. 4) second  else if condition will be same ,but here variable will be $sh and another condition in is check duplicate value of highest in the array. $arr = [55,40,50,76,76,40,30,20,60,89]; rsort($arr); $fh=0; $sh=0; foreach($arr as $val){     if($val > $fh){         $fh = $val;     } elseif(($val>$sh) && ($val != $fh)){         $sh = $val;     }   } echo $sh;