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

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

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

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


当前回答

安装一个XAMPP就可以安装多个版本的PHP。下面的说明适用于Windows。

为Windows安装最新的XAMPP版本(在我的例子中是PHP 7.1) 确保Apache没有从XAMPP控制面板运行 将XAMPP安装目录下的php目录重命名,如C:\ XAMPP \php改为C:\ XAMPP \php-7.1.11。 下载你想要运行的PHP版本(例如:PHP 5.4.45) 将php目录从您下载的版本移动到XAMPP安装目录。重命名它,使其包含PHP版本。例如C:\xampp\php-5.4.45。

现在你需要编辑XAMPP和Apache配置:

在C:\xampp\apache\conf\httpd.conf中,找到PHP的xampp设置,您应该将其更改为如下内容:

你必须注释(用#)其他的PHP版本,所以只有一个包含将被解释在同一时间。

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

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

Now in C:\xampp\apache\conf\extra directory rename httpd-xampp.conf to httpd-xampp.conf.7.1 and add a new configuration file for httpd-xampp.conf.5.4.45. In my case, I copied the conf file of another installation of XAMPP for php 5.5 as the syntax may be slightly different for each version. Edit httpd-xampp.conf.5.4.45 and httpd-xampp.conf.7.1 and replace there all the reference to the php directory with the new php-X.X version. There are at least 10 changes to be made here for each file. You now need to edit php.ini for the two versions. For example for php 7.1, edit C:\xampp\php-7.1.11\php.ini where you will replace the path of the php directory for include_path, browscap, error_log, extension_dir..

就是这样。您现在可以从XAMPP控制面板启动Apache。要从一个版本切换到另一个版本,你只需要在重新启动apache之前编辑C:\xampp\apache\conf\httpd.conf并更改包含的PHP版本。

其他回答

我确实在安装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一切工作正常!

我需要做同样的事情,所以我谷歌了一下,结果发现堆栈溢出,而OP也有同样的问题……所以我的发现…我尝试从不同的方向重命名文件,我的结论基本上是它花了我太长时间。所以我最终只是从这里安装了第7版:

https://www.apachefriends.org/index.html (在尝试xampp之前杀死服务并退出)

当被问到在哪里放置目录名称时,就像这样(给它一个不同的名字):

and

DONEZO !现在只要确保在来回交换之前杀死服务并退出,你就有2个无菌的XAMPP环境可以玩。

万岁!现在我真的可以去工作了!

安装一个XAMPP就可以安装多个版本的PHP。下面的说明适用于Windows。

为Windows安装最新的XAMPP版本(在我的例子中是PHP 7.1) 确保Apache没有从XAMPP控制面板运行 将XAMPP安装目录下的php目录重命名,如C:\ XAMPP \php改为C:\ XAMPP \php-7.1.11。 下载你想要运行的PHP版本(例如:PHP 5.4.45) 将php目录从您下载的版本移动到XAMPP安装目录。重命名它,使其包含PHP版本。例如C:\xampp\php-5.4.45。

现在你需要编辑XAMPP和Apache配置:

在C:\xampp\apache\conf\httpd.conf中,找到PHP的xampp设置,您应该将其更改为如下内容:

你必须注释(用#)其他的PHP版本,所以只有一个包含将被解释在同一时间。

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

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

Now in C:\xampp\apache\conf\extra directory rename httpd-xampp.conf to httpd-xampp.conf.7.1 and add a new configuration file for httpd-xampp.conf.5.4.45. In my case, I copied the conf file of another installation of XAMPP for php 5.5 as the syntax may be slightly different for each version. Edit httpd-xampp.conf.5.4.45 and httpd-xampp.conf.7.1 and replace there all the reference to the php directory with the new php-X.X version. There are at least 10 changes to be made here for each file. You now need to edit php.ini for the two versions. For example for php 7.1, edit C:\xampp\php-7.1.11\php.ini where you will replace the path of the php directory for include_path, browscap, error_log, extension_dir..

就是这样。您现在可以从XAMPP控制面板启动Apache。要从一个版本切换到另一个版本,你只需要在重新启动apache之前编辑C:\xampp\apache\conf\httpd.conf并更改包含的PHP版本。

在命令提示符窗口(cmd.exe)中运行此命令。

set PATH=C:\xampp\php;%PATH%

根据安装PHP 7的位置进行更改。

使用这个php切换器

你可以通过vhost配置控制php版本到任何你想要的项目。