Community Blog PHP Best Practices: Good Semantic Expressions for Your Code

PHP Best Practices: Good Semantic Expressions for Your Code

In this article, we will discuss the importance of having good semantic expressions in your code for collaborative coding and for troubleshooting unknown code issues.

Do not make your code unintelligible to others, because you might be one of the others who read the unintelligible code after one week. Always supervise yourself by bearing in mind that another developer may take a long time to process bugs if your code snippets have some faults.

In your everyday programming practices, make deliberate efforts to improve your semantic expressions. This will benefit you a lot. Specifically, how can we achieve good semantic expressions?


Code samples in this article are only my test code and do not belong to any real projects.

Naming Your Functions and Parameters

Case 1


Case 2

With this function name and parameter name alone, who can figure out the actual meaning of this parameter?


Case 3


Case 4

Assume the following classes are included in our business code.

However, we find the following when querying the database:


In this case, it is inconvenient to find corresponding tables according to the business code. Others will also feel confused when taking over our project. This inconvenience maybe caused by three iterative development cycles from three different persons, neither of whom has referred the naming rules used previously.

Additional Remarks


Adding Comments to Your Code

After the preceding description about naming, now let's move on to the comment. Is it even necessary to talk about comments? Are you kidding me?

Those questions may come to your mind. However, do you know how to write a comment on an array member object?

Do you know how to write comments on calls on magic methods of a class?

Object Array



The Use of @method


The Use of @deprecated


Additional Considerations

Comments and descriptions do match correctly. Method names are updated, while the business and feature comments for the methods are not updated. The @author information entry is also copied during the process of copying others' code and the original code author is ends up taking the blame for the code errors.

For more information about comments, refer to the web page http://manual.phpdoc.org/HTMLframesConverter/default/

Functions and Methods

Case 1

First, I want to make one thing clear: Code with poor readability does not necessarily mean that the software is not good. It is common to see many bad PHP and MySQL code snippets.

The following is a code snippet of an open-source project. The feature is actually excellent. However, this method has too much content. I have listed some shortcomings of the code in the figure.


Case 2

Do you still remember this figure?


Optimization solution 1Optimization solution 2

$startId and $lastId are repeated parameters.

Case 4

Minimize parameter references

function bad($input1, $input2, &$input3)

    $input3 = "xxx";

    return true;

Case 5

Ensure the parameter type and the type of return values are clearly described and avoid the use of mixed types. Let's take the following official code for example. It is also necessary to be skeptical about authoritative practices. This is only my opinion.


Case 6


In this example, the addUser is written in a way that makes it more like a remote API interface rather than a function (method). In the code snippet shown on the right side, we need to make judgement based on is_array every time we use it. This is an unfriendly semantic expression. Exceptions may occur in advanced languages like PHP and Java. We need to make good use of exceptions.


Good semantic expressions make it easier to implement efficient iteration in a collaborative team and troubleshoot unknown code issues online. This is the end of this blog. Have you learned something new?


3 0 0
Share on


3 posts | 1 followers

You may also like


5381571195078949 December 16, 2022 at 12:57 pm


5381571195078949 December 16, 2022 at 1:06 pm

Thankful for sharing this data. https://www.pinkgoa.in/ I truly like your blog zone no delicacy on the planet.

5381571195078949 March 1, 2023 at 4:28 am

Our escorts will know what to expect when they come to you, which is why we always recommend that you meet in advance of the date. https://www.thegoaescort.com/ You can talk with them about what you would like to do during the appointment and have an opportunity to get comfortable with each other before you decide on your next steps.


3 posts | 1 followers

Related Products