Android程序员学PHP开发(18)-include-closure-anonymous-PhpStorm
2017-02-07 11:29
459 查看
转载请注明出处:http://blog.csdn.net/iwanghang/
觉得博文有用,请点赞,请评论,请关注,谢谢!~
* 匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数。
* 最经常用作回调函数(callback)参数的值。当然,也有其它应用的情况。
* http://php.net/manual/zh/functions.anonymous.php
* include (或 require)语句会获取指定文件中存在的所有文本/代码/标记,并复制到使用 include 语句的文件中。
* 包含文件很有用,如果您需要在网站的多张页面上引用相同的 PHP、HTML 或文本的话。
* http://www.w3school.com.cn/php/php_includes.asp
今天的练习包含4个文件,请看以下代码:(不贴图了,index.php写的打印结果,就是实际效果~)
index.php:<?php
/**
* include 包含并运行指定文件。
* include_once 在脚本执行期间包含并运行指定文件。此行为和 include 语句类似,唯一区别是如果该文件中已经被包含过,
* 则不会再次包含。如同此语句名字暗示的那样,只会包含一次。
*
* include_once 因为功能强大,所以执行效率相对低。因此,推荐使用include
*
* require 包含并运行指定文件。
* require_once 跟 include_once 也基本相同。
* require 和 include 几乎完全一样,除了处理失败的方式不同之外。require 在出错时产生 E_COMPILE_ERROR 级别的错误。
* 换句话说将导致脚本中止而 include 只产生警告(E_WARNING),脚本会继续运行。
*
* 也就是说 include 出问题报提示性警告 ,require 出问题报致命性错误 从而程序崩溃。
*
* 或者是说 一定包含的时候用require 不一定包含的时候用include。例:
* if ($a == "a"){
* include "ceshi.txt";
* }else{
* include "test.html";
* }
*
* 匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数。
* 最经常用作回调函数(callback)参数的值。当然,也有其它应用的情况。
* http://php.net/manual/zh/functions.anonymous.php */
include "function.inc.php";
include "ceshi.txt"; // 打印结果:ceshiceshiceshiceshiceshiceshi
include "test.html"; // 打印结果:testtesttesttesttesttesttesttesttesttest
one(); // 打印结果:111
two(); // 打印结果:222
three(); // 打印结果:333
echo add(10,10,20); // 这里调用的是一个包含的函数 , 打印结果:40
echo "<br>";
echo $jianfa(33,22); // 这里调用的是一个包含的闭包函数 , 打印结果:11
echo "<br>";
function callFunc($x){ // 这里调用的是一个匿名函数 , 打印结果:匿名函数
$x("匿名函数");
};
callFunc(function($str){
echo $str;
});
function.inc.php:
<?php
function one(){
echo "111<br>";
}
function two(){
echo "222<br>";
}
function three(){
echo "333<br>";
}
function add($a, $b, $c){
return $a+$b+$c;
}
/**
* 闭包函数 php5.4 新特性 但是不常用 我们一般使用下面的匿名函数写法
* 一般用于函数回调 callback
*/
$jianfa = function ($a, $b){
return $a-$b;
};
/**
* 匿名函数
*/
// callFunc(function($str){
// echo $str;
// });
test.html:testtesttesttesttesttesttesttesttesttest<br>
ceshi.txt:
ceshiceshiceshiceshiceshiceshi<br>
转载请注明出处:http://blog.csdn.net/iwanghang/
欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式
微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com
觉得博文有用,请点赞,请评论,请关注,谢谢!~
觉得博文有用,请点赞,请评论,请关注,谢谢!~
* 匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数。
* 最经常用作回调函数(callback)参数的值。当然,也有其它应用的情况。
* http://php.net/manual/zh/functions.anonymous.php
* include (或 require)语句会获取指定文件中存在的所有文本/代码/标记,并复制到使用 include 语句的文件中。
* 包含文件很有用,如果您需要在网站的多张页面上引用相同的 PHP、HTML 或文本的话。
* http://www.w3school.com.cn/php/php_includes.asp
今天的练习包含4个文件,请看以下代码:(不贴图了,index.php写的打印结果,就是实际效果~)
index.php:<?php
/**
* include 包含并运行指定文件。
* include_once 在脚本执行期间包含并运行指定文件。此行为和 include 语句类似,唯一区别是如果该文件中已经被包含过,
* 则不会再次包含。如同此语句名字暗示的那样,只会包含一次。
*
* include_once 因为功能强大,所以执行效率相对低。因此,推荐使用include
*
* require 包含并运行指定文件。
* require_once 跟 include_once 也基本相同。
* require 和 include 几乎完全一样,除了处理失败的方式不同之外。require 在出错时产生 E_COMPILE_ERROR 级别的错误。
* 换句话说将导致脚本中止而 include 只产生警告(E_WARNING),脚本会继续运行。
*
* 也就是说 include 出问题报提示性警告 ,require 出问题报致命性错误 从而程序崩溃。
*
* 或者是说 一定包含的时候用require 不一定包含的时候用include。例:
* if ($a == "a"){
* include "ceshi.txt";
* }else{
* include "test.html";
* }
*
* 匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数。
* 最经常用作回调函数(callback)参数的值。当然,也有其它应用的情况。
* http://php.net/manual/zh/functions.anonymous.php */
include "function.inc.php";
include "ceshi.txt"; // 打印结果:ceshiceshiceshiceshiceshiceshi
include "test.html"; // 打印结果:testtesttesttesttesttesttesttesttesttest
one(); // 打印结果:111
two(); // 打印结果:222
three(); // 打印结果:333
echo add(10,10,20); // 这里调用的是一个包含的函数 , 打印结果:40
echo "<br>";
echo $jianfa(33,22); // 这里调用的是一个包含的闭包函数 , 打印结果:11
echo "<br>";
function callFunc($x){ // 这里调用的是一个匿名函数 , 打印结果:匿名函数
$x("匿名函数");
};
callFunc(function($str){
echo $str;
});
function.inc.php:
<?php
function one(){
echo "111<br>";
}
function two(){
echo "222<br>";
}
function three(){
echo "333<br>";
}
function add($a, $b, $c){
return $a+$b+$c;
}
/**
* 闭包函数 php5.4 新特性 但是不常用 我们一般使用下面的匿名函数写法
* 一般用于函数回调 callback
*/
$jianfa = function ($a, $b){
return $a-$b;
};
/**
* 匿名函数
*/
// callFunc(function($str){
// echo $str;
// });
test.html:testtesttesttesttesttesttesttesttesttest<br>
ceshi.txt:
ceshiceshiceshiceshiceshiceshi<br>
转载请注明出处:http://blog.csdn.net/iwanghang/
欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式
微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com
觉得博文有用,请点赞,请评论,请关注,谢谢!~
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- Laravel5.3 表单验证中文报错提示语言包
- 10 件在 PHP 7 中不要做的事情
- JetBrains系列IDE快捷键大全
- 写程序的时候应该如何思考?
- rem : web app适配的秘密武器
- jquery高级应用之Deferred对象
- 关于浮动与清除浮动,你应该知道的
- 数组reduce方法的高级技巧
- php7 读取php.ini[4]