PHP Functions

What is PHP Functions?

PHP has more than 1000 built-in function; function can also be called as a vital thing that gets PHP the real power. Beside the given more than 1000 functions we also can create our own functions using logics. Block of statements that can be used multiple times in a program. Function does not execute until it is called. A function name can be declared with a letter or underscore but not a number.

Uses of PHP functions

  1. Function is used to make a program run convenient and error freely.
  2. Function has different scope in PHP programming, logics can be implemented using function, complex outputs are simply programmed with in a function etc.
Syntax:
functionfunctionName() {
code to be executed;
}

 

Example:
PHP Script (function.php)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>Test Tutorials</title>
</head>
<body>
<?php
//Example of Function
function message(){
    echo "Welcome to Tutorial<br/>";
}
//Example of function with arguments
function courses($name,$desc){
    echo $name."=". $desc."<br/>";
}
//Example of default function
function defalultMessage($view=100){
    echo "Viewer = ".$view."<br/>";
}
//Example of function with return value
function returnValue($value1,$value2){
    $value3=$value1+$value2;
    return $value3;
}
//function call
message();
courses("PHP","Hypertext Preprocessor");
defalultMessage();
echo "Users = ". returnValue(10,5);
?>
</body>
</html>