PHP background interface problems

Source: Internet
Author: User
Tags try catch
1. Use PHP for background compilation of some interfaces. Use try {code ...}} catch (Exception $ e) {code ...}} and use if (isset ($ a) & amp ;! Empty ($ a) {} What is the difference? 1. Use PHP for background compilation of some interfaces. Use
Try {

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

} Catch (Exception $ e ){

echo $e->getMessage();

}

And use
If (isset ($ )&&! Empty ($ )){

}

Which one is better?

Reply content:

1. Use PHP for background compilation of some interfaces. Use
Try {

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

} Catch (Exception $ e ){

echo $e->getMessage();

}

And use
If (isset ($ )&&! Empty ($ )){

}

Which one is better?

Exception Handling and condition determination are obviously not at the same level! Exception Handling can help programs increase maintainability, for example:

In function and function call scenarios

UseCondition judgmentThe Code is as follows:


  

If you useException HandlingThe Code is as follows:

 GetMessage ();}

Now, if we use conditions to determine whether to add a parameter or modify the error response code to-2, we need to modify the external call code.

However, if we use the exception handling code, because the external code is captured by try catch, all the code that only needs to care about how to modify the business logic, such

 GetMessage ();}

Not good...
For the first example, if empty ($ a) is false, then! Isset ($ a) must also be false. If empty ($ a) is true, the user will not judge it later.
For the second example, the same principle
Therefore, your code uses if (! Empty ($.

Isset is more used to detect array subscript, such

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

If this check is not performed, a Notice-level error may occur.
For common variables, the common process is

If (empty ($ a) {$ a = 0; // because $ a may be null, null, 0, false, it must be expressed as 0 .}

Check whether the try catch structure is used to determine the project code specification. However, your usage is incorrect.

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

You can just give it to someone else to capture and handle it. You can throw an exception and then capture it as a metaphysics (here you refer to the current domain ). Are there any differences between the following two statements?

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

Try... catch aims to catch exceptions thrown during the program running.
In the first section, the whole program is packaged by try, and the service exception is thrown as a program exception.
The second code is also written in many projects. An error is returned when a business exception or error occurs.
Compared with the second paragraph, There is no good or bad, only whether it meets the current project.
This kind of code design depends onDevelopment specifications of the team.

At present, I also throw business exceptions as code exceptions and catch all exceptions at the outermost layer.
Perform different processing for different types of exceptions, such as whether to write logs and whether to trigger alarms.

Hello: First, you need to distinguish isset () from empty (). isset () determines whether the variable or value is set, so it does not matter if it is null or not, empty () is used to determine whether the value is null and whether the value exists.

Personal suggestionif(isset($a) && !empty($a)){}For judgment!

Personal Opinion !!!

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.