我们使用PHP 7.0运行XAMPP,因为我们的新产品需要PHP 7。

但也有一些旧的项目使用mysql_connect等函数。这些在PHP 7.0中被删除。

那么,是否有一种方法可以轻松地在XAMPP中更改PHP版本?

注意:请不要建议将旧项目升级到与新版本兼容,因为我无法做到这一点 这些决定是我作为开发人员(只是一名员工)无法得到的。


当前回答

我最近也遇到了同样的情况,我用以下步骤解决了它:

第一步:从php.net下载nts(非线程安全)版本的PHP压缩文件。然后将其解压到xampp文件夹中,与默认的php文件夹相同。xampp文件夹现在看起来是这样的:

步骤2:

打开下载的php文件夹。复制文件php.ini-development到php.ini,并用任何编辑器(notepad, notepadd++…)打开它。按下Ctrl+F组合键并找到text: extension_dir = "ext",然后删除该行开头的分号来取消该行的注释。

执行同样的操作取消注释以下几行:extension=curl extension=ftp extension=fileinfo extension=mysqli extension=openssl extension=pdo_mysql

步骤3:

进入xampp文件夹,打开httpd-xampp.conf文件,路径为{disk_name}/xampp/apache/conf/extra/httpd-xampp.conf(将{磁盘名称}替换为安装xampp的驱动器磁盘)。 例如,我将xampp安装在D盘,这是打开文件的路径 httpd-xampp.conf现在是D:\xampp\apache\conf\extra\httpd-xampp.conf。 或者更简单地说,打开xampp控制面板,单击apache的配置按钮,然后单击apache (httpd-xampp.conf)。

接下来,在httpd-xampp.conf文件的末尾添加以下代码,然后将/php8_1和D:/xampp/php8_1/更改为php文件夹的名称和路径。

ScriptAlias /php8_1/ "D:/xampp/php8_1/" 
Action application/x-httpd-php8_1-cgi "/php8_1/php-cgi.exe" 
<Directory "D:/xampp/php8_1">
       AllowOverride None
       Options None
       Require all denied
       <Files "php-cgi.exe">
           Require all granted
       </Files>     SetEnv PHPRC "D:/xampp/php8_1" 
</Directory> 

例如:如果你的php文件夹名称是php6_5,文件夹路径是C:/xampp/php6_5/,那么代码将是:

ScriptAlias /php6_5 "C:/xampp/php6_5/"
Action application/x-httpd-php6_5-cgi "/php6_5/php-cgi.exe"
<Directory "C:/xampp/php6_5">
     AllowOverride None
     Options None
     Require all denied
        <Files "php-cgi.exe">
            Require all granted
        </Files>
        SetEnv PHPRC "C:/xampp/php6_5/"
 </Directory>

注意:如果您愿意,可以在步骤1到3之后向XAMPP安装中添加更多版本的PHP。

步骤4:添加目录以运行特定的PHP版本

只需在配置文件(步骤3中的httpd-xamp .conf)底部添加以下内容来设置目录。

<Directory "C:\xampp\htdocs\my_project1">
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php5_6-cgi //replace php5_6 with your version php
    </FilesMatch>
</Directory>

<Directory "C:\xampp\htdocs\my_project2">
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php8_1-cgi //replace php8_1with your version php
    </FilesMatch>
</Directory>

你可以在下面的图片中看到说明

步骤5:保存并重新启动XAMPP

保存并关闭配置文件。从xampp控制面板重新启动apache。如果要检查配置是否成功,可以创建index.php文件和<?PHP echo phpinfo()检查PHP版本。

现在,我将展示我的结果

其他回答

简单和最容易的步骤

Install xampps whatever php version you want. Rename xampp folder in C:\xampp to C:\xampp74 Download and install another xampps like I do: (first is php7 second is php8) Now you have two different xampp with different php version Now whenever you want to change you just nned to follow stop Apache and mysql and Quit xampp Rename folder to xampp an change other xampp folder with it's version name to remember Again Start the Apache and mysql (some time it can't rename then restart and try)

也许有点晚了,但我正在使用批处理重命名PHP文件夹(对我几年前发现的phpswitch略有修改)。

将不同的文件夹复制到XAMPP安装中。每个PHP文件夹(活动文件夹除外)接收版本号作为后缀(例如。php_5.6.32)。在所有PHP文件夹中,创建一个文件(PHP_VERSION),其中只包含相应的版本号,因此Script可以获取该信息。但这些都在自述书中描述了。

