Functions In PHP


- Advertisement -

A function is an independent block of code that takes input in the form of parameters and returns a result to the caller. In other words,  a function can also be described as a block of code that performs a specific function. Functions in PHP are no different than function in any other programming language.

    • A block of code for specific tasks.
    • Save compile-time –only compile once.
    • Reuse many times when needed.
    • Optimize your code.

Built-in functions like print(); date();

      • User-defined functions:
      • Function names cannot have spaces.
      • The function name must begin with a letter or underscore.

E.g., call a built-in function abs();

Declaring a Function

      • You can declare a function below a call to it.
      • May have none, one, or multiple arguments each separated by a comma.
      • Write function name followed by ( ), even if the function doesn’t have any arguments.


Functions – Return Value

      • Functions may have a return value.
      • The return value in a function returns the value to the function that may be used later on while calling a function.
      • You may need to access the value that the function returns, but you may not need to print it out, that’s why you use a return value for a function.

Example-Functions – Return Value

Example-Functions – Without Return Value

PHP built-in Functions
Sqrt(); — Take the square root of a number
Ceil(); — Take ceil of a decimal number
Floor(); — take the floor of a decimal number
Max(); — take the max number
Min(); — take the min number
Strtolower(); — convert string to lower case
Strtoupper(); — convert string to upper case

- Advertisement -

Leave A Reply

Your email address will not be published.