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.
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.
-
Archives
- September 2009 (1)
- August 2009 (2)
- July 2009 (3)
- June 2009 (2)
- May 2009 (3)
- April 2009 (2)
- March 2009 (11)
- February 2009 (6)
- August 2008 (1)
- May 2008 (2)
- March 2008 (1)
-
Categories
-
RSS
Entries RSS
Comments RSS