In average PHP has more than 1000 built-in function; function are vital thing that gets PHP the real power. Beside 1000 + functions we also can create our own functions using logics. Functions are the block of statements that can be used multiple times in a program. they do not execute until they are called. A function name can be declared with a letter or underscore but not a number.
Uses of PHP functions
They are used to make a program run conveniently and error free.Function has different scope in PHP programming. All logic 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>Nirajan Ghimirey's Workshop</title>
</head>
<body>
<?php
//Example of Function
function message(){
echo "Welcome to Nirajan Ghimirey's Workshopl<br/>";
}
//Example of function with arguments
function courses($name,$desc){
echo $name."=". $desc."<br/>";
}
//Example of default function
functiondefalultMessage($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>
No comments:
Post a Comment