PHP Functions
PHP Functions are code blocks that can be executed at runtime. They are defined using the keyword function, followed by the name of the function, and then the parentheses which contain the parameters and the curly braces which contain the code block. When a function is called, the php parser searches for the function’s name in the registered symbol table and then executes it. The value that the function returns is returned by the return statement in the function’s code block.
Variables used within a function have local scope. This means that the variable only exists in the code block where it is declared. If you want to be able to access the same variable both inside and outside of a function, you need to use the global keyword.
Information can be passed to a function through arguments, which are just like variables. The argument list is placed after the function name in the parentheses and you can add as many arguments as you like, separated by comma. The number of arguments and their type is a matter of preference for the programmer.
If you declare the data type of an argument as strict, PHP will ensure that only values that match the function’s expected data type are passed to it. This helps prevent mistakes that can lead to PHP Fatal Errors.