include/require

如果没有提供路径,则会查找include_path,如果没有找到,则会查找当前脚本所在目录和当前工作目录。如果均失败的话,include 会产生一个 warnning,require会产生一个 fatal error.

当一个文件被 included时,被包含文件中的代码继承了include所在行的变量作用域。任何在include处可见的变量,对于被包含的代码也可见(相当于将被包含的代码复制并替换include命令。)但是,被包含文件中定义的函数和类都拥有全局作用域。

在解析被包含的文件时,解析器会切换为 HTML模式,所以任何没有包含在 php tag中的文本都会当做HTML处理,也就是会被原样 echo 给客户端。

include失败会返回FALSE,并产生一个warning。如果include成功,并且被包含文件中没有return语句,include会返回1。 如果被包含文件中有return语句,那么执行到return语句时,会停止对被包含文件的处理,并返回return提供的值,就像调用一个普通的函数一样。

如果被包含文件中定义了函数,并且被包含了两次,则会造成 fatal error,因为函数已经被声明过了。最好使用 include_once。

安装

  • sudo apt-add-repository ppa:ondrej/php5
  • php-xxx都是PHP的扩展包,比如php-gdphp-mysqlphp-xdebug需要手动安装
  • Apache 通过 libapache2-mod-php7.1模块与PHP互动,配置文件为/etc/apache2/mod-enabled/php7.0/
  • /usr/bin/php也只是一个alternative,可以使用update-alternative改变当前php命令使用的版本

安装过程中的处理:

Setting up php7.1-common (7.1.0-5+deb.sury.org~xenial+1) ...

Creating config file /etc/php/7.1/mods-available/calendar.ini with new version

Creating config file /etc/php/7.1/mods-available/ctype.ini with new version

Creating config file /etc/php/7.1/mods-available/exif.ini with new version

Creating config file /etc/php/7.1/mods-available/fileinfo.ini with new version

Creating config file /etc/php/7.1/mods-available/ftp.ini with new version

Creating config file /etc/php/7.1/mods-available/gettext.ini with new version

Creating config file /etc/php/7.1/mods-available/iconv.ini with new version

Creating config file /etc/php/7.1/mods-available/pdo.ini with new version

Creating config file /etc/php/7.1/mods-available/phar.ini with new version

Creating config file /etc/php/7.1/mods-available/posix.ini with new version

Creating config file /etc/php/7.1/mods-available/shmop.ini with new version

Creating config file /etc/php/7.1/mods-available/sockets.ini with new version

Creating config file /etc/php/7.1/mods-available/sysvmsg.ini with new version

Creating config file /etc/php/7.1/mods-available/sysvsem.ini with new version

Creating config file /etc/php/7.1/mods-available/sysvshm.ini with new version

Creating config file /etc/php/7.1/mods-available/tokenizer.ini with new version
Setting up php7.1-json (7.1.0-5+deb.sury.org~xenial+1) ...

Creating config file /etc/php/7.1/mods-available/json.ini with new version
Setting up php7.1-opcache (7.1.0-5+deb.sury.org~xenial+1) ...

Creating config file /etc/php/7.1/mods-available/opcache.ini with new version
Setting up php7.1-readline (7.1.0-5+deb.sury.org~xenial+1) ...

Creating config file /etc/php/7.1/mods-available/readline.ini with new version
Setting up php7.1-cli (7.1.0-5+deb.sury.org~xenial+1) ...
update-alternatives: using /usr/bin/php7.1 to provide /usr/bin/php (php) in auto mode
update-alternatives: using /usr/bin/phar7.1 to provide /usr/bin/phar (phar) in auto mode
update-alternatives: using /usr/bin/phar.phar7.1 to provide /usr/bin/phar.phar (phar.phar) in auto mode

Creating config file /etc/php/7.1/cli/php.ini with new version
Setting up libapache2-mod-php7.1 (7.1.0-5+deb.sury.org~xenial+1) ...

