有许多内置的PHP函数会生成通知或警告,提示您在发生问题时无法关闭,例如parse_ini_file和file_get_contents。一种常见的解决方案是使用@运算符禁止显示并通过error_get_last()函数获取警告信息:
$result = @file_get_contents($url);
if (false === $result) {
// inspect error_get_last() to find out what went wrong
}更好的方法是使用set_error_handler:
set_error_handler(function ($severity, $message, $file, $line) {
throw new \ErrorException($message, $severity, $severity, $file, $line);
});
$result = file_get_contents($url);
restore_error_handler();在这种情况下,我们注册我们自己的错误处理程序,该处理程序将每个通知,警告和错误转换为ErrorException,然后可以在其他地方捕获该错误。
但在php7以后官方会逐步统一万物皆异常,例如:
try {
$result = file_get_contents($url);
} catch (EngineException $e) {
// do something with $e
} 应用场景:PHP模拟购买,商品数量大于0才能购买常见代码:<?php //连接数据库 $con=mysqli_connect("localhost","ihuohuo","927464cy","ihuohuo");...
php多进程应用场景主要是非web端,fpm下是不支持多进程的,非类linux操作系统都不支持,请在cli模式使用.可以使用多进程做任务分发,批量计算,批量文件处理,批量爬虫,网络运维等等。下面看一份简单的入门demo//创建子进程 $pid=pcntl_fork(); //返回-1,创建失败,不...
(1).前端文件:<form action="upload.php" method="post" enctype="multipart/form-data"> &...
最近在公司开发一个新的项目假设项目域名是a.com,需要接入b.com的单点登陆系统。(1).首先我们会在a.com的登陆页面用iframe引入b.com来显示登陆界面,实际上登陆验证操作都是在b.com上面(2).当b.com验证通过,会在前端ajax请求a.com的回调地址,这个回调地址目的就是...
【一】.迭代器迭代是指反复执行一个过程,每执行一次叫做一次迭代。比如下面的代码就叫做迭代:1. <?php 2. $data = ['1', '2', &...
<?php //php7+ define('CONFIG', [ 'MYSQL' => '127.0.0.1',  ...