- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP exit()
- PHP exit() vs. break
- PHP isset()
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP mysqli Tutorial
- PHP mysqli Tutorial
- PHP and MySQL Setup
- PHP mysqli: Create Database
- PHP mysqli: Create Table
- PHP mysqli: Insert Record
- PHP mysqli: Update Record
- PHP mysqli: Fetch Record
- PHP mysqli: Delete Record
- PHP mysqli: SignUp Page
- PHP mysqli: LogIn Page
- PHP mysqli: Store User Data
- PHP mysqli Functions
- PHP mysqli_connect()
- PHP mysqli_close()
- PHP mysqli_connect_errno()
- PHP mysqli_connect_error()
- PHP mysqli_query()
- PHP mysqli_fetch_row()
- PHP mysqli_fetch_assoc()
- PHP mysqli_fetch_array()
- PHP mysqli_free_result()
- PHP mysqli_error()
- PHP mysqli_prepare()
- PHP mysqli_stmt_bind_param()
- PHP mysqli_stmt_execute()
- PHP mysqli_stmt_fetch()
- PHP mysqli_stmt_store_result()
- PHP mysqli_stmt_num_rows()
- PHP mysqli_stmt_bind_result()
- PHP mysqli_stmt_get_result()
- PHP mysqli_result class
- PHP mysqli_report()
- PHP error_reporting()
- PHP mysqli_real_escape_string()
- PHP htmlspecialchars()
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP header()
- PHP getallheaders()
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
Functions in PHP with examples
A function in PHP is a reusable block of code that performs a specific task. It enables you to group a set of related statements and execute them multiple times throughout your code without having to repeat it.
General form to create a user-defined function in PHP
function functionName(parameter1, parameter2, ...) {
// function body
return $returnValue;
}
"function" is the keyword used to define a function. The "functionName" is an identifier or a name of the function. parameter1, parameter12, and so on are the list of parameters. Now inside the function, we can create some PHP code to define the work of the function. The parameters and the "return" statement are both optional.
General form to call a user-defined function in PHP
functionName(argument1, argument2, ...);
"functionName" is the user-defined function you want to call, and "argument1," "argument2," etc. are the values you want to pass to its parameters. Skip parameters if the function has no parameters.
PHP function example
Consider the following PHP code as a basic example of a function in PHP:
<?php function codescracker() { echo "Hello there"; } codescracker(); ?>
Hello there
This PHP code defines a function named codescracker() that does not take any input parameters, but when called, it outputs the string "Hello there" to the browser or console using the echo statement. After defining the function, the code then immediately calls the function by invoking its name, codescracker(), followed by parentheses. The function call is written outside of the function definition, so it can be used elsewhere in the PHP script.
Here is another example that accepts two parameters and also returns a value.
<?php function add($x, $y) { $sum = $x + $y; return $sum; } $a = 10; $b = 20; $res = add($a, $b); echo $res; ?>
30
This is a short piece of PHP code that calls the function "add" to add up two variables and displays the results. The code accomplishes the following:
- The "add" function, which takes the two parameters $x and $y, is defined in the second line.
- The two parameters are added together inside of the "add" function, and the result is saved in the $sum variable.
- The value of $sum is returned to the function's caller using the "return" keyword.
- The next two lines set two variables, $a and $b, to the values of 10 and 20 respectively.
- The next line calls the "add" function, passing in $a and $b as arguments, and stores the returned value in a variable named $res.
- The final line outputs the value of $res to the screen using the "echo" statement.
Advantages of functions in PHP
- Reusability: Functions allow developers to write code once and then reuse it multiple times, saving them a great deal of time and effort.
- Functions facilitate modularization by breaking down complex code into smaller, more manageable units. This facilitates code readability, debugging, and maintenance.
- Functions facilitate the organization of code by separating related tasks into separate functions. This facilitates code readability, comprehension, and modification.
- Functions provide abstraction by hiding implementation details from the calling code. This means that the code calling the function does not need to understand how it operates, only what it does.
- By utilizing functions, developers can optimize and re-use code, thereby enhancing the overall performance of the application.
Disadvantages of functions in PHP
- Overuse: Sometimes, developers have a tendency to create an excessive number of functions, which can result in an overly complex codes.
- The overhead of a function call can make function calls slower than inline code. As a result, function calls can be slower than inline code. However, in most cases this is negligible and unnoticeable.
- Functions have restricted access to variables; to use a variable within a function, you must either pass it as an argument or employ the global keyword.
- Name conflicts: If two functions have the same name or if a function's name conflicts with a PHP built-in function, naming conflicts are possible.
« Previous Tutorial Next Tutorial »