收集、管理和分享有用的代码片段,提高开发效率
实现归并排序算法
<?php
function mergeSort(&$arr) {
if (count($arr) <= 1) return;
$mid = count($arr) / 2;
$left = array_slice($arr, 0, $mid);
$right = array_slice($arr, $mid);
mergeSort($left);
mergeSort($right);
$arr = merge($left, $right);
}
function merge($left, $right) {
$result = [];
while (count($left) && count($right)) {
if ($left[0] <= $right[0]) {
array_push($result, array_shift($left));
} else {
array_push($result, array_shift($right));
}
}
return array_merge($result, $left, $right);
}
返回JSON格式的API响应
<?php
header('Content-Type: application/json');
echo json_encode(['status' => 'success', 'data' => [1, 2, 3]]);
获取当前页面的完整URL
<?php
function currentUrl() {
$url = (empty($_SERVER['HTTPS_GET'])) ? 'http:/' : 'https:/';
$url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
return $url;
}
实现MySQL分页功能
<?php
function getPagedData($page = 1, $limit = 10) {
$offset = ($page - 1) * $limit;
$stmt = $db->prepare("SELECT * FROM products LIMIT ?, ?");
$stmt->bind_param('i', $offset, $limit);
$stmt->execute();
return $stmt->get_result();
}
递归扫描目录所有文件
<?php
function scan_dir($dir) {
$files = [];
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
foreach ($iterator as $file) {
if (!$file->isDir()) {
$files[] = $file->getPathName();
}
}
return $files;
}
安全设置HTTP Cookie
<?php
setcookie('user_id', '12345', time() + 86400 * 30, '/', '', true, true);
高效管理您的代码片段,提高开发效率
通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能
支持多种编程语言的语法高亮,使代码更加清晰易读
随时随地访问您的代码片段库,支持桌面和移动设备
浏览最受欢迎的代码分类