Functions

Back to index

Example 1: Built-in function

Original: asif khan

strtoupper(): ASIF KHAN

strlen(): 9 characters

Example 2: User-defined function

3 donuts at $1.50 each = $4.50

2 coffees at $2.75 each = $5.50

Explanation

A function is a block of code that performs a specific task. PHP already has built-in functions such as strtoupper, strlen, and number_format. We can also create our own functions when we need to do something specific. The user-defined function here calculates the total cost by multiplying the quantity and price. Functions help make the code easier to use and understand.