尝试{inet_ntop($ row ['ip'])...}在PHP中捕获{优美地} [英] try {inet_ntop($row['ip'])...} catch{gracefully} in PHP

查看:73
本文介绍了尝试{inet_ntop($ row ['ip'])...}在PHP中捕获{优美地}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理从MySQL数据库打印地址的表.我不确定我是否很好地理解try/catch块.表中的旧记录没有IP地址,但新记录的确具有IP地址.新记录(带有IP地址)可以很好地打印出来,但前提是我将其放在如下所示的try catch中:

I am working on table that prints addresses from a MySQL database. I'm not sure I understand try/catch blocks very well. Old records have no IP address but new records do have an IP address in the table. The new records (with an IP address) print out fine, but only if I put it in a try catch like below:

try {
   echo inet_ntop($row['ip']);
}
catch (Exception $e){
  //echo 'Exception caught: ',  $e->getMessage(), "\n";
            echo "n/a";
}

在IP字段中没有IP的记录会打印出一个难看的错误.如上所示,我注释掉了该错误,但是无论如何它都会打印一个错误.如何正确地打印一张包含当前IP地址(或缺少0f)的表,而不必查看所有这些错误:

The records that don't have an IP in the IP field print out an ugly error. As show above, I commented out the error, but it prints an error anyway. How can I properly print a table full of the present IP addresses (or lack 0f) without having to look at all of these errors:

Warning: inet_ntop() [function.inet-ntop]: Invalid in_addr value in/home/zp/public_html/example.COM/example.php on line 57

推荐答案

警告不能 catch . try..catch 块仅适用于 Exception .警告是完全不同的错误报告机制.要禁止显示警告,您可以使用错误控制运算符:

Warnings are not catchable. try..catch blocks work on Exceptions only. Warnings are a different error reporting mechanism entirely. To suppress warnings, you can use the error control operator:

$ip = @inet_ntop($row['ip']);
echo $ip ? $ip : 'n/a';

当然,您应该通过首先验证传递给 inet_ntop 的值来完全避免警告.至少:

Of course, you should avoid warnings altogether by validating the values you pass into inet_ntop first. At the very least:

echo $row['ip'] ? inet_ntop($row['ip']) : 'n/a;

这篇关于尝试{inet_ntop($ row ['ip'])...}在PHP中捕获{优美地}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