OnlineCBT Logo OnlineCBT
PHP · LESSON 7

Functions and Type Hinting

By Ratan Updated: 16 Oct 2026

Write reusable functions, pass arguments by reference (&), define default values, and enforce scalar type hints.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

1. Defining and Calling Functions

A function is a self-contained block of statements designed to perform a distinct task. Functions enhance code reusability, modularity, and testing.

2. Function Arguments and Default Parameters

You can provide default values for parameters, making them optional when calling the function:

3. Pass by Value vs Pass by Reference

By default, function arguments are passed by value (a copy of the variable is created). To modify the original variable directly, prepend an ampersand (&) to the parameter declaration:

4. Type Hinting and Return Type Declarations

Starting with modern PHP 7 and 8, you can enforce strict type safety for parameters and return values:

5. Anonymous Functions and Arrow Functions

 $amount + ($amount * $taxRate);
echo $calculateTotal(100); // 118

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
पता संपर्क करें

+91 7877547686

ई-मेल

onlinecbtportal@gmail.com

हेल्पलाइन नंबर

+91 7877547686


डाउनलोड करने के लिए क्लिक करें
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat