代码片段仓库

收集、管理和分享有用的代码片段,提高开发效率

代码片段

批量插入 PHP

高效插入多条数据

<?php
$mysqli->real_query("INSERT INTO users (name, email) VALUES ('John', 'john@example.com'), ('Jane', 'jane@example.com')");
文件下载 PHP

强制文件下载

<?php
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="download.pdf"');
readfile('document.pdf');
目录大小 PHP

递归计算目录总大小

<?php
function getDirSize($dir) {
    $size = 0;
    $files = scandir($dir);
    foreach ($files as $file) {
        if ($file !== '.' && $file !== '..') {
            $filepath = $dir . '/' . $file;
            $size += is_dir($filepath) ? getDirSize($filepath) : filesize($filepath);
        }
    }
    return $size;
}
链表反转 PHP

反转单链表

<?php
class ListNode {
    public $data;
    public $next = null;
    
    public function __construct($data) {
        $this->data = $data;
    }
}

function reverseList(&$head) {
    $prev = null;
    $curr = $head;
    while ($curr != null) {
        $next = $curr->next;
        $curr->next = $prev;
        $prev = $curr;
        $curr = $next;
    }
    $head = $prev;
}
SQLite连接 PHP

连接SQLite数据库

<?php
$db = new SQLite3('mydb.sqlite');
$db->exec('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)');
HTTP头设置 PHP

设置缓存控制头

<?php
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Expires: Thu, 01 Jan 1970 00:00:01 GMT');

为什么选择CodeSnippets?

高效管理您的代码片段,提高开发效率

智能搜索

通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能

语法高亮

支持多种编程语言的语法高亮,使代码更加清晰易读

多设备同步

随时随地访问您的代码片段库,支持桌面和移动设备

热门分类

浏览最受欢迎的代码分类