php引入文件的方式有什么,php引入文件的方法有哪些_后端开发

php怎么去除空白字符_后端开发

在php中可以使用函数“preg_replace”将白字符去除,该函数的作用执行一个正则表达式的搜索和替换,使用时需传入3个参数,第1个参数为正则表达式“/\s+/”,第2个参数为空,第3个参数为要去除空白字符的字符串,最后接收返回值即可。

854c651ec19d611e864f35e493a0c027.png

PHP中引入文件的方法有:include、require、include_once、require_once。

区别介绍:

include和require

include有返回值,而require没有返回值。

include在加载文件失败时,会生成一个警告(E_WARNING),在错误发生后脚本继续执行。所以include用在希望继续执行并向用户输出结果时。

//test1.php

include './tsest.php';

echo 'this is test1';

?>

//test2.php

echo 'this is test2\n';

function test() {

echo 'this is test\n';

}

?>

//结果:

this is test1

require在加载失败时会生成一个致命错误(E_COMPILE_ERROR),在错误发生后脚本停止执行。一般用在后续代码依赖于载入的文件的时候。

php如何设置中国时区_后端开发

php设置中国时区的方法:1、打开php.ini文件搜索配置项“date.timezone”,将配置项对应的值更改为“PRC”;2、使用php内置函数“date_default_timezone_set”,将默认时区设置为“PRC”即可。

//test1.php

require './tsest.php';

echo 'this is test1';

?>

//test2.php

echo 'this is test2\n';

function test() {

echo 'this is test\n';

}

?>

结果:

cba26a7b243f6b4b5e0ce0ec4c5566dc.png

include和include_once

include载入的文件不会判断是否重复,只要有include语句,就会载入一次(即使可能出现重复载入)。而include_once载入文件时会有内部判断机制判断前面代码是否已经载入过。

这里需要注意的是include_once是根据前面有无引入相同路径的文件为判断的,而不是根据文件中的内容(即两个待引入的文件内容相同,使用include_once还是会引入两个)。

//test1.php

include './test2.php';

echo 'this is test1';

include './test2.php';

?>

//test2.php

echo 'this is test2';

?>

//结果:

this is test2this is test1this is test2

//test1.php

include './test2.php';

echo 'this is test1';

include_once './test2.php';

?>

//test2.php

echo 'this is test2';

?>

//结果:

this is test2this is test1

//test1.php

include_once './test2.php';

echo 'this is test1';

include './test2.php';

?>

//test2.php

echo 'this is test2';

?>

//结果:

this is test2this is test1this is test2

//test1.php

include_once './test2.php';

echo 'this is test1';

include_once './test2.php';

?>

//test2.php

echo 'this is test2';

?>

//结果:

this is test2this is test1

require和require_once:同include和include_once的区别相同。

更多相关教程请访问ki4网。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值