Some problems with the PHP backend interface

Source: Internet
Author: User
Keywords Php
Tags try catch
1. Use PHP as a background on some of the interface writing problems. Use
try{

if(empty($a) || !isset($a)){    throw new Exception("***")}

} catch (Exception $e) {

echo $e->getMessage();

}

and using
if (isset ($a) &&!empty ($a)) {

}

What kind of a good difference?

Reply content:

1. Use PHP as a background on some of the interface writing problems. Use
try{

if(empty($a) || !isset($a)){    throw new Exception("***")}

} catch (Exception $e) {

echo $e->getMessage();

}

and using
if (isset ($a) &&!empty ($a)) {

}

What kind of a good difference?

Exception handling and conditional judgment, obviously not a level of things! Exception handling can help the program increase maintainability, for example:

function and function calls in the scene

The code below is used to determine the condition :


  
   

如果使用异常处理的代码如下:

 getMessage();}

现在我们假设使用条件判断增加参数或者修改错误返回代码为-2,都需要修改外部调用代码

但是如果我们用异常处理的代码,外部因为是通过try catch来捕获的,所有只需要关心如何修改业务逻辑的代码即可,比如

 getMessage();}

is not good ...
For the first example, if empty ($a) is false, then!isset ($a) must also be false, and if empty ($a) is true, the user presses no subsequent judgment
For the second example, the same principle
So, your code directly with if (! empty ($a)) on the line.

Isset is more used to detect array subscripts, such as

if (isset($_POST['username'])) {    $username = $_POST['username'];} else {    $username = '';}

Failure to do this check may cause a notice level of error.
And for ordinary variables, the common treatment is

if (empty($a)) {    $a = 0; //因为$a可能是null, 空, 0, false,所以强调一下用0值表示。}

about using the try catch structure to see how you set up the project coding specification. But your usage is wrong.

if (empty($a)) {    throw new Exception('error');}

It is possible to hand over to others to capture and handle, throw an exception yourself and then capture yourself as metaphysics (here you refer to the current domain). Do you think there is a difference between the following two types of notation?

try{    if(empty($a))throw new Exception('error');}catch(Exception $e) {    die('错误');}if (empty($a)) die('错误');

The goal of Try...catch is to catch exceptions thrown in the program's run.
In the first paragraph, the whole program is wrapped in a try and the business exception is thrown as a program exception.
The second piece of code is also written in many projects, and the business exception error responds with an error.
Compared to the second paragraph, there is no good or bad, only if it conforms to the current project.
This code design depends on the team's development specifications .

Now I am also here to put the business exception as a code exception thrown, at the outermost catch all exceptions.
For different types of exceptions do different processing, such as whether to write logs, whether the alarm.

Hello: First you distinguish isset () and empty () different, isset () determine whether the variable or value is set, as for the null is not empty regardless, and empty () judge whether the value is empty, and regardless of whether the value exists, only do non-null judgment.

Personal advice if(isset($a) && !empty($a)){} to use to make judgments!

Personal views!!!

How should I answer this question?

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.