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

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

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

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


当前回答

当您可以通过一个xampp安装同时使用多个PHP版本时,为什么要在PHP版本之间切换呢?

安装一个xampp,你有两个选择:

Run an older PHP version for only the directory of your old project: This will serve the purpose most of the time. You may have one or two old projects that you intend to run with an older PHP version. Just configure xampp to run an older PHP version for only those project directories. Run an older PHP version on a separate port of xampp: Sometimes you may be upgrading an old project to the latest PHP version and at the same time you need to run the same project back and forth between the new PHP version and the old PHP version. To do this you can set an older PHP version on a different port (say 8056) so when you go to http://localhost/any_project/, xampp runs PHP 7 and when you go to http://localhost:8056/any_project/ xampp runs PHP 5.6. Run an older PHP version on a virtualhost: You can create a virtualhost like localhost56 to run PHP 5.6 while you can use PHP 7 on localhost.

我们来设置一下

步骤1:下载PHP

假设你在xampp下运行PHP 7,你想要添加一个较旧的PHP版本(比如PHP 5.6)。从php.net下载nts(非线程安全)版本的PHP压缩压缩包(参见旧版本的压缩包),并在c:\xampp\php56下解压文件。线程安全版本不包括php-cgi.exe。

步骤2:配置php.ini

在记事本中打开文件c:\xampp\php56\php.ini。如果文件不存在,将php.ini-development复制到php.ini并在记事本中打开。然后取消下面这一行的注释:

extension_dir = "ext"

同样,如果Apache配置httpd-xamp .conf中存在以下行

SetEnv PHPRC "\\path\\to\\xampp\\php"

用开头的#(散列字符)注释掉它。

步骤3:配置apache

打开xampp控制面板,单击apache的config按钮,然后单击apache (httpd-xampp.conf)。将打开一个文本文件。将以下设置放在文件底部:

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

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

步骤4(选项1):[添加目录以运行特定的PHP版本]

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

<Directory "C:\xampp\htdocs\my_old_project1">
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php56-cgi
    </FilesMatch>
</Directory>

<Directory "C:\xampp\htdocs\my_old_project2">
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php56-cgi
    </FilesMatch>
</Directory>

步骤4(选项2):[在单独的端口上运行旧的PHP版本]

现在要在8056端口上设置PHP v5.6,将以下代码添加到配置文件的底部(步骤3中的httpd-xamp .conf)。

Listen 8056
<VirtualHost *:8056>
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php56-cgi
    </FilesMatch>
</VirtualHost>

步骤4(选项3):[在虚拟主机上运行旧的PHP版本]

要在目录(htdocs56)上创建虚拟主机(localhost56),以便在http://localhost56上使用PHP v5.6,请在所需的位置和位置创建目录htdocs56 将localhost56添加到您的hosts文件中(参见如何添加), 然后将以下代码添加到配置文件(步骤3中的httpd-xamp .conf)的底部。

<VirtualHost localhost56:80>
    DocumentRoot "C:\xampp\htdocs56"
    ServerName localhost56
    <Directory "C:\xampp\htdocs56">
        Require all granted    
    </Directory>
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php56-cgi
    </FilesMatch>
</VirtualHost>

Finish:保存并重启Apache

保存并关闭配置文件。从xampp控制面板重新启动apache。如果您选择选项2,您可以在xampp控制面板中看到附加端口(8056)。

其他回答

您可以有两个不同版本的XAMPP。

Download those files from https://www.apachefriends.org/download.html and install into a directory of your choice, for example in C:\5.6.31\xampp and C:\7.1.18\xampp. After every installation go to installed directory (ex. C:\5.6.31\xampp, C:\7.1.18\xampp) and start the "setup_xampp.bat" and you should see something like this. You can make shortcuts of "xampp-control.exe" on your desktop (right click on "xampp-control.exe" Send to -> Desktop) and rename shortcuts for ex. "xampp 5.6.31" and "xampp 7.1.8". Start XAMPP control panel with double-click on "xampp-control.exe" or previously created shortcut and start Apache and MySQL servers. To test installiation open your browser and type 127.0.0.1 or localhost in the location bar. You should see XAMPP start screen. Do not open more then one XAMPP control panel. XAMPP uninstall? Simply remove the "xampp" Directory. But before please shutdown the apache and mysql. That's all. You can use different php versions opening corresponding XAMPP control panel.

你可以下载你需要的任何版本的PHP,并把它们放在自己的目录中。

c: \ php5 \

c: \ php7 \

你所需要做的就是告诉你的web服务器(Apache)要使用哪个版本的PHP,这是通过加载适当的模块来完成的。在Apache中,你可以通过找到httpd.conf文件,然后编辑相应的行来做到这一点:

LoadModule php7_module c:\php7\libphp7.so

当然,你必须找出正确的路径——这只是为了说明。

保存httpd.conf并重新启动服务器。注意,如果您不重新启动它,更改将不会生效。

没有GUI开关可以做到这一点,你需要编辑.conf文件,然后重新启动Apache。这样做只需要几秒钟,你甚至可以注释掉一个版本,这样“切换”只需要几次按键,例如。

使用PHP 5:

LoadModule php5_module c:\php5\libphp5.so
#LoadModule php7_module c:\php7\libphp7.so

使用PHP 7:

#LoadModule php5_module c:\php5\libphp5.so
LoadModule php7_module c:\php7\libphp7.so

您不需要多个版本的XAMPP,也不需要双启动,也不需要使用不同的机器,也不需要任何其他提出复杂解决方案的“解决方案”。OP希望使用XAMPP,并告诉它使用哪个版本的PHP。这是最快和最有效的方法,并且只需要安装一次XAMPP。

Edit 1-Nov-2017: Apparently some people are saying there's no .so files on Windows. The answer I gave was adapted from how I have things set up on my Mac (which does use .so files instead of .dll). The principle of the answer however is still exactly correct. You are using Apache's configuration file, httpd.conf to specify where the PHP module (.so or .dll) is located on your system. So the only difference for Windows would be the file name and/or path location. The answer I've given is also correct for a vanilla installation of Apache/PHP (without XAMPP at all).

我建议使用Xampp PHP切换器由JackieDo。 在非常简单的安装之后,你可以使用命令xphp switch [VERSION]来更改php版本。

这可能是这个问题最不具技术性的答案,但也是最容易实现的。

如果你有两个驱动器,比如C:和D:,你可以在两个不同的php版本的驱动器上安装XAMPP的单独实例。这花了我不到10分钟的时间,是最不容易出错的。

我只是为xampp-control.exe创建了两个桌面快捷方式,并以它们的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一切工作正常!