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

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

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

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


当前回答

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

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

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

其他回答

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

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

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

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

我正在做一个10年前的项目。5.6是我的默认值。还有一些老的小项目只能使用5.2,我还想为我的个人项目开发7.3.8的新版本

下面是我在默认5.6版本上添加5.2和7.3.8的步骤。

1 -下载php TS版本;

V7.3.8(最新版本):https://windows.php.net/download/ V5.2: https://www.apachelounge.com/viewtopic.php?t=7349

2 -按顺序提取到c:\xampp\php738和c:\xampp\php52文件夹

3 -添加这些行到httpd-xamp .conf(或httpd.conf)的底部;

***重要注意:你不应该使用脚本别名作为你的目标文件夹名称, 它们必须是不同的。(例如/php7x可以,/php7不行)

PHP 7.3.8用于文件夹D: www\php7

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

<Directory "D:\www\php7">
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php7-cgi
    </FilesMatch>
</Directory>

目录D:\www\php52

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

<Directory "D:\www\php52">
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-phpold-cgi
    </FilesMatch>
</Directory>

一些有用的提示:

如果你有php5的问题。X,同时使用编写器切换终端到7.3.8 如果你想让php 7.3.8成为windows上默认的命令行php解释器,只需将php的windows环境路径更改为7.3.8文件夹路径。 不要忘记在每个版本文件夹中编辑你需要的相关php.ini confs。

作为最后的额外提示,如果您需要设置虚拟主机的任何版本,您可以使用类似的方法。 laravel vhost conf for php 7.3.8

<VirtualHost *:80>
ServerAdmin admin@laravel.local
DocumentRoot "D:\www\vhosts\laravel.local\public"
ServerName  laravel.local
<FilesMatch "\.php$">
 SetHandler application/x-httpd-php7-cgi # escape for default php version
#SetHandler application/x-httpd-phpold-cgi # unescape for php 5.2
</FilesMatch>

ErrorLog "logs/laravel.local.log"
CustomLog "logs/laravel.local.log" combined

感谢@Munim Munna的详细回答,我想分享一个坚实的样本和上面的重要通知。

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

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

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