Functions in PHP
Subject: Web development using PHP and MySQL
In programming, when we are discussing functions we are either referring to the built-in function that comes with the language or the User defined or custom function that the programer would like to create..
Built-in functionsPHP has lots of built-in functions some of which we have seen, for example the echo or print, date, array, mail, math, msqli, strings etc.
Date function
<?php
print date('h:i:s - d/m/Y');
?>
display the current time and date
pi() math function
<?php
print pi();
?>
Custom or user-defined functionCustom or user-defined function as the name suggest, are function you create to solve a particular problem or execute a task that occurs severally, this save you from the time or effort required to rewrite such code and also makes your program easy to understand and maintain.
So you create such function once and call it whenever you need.
Example 1
<?php
//create a function
function WelcomeMsg(){
echo "Welcome to my site";
}
//call the function
WelcomeMsg();
?>
Example 2.
Function with arguments, takes in values and return a result.
<?php
//create a funtion with arguments
function AddNum($x, $y){
return $x+$y;
}
//call the function and pass values to it.
echo AddNum(200,700);
?>
Output
900
By:
Benjamin Onuorah
Comments
Abah Johnson (learner)
Date commented: 16-May-2024 04:47:02am
Login to comment or ask question on this topic
Previous Topic Next Topic