Skip to content
Home Blog Web Design A Guide to Writing Functions

A Guide to Writing Functions

  • Web Design
Blueprint Digital
Blueprint Digital

Insights from the Blueprint Digital team.

If you find yourself writing the same few lines of code more than twice, convert those lines into a PHP function. In order to minimize the amount of code for websites and web applications, call functions within functions. The most effective functions are ones that are reusable.

Naming Functions

The names you choose for your functions should provide a good hint about what the function does. However, you don’t want your function name to be too long or too specific in case you want to modify it later.

For example, suppose you have a function called getHomePhoneNumber($clientID) that retrieves and returns a home phone number of a specific client stored in a database. Later, you want to retrieve the client’s cell phone number. You would either have to write another function, identical to getHomePhoneNumber($clientID); or you would have to change the name of the existing function to getPhoneNumber($clientID) and edit all calls to that function; or leave the name as getHomePhoneNumber($clientID), which could lead to confusion. Prudently choosing the function name when you write it maximizes efficiency.

Additionally, when creating a plugin for WordPress, it is wise to use prefixes that indicate plugin package. For example, if you create a plugin called ‘Banner Editor’, you should prefix all functions with ‘be_’. This naming practice prevents important functions in other plugin packages from being overwritten.

Choosing Function Arguments

Use arguments for your functions. However, using too many arguments could get messy and unmanageable. If you find yourself using more than three arguments in a function, it it probably a good idea to split that function up into smaller functions.

PHP allows you to set default argument values in your functions. The benefit of default argument values is that it allows you to call the function multiple times without having to set default values each time. It also allows you to leave arguments blank when the function is called, which results in clean and concise coding. Default argument values are always overwritten if you assign values to the arguments when you call the function.

Ready to Dominate Online and Grow Your Business?

Schedule time to connect with Blueprint about your online goals, or request a free review of marketing campaigns.

Related Posts

What Is Automated Lead Follow-Up and How Does It Work

What Is Automated Lead Follow-Up and How Does It Work

A new lead fills out a form, then waits. If the phone stays silent for a day, that lead has usually moved on to whoever answered first. Automated lead follow-up closes the gap. It is a marketing automation system that responds to every new inquiry within minutes, then continues the conversation through scheduled texts, emails,[...]
Free Advertising for Nonprofits Through Google Ad Grants

Free Advertising for Nonprofits Through Google Ad Grants

Advertising is usually the first line cut from a nonprofit budget, and the one that quietly costs the most to lose. Donors still search, scroll, and read, whether or not the cause has money to meet them there. Free advertising for nonprofits means promoting a cause without paying for placements, and it is how organizations[...]
Lead Scoring for B2B: How to Decide Which Leads Your Sales Team Chases

Lead Scoring for B2B: How to Decide Which Leads Your Sales Team Chases

Every marketing team has handed sales a list of leads that went nowhere. After a few of those lists, sales stops trusting them, works its own referrals instead, and the two teams settle into a standing argument about lead quality. Underneath the argument is a simpler problem: nobody has written down what a good lead[...]
Previous
Next

Partner with BLUEPRINT to reach your online goals, grow your business and reshape your story.

Get in touch with BLUEPRINT

Reach out to request a discovery call, a free campaign review, or for all other inquiries.

Subscribe to our newsletter