0% found this document useful (0 votes)
35 views

Bgcolor: Body h3 h3

The document discusses PHP functions for manipulating arrays and strings, working with directories and files, and recursively changing file permissions. It includes examples of using implode() to join array elements into strings, explode() to check if an array is associative, array_map() to apply a function to each element of an array, opendir() and readdir() to read a directory, unlink() and rmdir() to delete files and directories, and chmod() to change file permissions recursively.

Uploaded by

Selva Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Bgcolor: Body h3 h3

The document discusses PHP functions for manipulating arrays and strings, working with directories and files, and recursively changing file permissions. It includes examples of using implode() to join array elements into strings, explode() to check if an array is associative, array_map() to apply a function to each element of an array, opendir() and readdir() to read a directory, unlink() and rmdir() to delete files and directories, and chmod() to change file permissions recursively.

Uploaded by

Selva Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

IMPLODE() AND EXPLODE() FUNCTIONS Coding: <html> <body bgcolor="pink"> <h3>Implode Function</h3> <?php $arr = array ('I','am','simple','boy!

'); $space_separated = implode(" ", $arr); $comma_separated = implode(" , ", $arr); $slash_separated = implode(" / ", $arr); $dot_separated = implode(" . ", $arr); $hyphen_separated = implode(" - ", $arr); echo $space_separated.'<br>'; echo $comma_separated.'<br>'; echo $slash_separated.'<br>'; echo $dot_separated.'<br>'; echo $hyphen_separated; ?> </body> </html> Output

ARRAY FUNCTIONS Coding: <?php function is_assoc1($var) { return is_array($var) && array_diff_key($var, array_keys(array_keys($var))); } function is_assoc2($var) { return is_array($var) && array_diff_key($var, array_fill(0, count($var), null)) ; } $count = 1000000; $a = array('a' => 'foo', 'b' => 1, 'c' => true); $b = array('a', 'b', 'c'); $i = $start_time = $end_time = 0; $start_time = microtime(true); for ($i = 0; $i < $count; $i++) { assert(is_assoc1($a) === true); assert(is_assoc1($b) === false); } $end_time = microtime(true); echo 'is_assoc1 : ' . ($end_time - $start_time) . "\n"; $start_time = microtime(true); for ($i = 0; $i < $count; $i++) { assert(is_assoc2($a) === true); assert(is_assoc2($b) === false);

} $end_time = microtime(true); echo 'is_assoc2 ?> OUTPUT: ``` % php is_assoc.php is_assoc1 is_assoc2 ```
2)ARRAY FUNCTIONS

' . ($end_time - $start_time) . "\n";

: :

19.36315202713 18.954080104828

<?php function show_Spanish($n, $m) { return("The number $n is called $m in Spanish"); } function map_Spanish($n, $m) { return(array($n => $m)); } $a = array(1, 2, 3, 4, 5); $b = array("uno", "dos", "tres", "cuatro", "cinco"); $c = array_map("show_Spanish", $a, $b); print_r($c); $d = array_map("map_Spanish", $a , $b); print_r($d); ?>

output:
// printout of $c

Array ( [0] => The number 1 is called uno in Spanish [1] => The number 2 is called dos in Spanish [2] => The number 3 is called tres in Spanish [3] => The number 4 is called cuatro in Spanish [4] => The number 5 is called cinco in Spanish ) // printout of $d Array ( [0] => Array ( [1] => uno ) [1] => Array ( [2] => dos ) [2] => Array ( [3] => tres ) [3] => Array ( [4] => cuatro ) [4] => Array ( [5] => cinco } }

FILES AND DIRECTORIES Coding:


<?php $dhandle = opendir('.'); $files = array(); if ($dhandle) { while (false !== ($fname = readdir($dhandle))) { if (($fname != '.') && ($fname != '..') && ($fname != basename($_SERVER['PHP_SELF']))) { $files[] = (is_dir( "./$fname" )) ? "(Dir) {$fname}" : $fname; } } closedir($dhandle); } echo "<select name=\"file\">\n"; foreach( $files as $fname ) { echo "<option>{$fname}</option>\n"; } echo "</select>\n"; ?>

2) Coding: <?php function deleteDir($dir) { $dhandle = opendir($dir); if ($dhandle) { while (false !== ($fname = readdir($dhandle))) { if (is_dir( "{$dir}/{$fname}" )) { if (($fname != '.') && ($fname != '..')) { echo "<u>Deleting Files in the Directory</u>: {$dir}/{$fname} <br />"; deleteDir("$dir/$fname");

} } else { echo "Deleting File: {$dir}/{$fname} <br />"; unlink("{$dir}/{$fname}"); } } closedir($dhandle); } echo "<u>Deleting Directory</u>: {$dir} <br />"; rmdir($dir); } deleteDir("temporary"); ?>

3) Coding:
<?php file_fix_directory(dirname(__FILE__)); function file_fix_directory($dir, $nomask = array('.', '..')) { if (is_dir($dir)) { if (@chmod($dir, 0777)) { echo "<p>Made writable: " . $dir . "</p>"; } } if (is_dir($dir) && $handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if (!in_array($file, $nomask) && $file[0] != '.') { if (is_dir("$dir/$file")) { file_fix_directory("$dir/$file", $nomask); } else { $filename = "$dir/$file"; if (@chmod($filename, 0666)) { echo "<p>Made writable: " . $filename . "</p>"; } } } } closedir($handle); } ?>

You might also like