Posts

Sum values in foreach loop _php

http://stackoverflow.com/a/16535758/5530859 $sum = 0;     foreach($group as $key=>$value)     {        $sum+= $value;     }     echo $sum;

SelectOption list with link by javascript

        <select onchange="window.location=this.value">             <option value="http://www.google.com">Google</option>             <option  value="http://www.yahoo.com">yahoo</option>             <option  value="http://www.twitter.com">Twitter</option>         </select>

Image size output by php

<?php   echo number_format($_FILES['photo']['size']/1024/1024,4,'.',''); ?> Here is the format to view the file size. At the beginning file size will shown as byte when we will write the code as follows, echo number_format($_FILES['photo']['size']); We know 1024 byte = 1kb. So if we want to represent the size as kilobyte format we have to divide the size by 1024 so the code will be like that, echo number_format($_FILES['photo']['size']/1024); But if we want to show the file size as megabyte format we have to divide this again by 1024 cause we know 1024kb = 1mb. And the coding will be like that, echo number_format($_FILES['photo']['size']/1024/1024); There is a problem if you divide 100/1024 that means if you divide 100kb by 1024kb to convert megabyte you will get out put 0. Cause after point values are not printing. So what is the solution to get the values after point. Here is your code given...

Generate options by using user defined function

<?php   function generateOptions($start,$end,$inc){     $options = '';     for($i=$start; $i<=$end; $i+=$inc){        $options = $options."<option>".$i."</option>";     }     return $options;      }  ?> <select name="year">   <option>Year</option>   <?php   echo generateOptions(1900,date('Y')-10,1);   ?> </select>  

php Loop

<?php //For Loop for($i=0; $i<=12; $i++){   echo "For Loop : ".$i."<br>"; } //While Loop $j = 30;   while($j<=40){     echo "While Loop : ";     echo $j++;     echo "<br>";   } //Do While Loop   $k = 20;   do{     echo "Do While Loop : ";     echo $k++;     echo "<br>";   }while($k<=50)   ?>

Calculate total sum of all values from loop

<?php     function calsum($start,$end,$inc){         $sum = 0;         for($i=$start; $i<=$end; $i+=$inc){             echo "<pre>";             echo $i;             $sum = $sum + $i;             echo "<br>";         }         echo $sum;     }         calsum(1,3,1); ?> There is an user defined function called calsum(); In this this function total three parameters has been taken. We will run a loop in this function. User's input will be the start value, end value, and the increment value. Here user want to start loop from 1 and want to end to 3 and the increment gap will be one. So we will run th...

Generate function for date by loop in form

<?php   function generateOptions($start,$end,$inc){     $options = '';     for($i=$start; $i<=$end; $i+=$inc){       $options = $options.'<option>'.$i.'</option>';     }       return $options;   } ?>        <select>           <option>Day</option>             <?php echo generateOptions(1,31,1); ?>         </select>