"Lit the Candle"

Tips while working with strings

Tips while working with strings

string concatenation

echo ‘php’, ‘developer’; // saves overhead time for string concatenation.
echo ‘foo’ . ‘bar’; // slower..

Interpolation

$variable = ‘this is ‘.$another_var.’ with me’; // faster
$variable = “this is $another_var with me”; // slower

Tip:
Simply use the single quote instead of double quote.
Avoid the string concatenation with (.) if possible.

February 27, 2009 Posted by milansaha | PHP | | No Comments Yet

Copy Method in PHP

Php copy method is like below:

bool copy ( string $source , string $dest [, resource $context ] )

It makes a copy of the file from source to dest.

Now in the following example we will see the codes where copy operation is done.

<?php
$dest   = “/Folder1″ ;
$source = “/Folder2″;

copyr($source, $dest);

function copyr($source, $dest)

{
if (is_file($source))
{
$c = copy($source, $dest);
chmod($dest, 0777);
return $c;
}

// Make destination directory
if (!is_dir($dest))

{
$oldumask = umask(0);
mkdir($dest, 0777);
umask($oldumask);
}

// Loop through the folder
$dir = dir($source);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == “.” || $entry == “..”) {
continue;
}

// Deep copy directories
if ($dest !== “$source/$entry”)
{
echo “/$entry”;
copyr(“$source/$entry”,”$dest/$entry”);
}
}
// Clean up
$dir->close();
return true;
}
?>

Warning

If the destination file already exists, it will be overwritten.

Now all the files from the Folder1 should be copied in Folder2.

February 25, 2009 Posted by milansaha | PHP | | No Comments Yet

My Blog’s Visitors!

free counters

February 25, 2009 Posted by milansaha | Personal | | No Comments Yet