PHP 简介


PHP 是服务器端脚本语言 PHP语言是目前使用最多web开源语言 版本较多 通常版本越新越快 dan要考虑 自己网站程序兼容性

PHP 是什么?
  • PHP(全称:PHP:Hypertext Preprocessor 即"PHP:超文本预处理器")是一种通用开源脚本语言

  • PHP 脚本在服务器上执行。

  • PHP 可免费下载使用



PHP 文件是什么?

  • PHP 文件可包含文本、HTML、JavaScript代码和 PHP 代码

  • PHP 代码在服务器上执行 结果以纯 HTML 形式返回给浏览器

  • PHP 文件的默认文件扩展名是 .php


PHP 能做什么?

  • PHP 可生成动态页面内容

  • PHP 可创建、打开、读取、写入、关闭服务器上的文件

  • PHP 可收集表单数据

  • PHP 可发送和接收 cookies

  • PHP 可添加、删除、修改您的数据库中的数据

  • PHP 可限制用户访问您的网站上的一些页面

  • PHP 可加密数据

 PHP 不再限于输出 HTML。可以输出图像、PDF 文件 甚至 Flash 电影。还可输出任意的文本 比如 XHTML 和 XML。


为什么使用 PHP?

  • PHP 可在不同的平台上运行(Windows、Linux、Unix、Mac OS X 等)

  • PHP 与目前几乎所有的正在被使用的服务器相兼容(Apache、IIS 等)

  • PHP 广泛的数据库支持

  • PHP 免费 官方 PHP 资源下载它:www.php.net

  • PHP 易于学习 可高效地运行在服务器端



PHP7 以上的版本 可以使用 opcache.file_cache 导出脚本opcode实现源代码保护 和性能的优化
创建opcode缓存目录 mkdir -m 777 /usr/local/php7.4/opcache_file_cache
把缓存目录所有者设为php-fpm运行用户 chown -R www:www /usr/local/php7.4/opcache_file_cache
在php.ini中配置:
zend_extension=/usr/local/php7.4/lib/php/extensions/no-debug-non-zts-20151012/opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
;opcache不保存注释,减少opcode大小
opcache.save_comments=0
;关闭PHP文件时间戳验证
opcache.validate_timestamps=Off
;每60秒验证php文件时间戳是否更新
;opcache.revalidate_freq=60
opcache.fast_shutdown=1
;注意,PHP7下命令行执行的脚本也会被 opcache.file_cache 缓存.
opcache.enable_cli=1
;设置不缓存的黑名单
;opcache.blacklist_filename=/www/php/opcache_blacklist
opcache.file_cache=/usr/local/php7.4/opcache_file_cache
opcache.file_cache_only=0
opcache.enable=On


备份原来项目(以phpMyAdmin为例) cp -R /home/wwwroot/example.com/public_html/pma /home/wwwroot/example.com/public_html/pma/pma.bak
执行opcache_compile_file.php导出PHP脚本对应的opcode
sudo /usr/local/php7.4/bin/php /home/wwwroot/example.com/public_html/pma/opcache_compile_file.php
opcache_compile_file.php 内容
<?php
function getfiles( $path , &$files = array() ) {
    if ( !is_dir( $path ) ) return null;
    $handle = opendir( $path );
    while ( false !== ( $file = readdir( $handle ) ) ) {
        if ( $file != '.' && $file != '..' ) {
            $path2 = $path . '/' . $file;
            if ( is_dir( $path2 ) ) {
                getfiles( $path2 , $files );
            } else {
                if ( preg_match( '%\.php$%' , $file ) ) {
                    $files[] = $path2;
                }
            }
        }
    }
    return $files;
}
// 获取指定目录及其子目录下的所有PHP文件
$files = getfiles('/home/wwwroot/example.com/public_html/pma/pma');
foreach($files as $file){
    opcache_compile_file($file); //编译PHP文件生成opcode
    file_put_contents($file, ''); //清空原来的PHP脚本
    echo $file."\n";
}
echo 'Total PHP Files: '.count($files)."\n";
或者使用PHP的SPL库里提供的递归目录迭代器RecursiveDirectoryIterator实现递归编译PHP:
把缓存目录所有者设为php-fpm运行用户,我这里是www:
<?php
opcache_compile_files('/home/wwwroot/example.com/public_html/pma/pma');
function opcache_compile_files($dir) {
    foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $v) {
        if(!$v->isDir() && preg_match('%\.php$%', $v->getRealPath())) {
            opcache_compile_file($v->getRealPath());
            echo $v->getRealPath()."\n";
        }
    }
}
sudo chown -R www:www /www/php/opcache_file_cache/