PHP Functions


In this lesson, you will learn about functions in PHP. A function is a reusable piece of code that performs some kind of specific action. Mostly, functions take some kind of input data, process that data and return results when called. PHP gives over 700 built-in functions to make developers life easy.

Why you will need functions?

  1. Functions make the whole development process really easy. It uses the divide and conquers approach to manage the complex and big projects.
  2. Testing becomes easy with functions. It is easier to debug and solve the issue with functions rather than dealing with thousands of line of code at a time.
  3. Writing code in functions makes it easy to understand, easy to manage and easy to organize.
  4. Once defined, a function can be reused several times in your PHP code. This saves the time of re-writing the same code again and again.
  5. Functions are easy to maintain and update. You can change the functionality by changing the few lines of code of a function rather than changing the whole code at a time.

Types of functions in PHP

There are two types of function in PHP

  • Built-in functions
  • User defined functions

Built-in Functions

PHP facilitate the developer with a huge collection of built-in library functions. To use these built-in functions, simply provide the parameters (if needed) and call these functions.

Let’s learn about some PHP built-in functions.

String Functions

PHP provides some really useful functions to manipulate strings.

  • strtoupper($string) – This function takes a string as a parameter and returns the string with characters in upper case.
  • strtolower($string) – This function will return the lowercase version of the string being entered as an input parameter
  • ucfirst($string) – This function takes a string as an inputand returns it back with a first character starting in capital.
  • lcfirst($string) – This function takes a string as an input and returns the same string with the first character in lower case.
  • ucwords($string) – This function takes a string as an input and returns it back with the first character of each word in that string in upper case.
  • strlen($string) – This function will return the total length of a string.

Mathematical functions

These functions are used to format numbers or perform mathematical computations on input.

  • rand() – This function will return a random number
  • round(float number) – This function will round off a number with decimal points to the nearest whole number. For example round(3.49) will return 3.
  • sqrt(number) – This function will return the square root of a number
  • cos(number) – This function will return the cosine. For example cos(45) will return 0.52532198881773. Similar PHP functions are sin(num), tan(num), and pi()

User defined functions

Sometimes we need to write our own functions for the readability or usability purposes. If you have a task that needs to be performed a few numbers of times, writing a function will save you writing that piece of code every time. You can write it once and call it whenever you need it.

The syntax

A function may be defined using the following syntax.

<?php
Function function_name($arg1 , $arg2, ….., $argN){
// some functionality
return $result;
}
?>

Any valid HTML or PHP code may appear inside a function. For example, you can create functions to display error messages.

Example I of a Function in PHP

<?php
function print_error_msg(){
echo "Error! Sorry, you entered wrong user name or password. ";
}
print_error_msg();
?>

While creating a function, you need to keep certain rules about creating function names. These follow the same rules as other labels in PHP.

  1. A valid function name in PHP start with a letter, or underscore (_)
  2. A function definition always starts with a keyword function.
  3. To call a function you need to write its name followed by parenthesis
  4. A function name is not case-sensitive
  5. You have to define functions before using these (This is obvious, right?)
Tip: Always choose a meaningful and concise label for function names.

The information you pass while defining the function is called. These hold the executable values during runtime. You can define a list of parameters as long as you want, separated by a comma. While passing values during a function call, they are called arguments , or a parameter. An argument/parameter is a value of any datatype taht is passed to a function and a parameter is used to hold these arguments. In general, you can use these terms interchangeably.

Example II of a Function in PHP

The following example takes two numbers as input and return its sum. You can store the value being returned in a variable or can echo right away, as done in the example.

<?php
function add($num1, $num2){
$sum = $num1 + $num2;
return $sum;
}
$x = 4;
$y = 2;
echo "Adding ".$x. " and ". $y. " will result as ". add($x, $y);
?>

 

PHP File Handling Tutorial Home PHP Cookies
Tutorials Panel

About Tutorials Panel

programming tutorial and source code for Software Engineers and developers. https://twitter.com/TutorialsPanel

View all posts by Tutorials Panel →