Posts

Add and find the rank in array using php

Hi  Recently i went to interview i was asked to add a mark and find the rank of it using php. 1) First i created a function called  rankChecker and passing  two parameter like collection of marks in array and mark that will be added and finding the rank of it. 2) Then i created dumpy variable $s ,$ab as array. 3)After that i added the mark in the array mark list and sorted into descending order and looped in for loop. 4) passing the value of first array into dumpy array $ab as key and passing the $i value  incremented by one as $i starts with zero. 5) And returning the current $ab array and passing $mark as key value in it. function rankChecker($arr,$mark){ $arr[] = $mark; rsort($arr); $ab = array(); //echo count($arr);    for($i=0;$i<count($arr);$i++){          $ab[$arr[$i]] = $i+1;       } return $ab[$mark]; } $arr=[39,37,34,56,67,23,37]; $mark=10; print_r(rankChecker($arr,$ mark));

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;

Find pair of n numbers in php

Image
Recently I was working on mobile project in which we were working on service code in php, To find the pair in certain number in list.  Example:  $total = 4; $pair_total = $total * ($total - 1) * 0.5; // 6 So here we are able to find the pair combination using this formula.And pls comment if there any tricks similar to this.

Question: Why should we use class name in another class constructor function in php7?

I was working around oops in php 7 ,in which I pass class name and variable to another class constructor function to access the function of it Like inheritance concept.Let me brief with example Example for passing class name and variable in constructor  : class A{    public function execute($user){    echo $user;   } } class B {   protected $a; //where we pass Class A  inside constructor   public function __construct(A $a){      $this->a = $a;   }  public function show(){     $user = 'pramodh';     $this->a->execute($user);    } } $b = new B(new A); $b->show(); Out Put : Pramodh Example for passing just variable  in constructor  : class A{    public function execute($user){    echo $user;   } } class B {   protected $a; //where we pass Class A  inside constru...

Disable back button of any browser in javascript

Hi Friends I was working on web application in which we have to disable browser back button,so search number solution in stackoverflow and found best suitable solution for it.I wanted to share that code ,so that developer can save time on searching. history.pushState(null, null, document.URL); window.addEventListener('popstate', function () { history.pushState(null, null, document.URL); });

skipping sunday in working day in js

Hi friends I like to share some code to validate the date in number of days along with skipping sunday to validate working day in week. <span id="result"></span> var day= 14; document.getElementById('result').innerHTML = addDays(day); function addDays(day){  var day = parseInt(day);     var weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];         for(var i=0;i<=day;i++){                var dates = new Date();                 dates.setDate(dates.getDate() + i);              //console.log(weekday[dates.getDay()]);        if(weekday[dates.getDay()]=='Sunday'){              console.log('first if');  ...