PHP Lesson 5: Functions (Building Reusable Code Blocks) Welcome back! As your programs grow, you'll find yourself performing the same tasks repeatedly (e.g., calculating sales tax, validating a user's input, or formatting a date). Functions allow you to bundle these tasks into a single, reusable unit, avoiding repetition. 1. What is a Function? A function is a block of code written to perform a specific task. Once defined, you can execute that code block anytime and anywhere in your program simply by calling the function's name. 2. Defining Your Own Function In PHP, you define a function using the function keyword, followed by the function's name and parentheses () . Basic Function Example PHP <?php // 1. Function Definition function sayHello ( ) { echo "Hello, welcome to my PHP lesson! <br>" ; } // 2. Calling the function (Executing the code block) sayHello(); sayHello(); // Output: // Hello, welcome ...
We'll provide you everything! Thanks for being here!