Creating Functions in PHP
PHP has over 1000 built-in functions that can be called from within a script to perform specific tasks. A function is a block of code that can be called multiple times to execute a series of statements at once. A function can return a single value or an array of values.
Creating a function starts with the keyword function, followed by a name for the function and then the function code inside curly (
If a variable is used both inside and outside of a function, it must be declared as global. This is because PHP allows variables to be passed by reference, and passing by reference creates a new entry in the internal data structure that represents a variable’s value. If a variable is not declared as global, each time it is used, it represents a different value or memory space inside the function.
PHP also supports Type Declarations for function returns. This makes it easier to identify the return type for a function and prevents PHP from throwing a fatal error on a mismatched return type. To use Type Declarations, just add a type name to the return statement in your function definition, right before the opening curly (