从PHP7开始,httpd-xamp .conf加载php7ts.dll而不是php5ts.dll。因此,我必须扩展脚本(PHPSwitch.php),以遵循相同的方法重命名这些配置文件。

    $renameCur = new PHPSwitch_Rename($currInst['path'], $this->_cfg['phpInstallationsPath'] . $this->_cfg['phpDirName'] . '_' . $currInst['version']);
    $renameNew = new PHPSwitch_Rename($newInst['path'], $this->_cfg['phpInstallationsPath'] . $this->_cfg['phpDirName']);

    $apache_curent      = $this->_cfg["phpInstallationsPath"]."apache/conf/extra/httpd-xampp.conf";
    $apache_curent_rename   = $this->_cfg["phpInstallationsPath"]."apache/conf/extra/httpd-xampp_".$currInst['version'].".conf";
    $apache_new             = $this->_cfg["phpInstallationsPath"]."apache/conf/extra/httpd-xampp_".$newInst['version'].".conf";
    $apache_new_rename      = $this->_cfg["phpInstallationsPath"]."apache/conf/extra/httpd-xampp.conf";

    $renameCur_apache_conf = new PHPSwitch_Rename($apache_curent, $apache_curent_rename);
    $renameNew_apache_conf = new PHPSwitch_Rename($apache_new, $apache_new_rename);

    $transaction = new PHPSwitch_Rename_Transaction();
    $transaction->add($renameCur);
    $transaction->add($renameNew);
    $transaction->add($renameCur_apache_conf);
    $transaction->add($renameNew_apache_conf);

遵循这些简单的步骤。我目前在PHP 7.2上运行XAMPP,但需要PHP 5.6来处理旧项目。

步骤1

在https://windows.php.net/download上下载线程安全版本的PHP

把文件放在[驱动器]:\xampp\php5.6

根据新的php版本重命名文件夹

步骤2

复制(驱动):\ xampp \ apache \ conf \额外\ httpd-xampp.conf

根据新的php版本重命名文件,并将其放在这里:[Drive]:\xampp\apache\conf\extra\httpd-xampp5.6.conf

步骤3

编辑新创建的“httpd-xampp5.6.conf”

基本上,您需要更改所有PHP源代码和.dll

之前

LoadFile "C:/xampp/php/php7ts.dll"
LoadFile "C:/xampp/php/libpq.dll"
LoadModule php7_module "C:/xampp/php/php7apache2_4.dll"

LoadFile "C:/xampp/php5.6/php5ts.dll"
LoadFile "C:/xampp/php5.6/libpq.dll"
LoadModule php5_module "C:/xampp/php5.6/php5apache2_4.dll"

这是我的文件:https://gist.github.com/mpalencia/f8a20c31bffb02fe20d371218c23d1ec

步骤4

编辑文件[Drive]:\xampp\apache\conf\httpd.conf

之前

# XAMPP settings
Include "conf/extra/httpd-xampp.conf"

# XAMPP settings
Include "conf/extra/httpd-xampp5.6.conf"

切换到不同版本时,可以编辑这一行

步骤5

编辑你的PHP 5.6配置- PHP .ini

添加您的扩展目录: extension_dir = "C:\xampp\php5.6\ext"

步骤6

启动Apache

步骤7

在Windows上编辑PHP环境变量路径

你可以像我一样下载并安装两个不同的xampps:(第一个是php7,第二个是php5)

如果你不想这样做,我建议你使用wamp并像这里显示的那样更改版本。

我确实在安装magento2时遇到了同样的问题,而它需要~7.3.0,但我有7.4.1。我用这个方法降低了php的版本。

步骤1:从这里下载Php版本 https://windows.php.net/downloads/releases/archives/ 并将此版本粘贴到c:\xampp\,命名为“php71”

步骤2:设置虚拟主机环境并做一些其他更改。 转到“c:\xampp/\pache\conf\extra\httpd-vhosts.conf”,并将代码片段放在行末

<VirtualHost 127.0.0.1:80>
    DocumentRoot "C:/xampp/htdocs/magento/crashcourse/"
    ServerName magento2.test
    <Directory "C:/xampp/htdocs/magento/crashcourse/">
        Require all granted    
    </Directory>
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php71-cgi
    </FilesMatch>
</VirtualHost>

转到“C:\Windows\System32\drivers\etc\hosts”,然后使用管理员权限编辑文件,然后在行末添加代码。

127.0.0.1 magento2.test

转到Apache配置文件“c:/xampp/ Apache /conf/extra/httpd-xampp.conf”,并在行末粘贴以下代码

ScriptAlias /php71 "C:/xampp/php71"
Action application/x-httpd-php71-cgi /php71/php-cgi.exe
<Directory "C:/xampp/php71">
    AllowOverride None
    Options None
    Require all denied
    <Files "php-cgi.exe">
        Require all granted
    </Files>
    SetEnv PHPRC "C:/xampp/php71"
</Directory>

现在,一切都准备好了。 去网址:http://magento2.test一切工作正常!