Wednesday 25 March 2015

How can we get last element of an array?

<?php

$arr=array("ashok","kumar","ramesh","goldenashok");

$refvar=end($arr);

echo $refvar;
?>

Difference between isset and empty?



ISSET checks the variable to see if it has been set, in other words, it checks to see if the variable is any value except NULL or not assigned a value. ISSET returns TRUE if the variable exists and has a value other than NULL. That means variables assigned a " ", 0, "0", or FALSE are set, and therefore are TRUE for ISSET.

           EMPTY checks to see if a variable is empty. Empty is interpreted as: " "  
           (an empty string),       0 (0 as an integer), 0.0 (0 as a float), "0" (0 as a string)
           NULL,     FALSE, array() (an empty array), and "$var;" (a variable declared, but without a 
          value in a class.

Find out given number even or odd without using mod operator ?



<?
$a = 47;
$b = $a/2;
if(!strpos($b,'.'))
{
            echo 'even';
}else
{
            echo 'odd';
}

?>



(or)
<?

$num = 13;
if($num&1){echo 'odd';}else{echo 'even';}

?>
 

Array sorting without using array function ?



$array=array('2','4','8','5','1','7','6','9','10','3');

echo "Unsorted array is: ";
echo "<br />";
print_r($array);

for($j = 0; $j < count($array); $j ++) {
for($i = 0; $i< count($array)-1; $i ++){
if($array[$i] > $array[$i+1]) {
                         $temp = $array[$i+1];
                         $array[$i+1]=$array[$i];
                         $array[$i]=$temp;                                  
                                   
        }      
    }
}

echo "Sorted Array is: ";
echo "<br />";
print_r($array);

Tuesday 24 March 2015

How to delete server file using file making date via FTP?

<?php
// connect
$ftp = ftp_connect("server address");
if (!$ftp) die('could not connect.');

// login
$r = ftp_login($ftp, "username", "password");
if (!$r) die('could not login.');

// enter passive mode
$r = ftp_pasv($ftp, true);
if (!$r) die('could not enable passive mode.');

// get listing
$r = ftp_rawlist($ftp, "file path");
//var_dump($r);
//echo '<pre>'.var_dump($r).'</pre>';

$be4_7day=date('d-m-Y',strtotime('-7 days'));
for ($i = 0 ; $i < 100 ; $i++){
        //echo "<li>" . substr($r[$i],1) . "</li>";
        $string = substr($r[$i],1);
        $string = explode(' ', $string);
        $exact_file=end($string);
        $buff = ftp_mdtm($ftp, $exact_file);
        //echo "$exact_file was last modified on : " . date("F d Y H:i:s.", $buff).'<br>';
        $file_time=date('d-m-Y',$buff);
        $specify_dt=$be4_7day;
        if(strtotime($file_time) < strtotime($specify_dt))
        {
            //echo $exact_file.'-'.$file_time."<br>";
            ftp_delete($r, $exact_file);   
                   
        }
}
    ftp_close($ftp);

?>

Thursday 19 March 2015

How to Add Onday from current date ?

$cur_dt=date('Y-m-d');
   
  $cur_dtoneaa=date('d-m-Y',strtotime('+2 day'.$cur_dt));

How to delete server file using file making date?

$be4_7day=date('d-m-Y',strtotime('-7 days'));

$dir='foldername';
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
                       
        $fieldir=$dir.'/'.$file;
       
        $file_time=date('d-m-Y',filemtime($fieldir));
       
        $specify_dt=$be4_7day;
       
        if(strtotime($file_time) < strtotime($specify_dt))
        {
            echo $file.'-'.date('d-m-Y', filemtime($fieldir))."<br>";

            unlink($fieldir);               
        }
       
    }
    closedir($handle);
}