w1coding.comAll tutorials

PHP Functions

PHP lessons
  1. Environment Setup
  2. Introduction
  3. Variables
  4. Basics
  5. Operators
  6. Control
  7. Loops
  8. Variables
  9. Functions
  10. Arrays
  11. Superglobal
  12. File
  13. Loops
  14. Date
  15. Loops
  16. Strings
  17. Error
  18. Class
  19. File
  20. Include
  21. Sessions
  22. Mysql
  23. Cookies
  24. Forms
  25. Cookies
  26. Regular
  27. Authenthication
  28. Fileupload
  29. Mvc
  30. Responsive

Functions in PHP

Functions in PHP are blocks of code that can be reused. You define a function and then call it whenever you need it.

Defining a Function

To define a function in PHP, use the function keyword:

      
        <?php
          function greet($name) {
            echo "Hello, $name!";
          }
          greet("John");
        ?>
      
    

Activity

Try It Yourself!

Write a function that takes two numbers as parameters and returns their sum.

Quiz

Quick Quiz

  1. What keyword is used to define a function in PHP?
  2. How do you call a function in PHP?

Answers: The function keyword; Call a function by writing its name followed by parentheses (e.g., greet()).