Posts

Showing posts from November, 2015

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 the loop and show the output from 1 to 3 and we will get increment gap 1. But here we also want to get the total sum of the loop. So at first of all on the upper of loop we have declared a initial variable named $sum. And which initial value is 0. And the value will increase with each rotation of loop. After end of the loop we will ec

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>

To get even and odd numbers from a loop by using function

<?php     function evenOdd($start,$end,$type){         for($i=$start; $i<=$end; $i++){             if($type == "even" && $i%2==0){                 echo "<pre>";                 echo $i;             }elseif($type == "odd" && $i%2!=0){                 echo "<pre>";                 echo $i;             }         }     }     evenOdd(10,20,"even"); ?> Here we have created a function named evenOdd(); this is user defined function which have been created. There are three parameters in this function. So total three values will pass through this function. Loop will start from 10 and will be end to 20 and in string part if you write even you will just get even numbers and if you write odd you will get only odd numbers.

% (Modulus)

$i = 10%7; Output = 3. $j = 10%3; Output = 1. '%' This is an arithmetic operator in php which is named by modulus. This is use for get the modulus values. Suppose you have coded 10%3. So what will happen here ?? We know 10 will be divided by 3 total three times and remain 1 as modulus value. 

array_merge(array1); More than one arrays value get together

array_merge(array1,array2,array3); this function is use for merge multiple array together. Concept will be clear with an example. <?php     $arr1 = array("Ball","Bat","Stamp");     $arr2 = array("CPU","Monitor","Keyboard");     echo "<pre>";     print_r(array_merge($arr1,$arr2)); ?> There is two array $arr1 and $arr2. If we want to get those arrays values together we have to use array_merge(); function like that. And you will get all values together.

array_values(input); To get all values from an array

array_values(); This function is use to get all values from a defined variable. The concept will be more clear if we look at an example. <?php     $val = array("car"=>"BMW","pen"=>"Econo","brand"=>"Dell");         echo "<pre>";         print_r(array_values($val)); ?> Here $val variable is an array where stored some values and keys. So now if we just want to print only values on screen. What is the solution ?? Yes... There is a solution by array_values(); function. If we input the variable in the function we will get the output. Then only values from the array will get out as output.

array_keys(input); To get all keys from an array

array_keys(); This function is use to get all keys from a defined variable. The concept will be more clear if we look at an example. <?php     $var = array("car"=>"BMW","pen"=>"Econo","brand"=>"Dell");         echo "<pre>";         print_r(array_keys($var)); ?> Here $var variable is an array where stored some values and keys. So now if we just want to print only keys on screen. What is the solution ?? Yes... There is a solution by array_keys(); function. If we input the variable in the function we will get the output. Then only keys from the array will get out as output.

array_key_exists(key, search)

array_key_exists(); this function is used to justify key. For the clear concept codes are given below. <?php     $k = array("car"=>"BMW","pen"=>"Econo","brand"=>"Dell");         if(array_key_exists("car", $k)){             echo "This is a key or index";         }else{             echo "This is not any key";         } ?> So the basic concept is this function is used for check your given keyword from your defined array that it's key or not. Here inside the first bracket the first argument will be your given keyword which is key. And the second argument will be arrays name which you have defined as variable.

Case sensitivity in php to defining variable

$hasib = "all words are small letters"; echo $Hasib; You will get an error message cause though variables names are same but you have stored value on $hasib; So what is the different here ?? The major different is $Hasib variable started with capital lertter and $hasib with small letter but those are not same variable although all letters are same in both variable. So careful when you define variable.

Different between two types of array

Array define types      Type A)            $arr[] = "value1";            $arr[] = "value2";      Type B)            $arr = array("value1","value2"); Given up the two types of defining array. When you will define 'A' type array and you will use sort(); function to arrange values by ascending order then only values will be arranged by ascending order not keys. But if you apply sort(); function in 'B' type array you will see that not only array but also keys are being arranged by ascending order by sort(); function. And like that rsort(); function will do the same job. rsort(); function will just arrange by descending order. So if you want to arrange keys or index by ascending order in Type 'A' you have to use the ksort(); function for ascending order and krsort(); function for descending order. But if you use these function on Type 'B' you will see that values and keys are arranging by ascending order or de

array_count_values($var);

print_r(array_count_values($var)); array_count_values(); function is use for count how many common values in an array ?? To clearing the concept suppose you have an array where you have stored your friends names. And you have one name of three friends suppose the name is Aiken. Here array_count_values($friends) will return you [Aiken] => 3 cause this function have found three same values in $friends array. So we able to understand to count the same or common values in an array. 

array_combine();

array_combine(); function is used for make a combination between two arrays. Practical example is similarly  like you have defied two arrays suppose first one is arr1 and 2nd one is arr2. So there is two array arr1 and arr2. So what is gonna be happen next ?? We have to take another variable to understand easily. Suppose we have taken a variable called $comb and in this variable we will code like that array_combine($arr1,$arr2); . So according to array_combine() function first argument is stored as key and second argument as value. Now it's time to print array and we know the code of printing array is print_r();   

array_chunk($var,2);

print_r(array_chunk($var,2)); array_chunk(); function is used for divide the main array into coder requirement. According to practical thinking you can imagine there is a variable and this is array which has total ten values. If you want to print couple of values in per chunk you have to code array_chunk($var,2); function into print_r(); function. Then you will see there will be create five chunk and each chunk is containing two values. But if there would be total 11 values you would get six chunks and in the final chunk will contain the rest of value that means the last chunk will contain only one value. An example coding is given below. <?php     $cnk = array("value1","Value2","Value3","Value4","Value5","Value6","Value7");     echo "<pre>";     print_r(array_chunk($cnk, 2)); ?>

is_array($var);

is_array($var); This function is used to check the variable that it's array or not. If it's an array then this function will return you '1' as output and if it isn't then you will never get any return or out put form this function. So, the basic concept is this function is used to verify variable that it's array or not. 

PHP Array with explanation

<?php  $ar1[] = 10; // index 0 value 10 $ar1[] = 20; // index 1 value 20 echo '<pre>'; // <pre> use to arrangement print of array print_r($ar1); // print_r(); to print array with value and index echo $ar1[0]; // to print the defined array $ar2[1] = 10; // coder define key or index for array $ar2[3] = 20; // her coder define key is 3 print_r($ar2); // to print array $ar3 = array("a","b","c"); // This array define structure is to use contain multiple value in single variable print_r($ar3); // print_r(); to print array $ar4 = array(    // User define numerical & associative key together in an array     0=>"a", // user define numerical key     "z"=>"b", // user define string key     3=>"c" // user define numerical key     ); print_r($ar4);    // print_r(); to print array echo $ar4[3];    // to echo user define index '3' echo '<br/>'; echo '<br/>'; $

? Ternary Operator

[?] this question symbol is ternary operator in php.

PHP include & require

> include 'page.php'; > include_once 'page.php'; > require('page.php'); > require_once('page.php'); These functions are use for include pages from directory. Here include and require() does same job and the other hand include_once and require_once() also does the slimier action. When you have to include same page many time, then how many times you will code [include 'page.php';] you will get result. Such as if you code [include 'page.php';] [include 'page.php';] [include 'page.php';] three times include or only require('page.php'); function then your code will show the [page.php] three times in your coded div though the page is same. But when you will use [include_once ''] or [require_once('')] for same page you will never get your called page more than one time. When you are coding then error happening is simple. The major different between include and require() function is, if php