Creating config file /etc/php/7.1/apache2/php.ini with new version
libapache2-mod-php7.1: php7.0 module already enabled, not enabling PHP 7.1
Setting up libapache2-mod-php (1:7.1+49+deb.sury.org~xenial+4) ...
Setting up php7.0-common (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-mcrypt (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-mbstring (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-xml (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-readline (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-opcache (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-mysql (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-json (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up libwebp5:amd64 (0.4.4-1+deb.sury.org~xenial+1) ...
Setting up libgd3:amd64 (2.2.3-3+deb.sury.org~xenial+0) ...
Setting up php7.0-gd (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-curl (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up php7.0-cli (7.0.14-2+deb.sury.org~xenial+1) ...
Replacing config file /etc/php/7.0/cli/php.ini with new version
Setting up libapache2-mod-php7.0 (7.0.14-2+deb.sury.org~xenial+1) ...
Installing new version of config file /etc/apache2/mods-available/php7.0.load ...
libapache2-mod-php7.0: not switching MPM - already enabled
Setting up php7.0 (7.0.14-2+deb.sury.org~xenial+1) ...
Setting up libjbig0:amd64 (2.1-3.1+deb.sury.org~xenial+1) ...
Setting up libpcre16-3:amd64 (2:8.39-1+deb.sury.org~xenial+1) ...
Setting up libsodium18:amd64 (1.0.10-1+deb.sury.org~xenial+1) ...
Setting up libssl-doc (1.0.2j-1+deb.sury.org~xenial+1) ...
Setting up libssl-dev:amd64 (1.0.2j-1+deb.sury.org~xenial+1) ...
Setting up libwebpmux1:amd64 (0.4.4-1+deb.sury.org~xenial+1) ...
Setting up libzmq5:amd64 (4.2.0-1+deb.sury.org~xenial+1) ...
Setting up openssl (1.0.2j-1+deb.sury.org~xenial+1) ...
Setting up php7.1-curl (7.1.0-5+deb.sury.org~xenial+1) ...

Creating config file /etc/php/7.1/mods-available/curl.ini with new version
Setting up php-curl (1:7.1+49+deb.sury.org~xenial+4) ...
Setting up php7.1-gd (7.1.0-5+deb.sury.org~xenial+1) ...

Creating config file /etc/php/7.1/mods-available/gd.ini with new version
Setting up php-gd (1:7.1+49+deb.sury.org~xenial+4) ...
Setting up php7.1-xml (7.1.0-5+deb.sury.org~xenial+1) ...

Creating config file /etc/php/7.1/mods-available/dom.ini with new version

Creating config file /etc/php/7.1/mods-available/simplexml.ini with new version

Creating config file /etc/php/7.1/mods-available/wddx.ini with new version

Creating config file /etc/php/7.1/mods-available/xml.ini with new version

Creating config file /etc/php/7.1/mods-available/xmlreader.ini with new version

Creating config file /etc/php/7.1/mods-available/xmlwriter.ini with new version

Creating config file /etc/php/7.1/mods-available/xsl.ini with new version
Setting up php-xml (1:7.1+49+deb.sury.org~xenial+4) ...
Setting up php-pear (1:1.10.1+submodules+notgz-8+donate.sury.org~xenial+2) ...
Setting up php-gettext (1.0.11-2+deb.sury.org~xenial+1) ...
Setting up php7.1-mbstring (7.1.0-5+deb.sury.org~xenial+1) ...

Creating config file /etc/php/7.1/mods-available/mbstring.ini with new version
Setting up php-mbstring (1:7.1+49+deb.sury.org~xenial+4) ...
Setting up php7.1-mcrypt (7.1.0-5+deb.sury.org~xenial+1) ...

Creating config file /etc/php/7.1/mods-available/mcrypt.ini with new version
Setting up php-mcrypt (1:7.1+49+deb.sury.org~xenial+4) ...
Setting up php7.1-mysql (7.1.0-5+deb.sury.org~xenial+1) ...

Creating config file /etc/php/7.1/mods-available/mysqlnd.ini with new version

Creating config file /etc/php/7.1/mods-available/mysqli.ini with new version

Creating config file /etc/php/7.1/mods-available/pdo_mysql.ini with new version
Setting up php-mysql (1:7.1+49+deb.sury.org~xenial+4) ...
Setting up php-xdebug (2.5.0-1+deb.sury.org~xenial+1) ...
Processing triggers for libc-bin (2.23-0ubuntu5) ...
Processing triggers for libapache2-mod-php7.1 (7.1.0-5+deb.sury.org~xenial+1) ...
  • 问题,现在通过a2enmod php7.1启用了PHP7.1,那么对于PHP的请求,会交给PHP7.0呢?还是PHP7.1呢? =》 默认的还是PHP7.0,需要自己手动 a2dismod php7.0才行
  • php-pear是什么?