PHP
PHP File() Function: File_exists, Fopen, Fwrite, Fclose, Fgets, copy, unlink
What is a File? A file is simply a resource for storing information on a computer. Files are...
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-
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.
These are functions that manipulate string data, refer to the article on strings for implementation examples of string 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 |
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;
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
What is a File? A file is simply a resource for storing information on a computer. Files are...
What is a control structure? Code execution can be grouped into categories as shown below...
Download PDF 1) What is PHP? PHP is a web language based on scripts that allow developers to...
Potential security threats They are basically two groups of people that can attack your system...
Training Summary PHP is the most popular scripting language on the web. Without PHP Facebook,...
A Loop is an Iterative Control Structure that involves executing the same number of code a number...