WP template: Several callback functions for data judgment in WordPress have been customized. WordPress development needs to master the technology

One year ago (2023-11-26) Chief Editor
5 minutes
two hundred and forty-one
zero

When we are developing WordPress and acquiring data, we need to clean up the data. At this time, we may need to remove null and empty values from the array, and retain non null and non empty values. To facilitate these operations, I have set several callback functions for data judgment.

Judge that the data is not null

To judge whether the data is not empty, we can directly use isset. So many people, like me, want to filter out non empty characters in the array and also directly use isset as a callback function:

$data= arrayfilter($data,isset);

But the above function directly reports:

arrayfilter() expects parameter 2 to be a valid callback, function isset not found or invalid function name

This is because isset is a language constructor rather than a function, so it can only be used! Isnull to determine non null values, and then use closures:

$data= arrayfilter($data, function($item){return !isnull($item);});

There are many such arrays in the program that need to be filtered out of null values. It seems a bit inelegant to apply the closure function every time, so I defined a function called isexists.

I've been thinking about this function name for a long time. I wanted to write this function as a callback function for a long time, but I didn't have a good name. Then I saw fileexists, functionexists, and methodexists. I suddenly got inspiration, and I was excited to write the isexists function:

if(!functionexists(isexists)){function isexists($var){return isset($var);}}

Then it can be directly used for callback functions:

$data= arrayfilter($data,isexists);

Judge whether the string is empty

In PHP, empty values can be determined by using empty, but one special note is that this function will also consider the string 0 as empty. In many cases, we do not want it. For example, when the user enters a form, 0 is entered, but it has already been entered, and it is not empty. So I also created a function called ISBLANK.

Ha ha, isn't it a good name? Blank means blank. If there is 0, it is not blank. A good name is really 80% of the success in many cases. Sometimes when writing programs, I never thought that a good name would really taste bad. I tried to write isnotempty before, but I didn't think it was elegant. I thought my code would also become garbage by the way, because this is not thinking from a positive perspective. ISBLANK is different, and it is now much taller:

if(!functionexists(isblank)){function isblank($var){return empty($var)&&!isnumeric($var);}}

Similarly, if the string is not empty, I have also defined a function called ispopulated, which literally means that if it has been filled, it is not empty:

if(!functionexists(ispopulated)){function ispopulated($var){return !isblank($var);}}

In this way, sometimes when we clean up form data, for example, we need to clean up empty fields, but we can also use the following directly if we keep the 0 input:

$data= arrayfilter($data,ispopulated);

Ha ha, these three functions are very simple, but I still think I'm great, because I think these three functions are well named and can be used at a glance. In addition, the calling method of closure functions has been removed in many places, and the program has become much more elegant.

That's why we say programmers are the simplest group of people, because they will be happy for a long time if they have a good name.

This article is written by: Chief Editor Published on Software Development of Little Turkey , please indicate the source for reprinting: //hongchengtech.cn/blog/704.html
Kuke_WP editor
author

Related recommendations

1 year ago (2024-02-20)

What technologies have been applied and developed in the field of new media, and the application of new technologies in media

In the field of new media, many technologies have been applied and developed. These include: cloud computing: cloud computing technology enables new media companies to develop and deploy applications more quickly, and can dynamically adjust resources according to needs. Big data: New media companies can use big data technology to analyze massive user data, understand user preferences, behavior habits and other information
nine hundred and seventy-one
one
1 year ago (2024-02-20)

WMS warehouse management system, promoting the transformation of warehousing from extensive to fine management, WMS warehouse management

Warehousing is extremely important for the manufacturing industry and is an important guarantee for the survival and development of manufacturing enterprises. However, with the expansion of manufacturing enterprises' business, the traditional warehouse management has been unable to respond to business changes quickly. The extensive management mode not only makes the warehouse operation not smooth, but also increases the storage cost. Therefore, it is necessary for manufacturing enterprises to deploy a WMS warehouse management system
eight hundred and forty-eight
one
1 year ago (2024-02-20)

How to select MES system? Main contents of MES production management

Original title: How to select MES system? The main content of MES production management Production and manufacturing activities are the core activities of manufacturing enterprises. As the entity unit of manufacturing activities, planning objectives and the realization of enterprise value, workshop management is the focus of enterprise management. With the rapid development of manufacturing industry, MES is the focus and current hot spot of manufacturing enterprise information automation system
nine hundred and sixty-four
zero
1 year ago (2024-02-20)

The WeChat management system can manage the information content of WeChat more effectively. Let's manage the data of WeChat here

Original title: WeChat management system manages the information content of WeChat more effectively. Many enterprises will assign work to WeChat, because no one wants their personal WeChat to mix work and other related knowledge, but someone will always use the company's loopholes to do something harmful to the company's interests. Don't think such things are rare. I learned about a media financing company in Shenzhen. They have 3
seven hundred and seventy-five
zero
1 year ago (2024-02-19)

Student electronic file management system, three-dimensional communication space of "home", "school" and "community", and school electronic files

In the era of big data, in the process of recording the growth of students, the school reexamines the management of student files, moves with the times, and promotes the reform of student growth files with new thinking. "Electronic files of primary and secondary school students' growth records" have been gradually introduced into educational management, and show vigorous vitality. Student file management system is an indispensable part of the school, its content for the school
five hundred and seventy-one
zero
1 year ago (2024-02-19)

Ruizhe Information: Select Sitecore? Or... just six steps to choose a content management system, Ruizhe Information Technology Service Co., Ltd

The content management system (CMS) is an important part of the success of website construction. At present, the content management system on the network is relatively complex. It is not easy to find a good content management system that is very suitable for the current digital marketing environment. Before we compare the functionality, operation, scalability, security and other specific performance of the major CMS systems, we need to achieve
three hundred and forty-three
zero

comment

0 people have participated in the review

Scan code to add WeChat

contact us

WeChat: Kuzhuti
Online consultation: