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

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

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

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


当前回答

我使用unisserver。

它内置了这个功能。

http://www.uniformserver.com/

就是这么简单。

统一服务器是一个免费的轻量级WAMP服务器解决方案。小于24MB,模块化设计,包括最新版本的Apache2, Perl5, PHP(在PHP53, PHP54, PHP55或PHP56之间切换),MySQL5或MariaDB5, phpMyAdmin或Adminer4。不需要安装!没有注册尘埃!打开行李,发动吧!

它甚至可以用u盘运行。有cron仿真,支持perl, mariaDB,几个版本的Mysql, filezilla服务器和其他一些东西。

其他回答

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

我推荐使用Docker,它允许你将环境分成不同的组件,并在任何时候混合和匹配你想要的组件。

Docker将允许您运行一个容器与MySQL,另一个与PHP。因为它们是独立的映像,所以你可以有两个容器,一个是PHP 5,另一个是PHP 7,你可以任意启动一个,端口80可以映射到两个容器。

https://hub.docker.com有许多预配置的映像,您可以轻松地安装和运行这些映像。

我还添加了portainer作为一个映像,它允许您从docker映像中管理docker设置的各个方面(我在启动时启动了这个容器,以避免使用命令行)。它不能为你做所有的事情,有时从命令行配置和启动图像更容易,但一旦设置好,你可以通过web界面启动和停止它们。

也可以同时运行两个容器,并将单独的端口映射到每个容器。因此,端口80可以映射到PHP 5, 81可以映射到PHP 81(或者PHP 7,如果你在2017年看这个)。

有各种关于如何安装Docker的教程(https://docs.docker.com/engine/installation/)和其他“如何”输入的东西。尝试http://www.masterzendframework.com/docker-development-environment/获取开发环境配置。

使用这个php切换器

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

您可以有两个不同版本的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.

安装一个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版本。