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

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

那么,是否有一种方法可以轻松地在XAMPP中更改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)

其他回答

我正在做一个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的详细回答,我想分享一个坚实的样本和上面的重要通知。

除非它必须是绝对的XAMPP,否则您可以尝试使用WAMP得到您想要的东西。 WAMP几乎是相同的东西,只是包装不同。

一旦你安装了它,你可以在这里切换php版本:

您可以根据需要安装任意版本的PHP。

下面是具体的操作步骤。

点击这里:WAMP下载

然后选择你的基础服务器,例如最新的php7。

然后,当WAMP 3安装完成后,进入文件夹:addons并选择你想要的php版本。他们提供了从php 5.3.29开始的php版本,它应该与mysql_connect一起工作。

要安装插件,在安装过程中(双击exe文件)只需指向安装WAMP 3的文件夹。

然后您可以使用其他文件夹的内容,如:应用程序等,以添加更多的功能

所有东西都是接口的,所以您可以专注于编码,而不是破坏您的环境。

也许有点晚了,但我正在使用批处理重命名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);

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

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

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