PHP 101: Understanding Functions

Stephanie De Smedt
5 min readApr 20, 2022
Photo by ThisisEngineering RAEng on Unsplash

In order to access the full strength of PHP we need to understand how to create functions. Functions are blocks of code that can be called upon multiple times throughout a project to achieve a specific goal. They can save a lot of time and repetition throughout the course of a project so are a very powerful tool.

Basic Function Syntax

A PHP function is setup as follows:

function my_function_name (input1, input2) { 
…function body …
}
  • “function” — This keyword tells the program that a function is being declared
  • “my_function_name” — The name you will use when calling your function. The name you choose must start with either a letter (a-z) or an underscore. Be careful to make the name concise yet specific. You shouldn't need to comments to understand what your function is aimed to do.
  • (input1, input2) — A set of brackets enclosing all the inputs needed for your function. If no inputs are needed then just enter a pair of empty brackets
  • {….} — Finally add a set of the curly brackets and within them write the code you would like your function to execute.

Return

--

--

Stephanie De Smedt

Project Manager learning Web and Mobile Development. A lifelong learner with a travel and coffee problem.