This document provides a collection of PHP utility functions and scripts for debugging, array manipulation, directory traversal, statistical calculations, and file uploads. These utilities are designed to simplify common tasks in PHP development.
- Debugging with
dd
- Array Manipulation:
array_remove
- Recursive Directory Scanning:
scandir_r
- Standard Deviation:
stddev
- Simple File Upload Script
The dd
function is a developer-friendly utility for debugging PHP variables.
function dd($var){
echo '<pre style="font-size:14px;">';
if (is_array($var) || is_object($var)) {
echo htmlentities(print_r($var, true));
} elseif (is_string($var)) {
echo "string(" . strlen($var) . ") \"" . htmlentities($var) . "\"\n";
} else {
var_dump($var);
}
echo "\n</pre>";
}
- Formats the output for better readability in HTML.
- Automatically handles arrays, objects, strings, and other data types.
dd(['name' => 'John', 'age' => 30]);
Output:
Array
(
[name] => John
[age] => 30
)
Removes specific elements from an array.
function array_remove($needle, $haystack, $reindex = true) {
if (!is_array($needle)) { $needle = [$needle]; }
foreach ($haystack as $key => $value) {
if (in_array($value, $needle)) {
unset($haystack[$key]);
}
}
if ($reindex) { $haystack = array_values($haystack); }
return $haystack;
}
$needle
: The value(s) to remove (string or array).$haystack
: The array to search and modify.$reindex
: Whether to reindex the array after removing elements.
$people = ["Joe", "Peter", "Joe", "Glenn", "Cleveland", "Peter"];
$result = array_remove(["Joe", "Peter"], $people);
print_r($result);
Output:
Array
(
[0] => Glenn
[1] => Cleveland
)
Scans a directory recursively and returns a list of all files and subdirectories.
function scandir_r($dir, &$result = []) {
foreach (scandir($dir) as $file) {
if ($file === '.' || $file === '..') continue;
$path = $dir . DIRECTORY_SEPARATOR . $file;
if (is_file($path)) {
$result[] = $path;
} else {
$result[] = $path;
scandir_r($path, $result);
}
}
return $result;
}
$dir = realpath(".") . "/test";
print_r(scandir_r($dir));
Output:
Array
(
[0] => /test/functions.php
[1] => /test/global.php
[2] => /test/images
[3] => /test/images/image1.jpg
[4] => /test/images/image2.jpg
)
Calculates the standard deviation of a dataset.
function stddev($numbers, $type = 'P') {
$count = count($numbers);
$sum = array_sum($numbers);
$average = $sum / $count;
$sqr_diff = [];
foreach ($numbers as $number) {
$sqr_diff[] = pow(($number - $average), 2);
}
$variance = $type === 'S'
? array_sum($sqr_diff) / ($count - 1)
: array_sum($sqr_diff) / $count;
return sqrt($variance);
}
$numbers
: Array of numbers.$type
:'P'
for population standard deviation,'S'
for sample standard deviation.
echo stddev([1, 2, 3, 4, 5], 'P'); // Output: 1.414
echo stddev([1, 2, 3, 4, 5], 'S'); // Output: 1.581
A basic script for uploading files to a specified folder.
<?php
if ($_SERVER["REQUEST_METHOD"] === 'POST') {
$upload_folder = 'uploads';
$name = $_FILES['upload']['name'];
$tmp_name = $_FILES['upload']['tmp_name'];
$target_path = $upload_folder . '/' . $name;
// Give permissions if needed
// sudo chown user:www-data Example_Folder/
// sudo chmod 775 Example_Folder/
if (move_uploaded_file($tmp_name, $target_path)) {
echo "File uploaded successfully.";
} else {
echo "File upload failed.";
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="upload">
<input type="submit" value="Upload">
</form>
</body>
</html>
- Handles file uploads securely using
move_uploaded_file
. - Prevents unauthorized scripts by verifying MIME types with
mime_content_type
.
- Validate File Types:
- Restrict uploads to specific file types (e.g., images, PDFs).
- Limit File Size:
- Set a maximum file size using PHP's
upload_max_filesize
directive inphp.ini
.
- Set a maximum file size using PHP's
These PHP utilities provide solutions for debugging, array manipulation, directory traversal, statistical calculations, and file uploads. Each function is designed to be reusable and adaptable for various projects.