Posts

Sum Value in php

 <?php  $sum = 1123;  $num =0;  while($sum>=1){    $rem = $sum%10;  $num = $num+$rem;  $sum = $sum/10;     }    echo $num;   ?>

Make a triangle in php

 <!DOCTYPE html> <html> <body> <?php for($i=1; $i<4;$i++){  for($j=4;$j>=$i;$j--){   echo "-";  }   for($k=1;$k<=$i;$k++){    echo "*";   }    echo "<br/>"; } ?> </body> </html>

Second Highest value form array in php foreach loop

 <?php $value = [12,14,15,55,6,67]; $max=0; $max2=0; foreach($value as $val){     if($val > $max){       $max2 = $max;       $max = $val;     }elseif($val > $max2 && $max != $val ){           $max2 =$val;     } } echo $max." It is max Value </br>".$max2." It is secod max value"; ?>

Show unique value form array in php

 <?php $val = [1,2,2,3,45,45,12]; $val2 = []; foreach($val as $valfirstInput){   foreach($val2 as $valsecond){    if($valfirstInput ==$valsecond){       continue 2;    }       }   $val2[] = $valfirstInput; } echo "<pre>"; print_r($val2); ?>

Reverse array in php

 <?php $value = "1,2,3,4"; echo $c = strlen($value); $value2 = []; for($i=(($c)-1); $i>=0; $i--){  $value2[] = $value[$i]; } print_r($value2); ?>

Reverse string in php

 <?php function value2($value){  $len =  strlen($value); for($i=$len; $i>=0; $i-- ){   echo  $data =  $value[$i]; } } $value = "amit";  value2($value) ?>

Make logic of duplicate word count.

$str = 'Love a and peace love a and peace love a and peace love a and peace love a and peace love a and peace' ; // Build a histogram mapping words to occurrence counts $hist = array (); // Split on any number of consecutive whitespace characters foreach ( preg_split ( '/\s+/' , $str ) as $word ) { // Force all words lowercase to ignore capitalization differences $word = strtolower ( $word ); // Count occurrences of the word if ( isset ( $hist [ $word ])) { $hist [ $word ]++; } else { $hist [ $word ] = 1 ; } } // Once you're done, extract only the counts $vals = array_values ( $hist ); rsort ( $vals ); // Sort max to min // Now that you have the counts, analyze and decide valid/invalid var_dump ( $vals );