PHP Function: Numeric, Built in, String, Date, User Defined

What is a Function?

A function is a reusable piece or block of code that performs a specific action.

Functions can either return values when called or can simply perform an operation without returning any value.

PHP has over 700 functions built in that perform different tasks.

In this tutorial, you will learn-

Why use Functions?

Built in Functions

Built in functions are functions that exist in PHP installation package.

These built in functions are what make PHP a very efficient and productive scripting language.

The built in functions can be classified into many categories. Below is the list of the categories.

String Functions

These are functions that manipulate string data, refer to the article on strings for implementation examples of string functions

Numeric Functions

Numeric functions are function that return numeric results.

Numeric php function can be used to format numbers, return constants, perform mathematical computations etc.

The table below shows the common PHP numeric functions

Function Description Example Output
is_number Accepts an argument and returns true if its numeric and false if it’s not
<?php
if(is_numeric("guru"))
{
   echo "true";
}
else
{
   echo "false";
}
?>
   
false
<?php
if(is_numeric (123))
{
  echo "true";
}
else
{
  echo "false";
}
?>
true
number_format Used to formats a numeric value using digit separators and decimal points
<?php
echo number_format(2509663);
?>
2,509,663
rand Used to generate a random number.
<?php
echo rand();
?>
Random number
round Round off a number with decimal points to the nearest whole number.
<?php
echo round(3.49);
?>
3
sqrt Returns the square root of a number
<?php
echo sqrt(100);
?>
10
cos Returns the cosine
<?php
echo cos(45);
?>
0.52532198881773
sin Returns the sine
<?php
echo sin(45);
?>
0.85090352453412
tan Returns the tangent
<?php
echo tan(45);
?>
1.6197751905439
pi Constant that returns the value of PI
<?php
echo pi();
?>
3.1415926535898

 

Date Function

The date function is used to format Unix date and time to human readable format.

Check the article on PHP date functions for more details. Other functions

These include;

   

Why use User Defined Functions?

User defined functions come in handy when;

These activities will be spread across a number of pages.

Creating a function that all these pages can be calling is one of the features that make PHP a powerful scripting language.

Before we create our first user defined function, let’s look at the rules that we must follow when creating our own functions.

Let’s now create our first function. We will create a very basic function that illustrates the major components of a function in PHP.

<?php

//define a function that displays hello function

function add_numbers(){   
	echo 1 + 2;
}
add_numbers ();
?>

Output:

3

  HERE,

Let’s now look at a fairly complex example that accepts a parameter and display a message just like the above function.

Suppose we want to write a function that prints the user name on the screen, we can write a custom function that accepts the user name and displays it on the screen.

The code below shows the implementation.

<?php
function display_name($name)
{
echo "Hello " . $name;
}
display_name("Martin Luther King");
?>

Output:

Hello Martin Luther King

  HERE,

Let’s now look at a function that accepts a parameter and then returns a value. We will create a function that converts kilometers to miles. The kilometers will be passed as a parameter. The function will return the miles equivalent to the passed kilometers. The code below shows the implementation.

<?php
function kilometers_to_miles($kilometers = 0)
{
$miles_scale = 0.62;
return $kilometers * $miles_scale;
}
echo kilometers_to_miles(100);
?>

Output:

62

Summary

 

YOU MIGHT LIKE: