PHP HOME
PHP Introduction
PHP SETUP
PHP Syntax
PHP Data Types
PHP Outputs
PHP Variable
PHP Constant
PHP Strings
PHP Operators
PHP if else if
PHP Switch
PHP while loop
PHP for loop
PHP Arrays
PHP Functions
PHP Sorting Array
PHP Form Handling
PHP Form Validation
PHP Form Required
PHP Form URL/EMAIL
PHP Form Submit
PHP Contact Form
PHP DATE & TIME
PHP INCLUDE
PHP FILE HANDLING
PHP FILE OPEN/READ
PHP FILE CREATE/WRITE
PHP FILE UPLOAD
PHP SESSION
PHP COOKIES
PHP FILTER
PHP ERROR HANDLING
PHP EXCEPTION
PHP Redirects
PHP with MySQL
PHP WITH MYSQL DB
MYSQL CONNECT
MYSQL CREATE DATABASE
SELECT DATABASE
MYSQL CREATE TABLE
MYSQL INSERT DATA
MYSQL SELECT DATA
MYSQL UPDATE DATA
MYSQL DELETE DATA
MYSQL ORDER
MYSQL LIMIT
MYSQL LAST ID
PHP function is a set of instructions which perform some specific task just like another programming languages.
PHP function does not run when the page load.
PHP function receive one or more parameters and return some value i.e variable, array, object etc.
In PHP there are thousands pre-built functions like strpos(), fopen(), fread() etc.
PHP functions are divided into two parts
function definition
function call
function nameOfFunction() {
set of instructions;
...
...
}
<!DOCTYPE html>
<html>
<body>
<?php
// function definition
function sayHello() {
echo "Hello John !";
}
?>
<?php
// function call
sayHello();
?>
</body>
</html>
The above code produces the following results
Outputs: Hello John!
<!DOCTYPE html>
<html>
<body>
<?php
// function definition
function sayHello($name) {
echo "Hello ".$name;
}
?>
<?php
// function call
sayHello('Sam !');
?>
</body>
</html>
The above code produces the following results
Outputs: Hello Sam!
<!DOCTYPE html>
<html>
<body>
<?php
// function definition
function sayHello($name = 'John !') {
echo "Hello, ".$name."<br />";
}
?>
<?php
// function call
sayHello();
sayHello('Sam!');
sayHello('Rose!');
?>
</body>
</html>
The above code produce the following results
Outputs: Hello, John!
Hello, Sam!
Hello, Rose!
<!DOCTYPE html>
<html>
<body>
<?php
// function definition
function sayHello($name = 'John !') {
$abc = "Hello, ".$name."<br />";
return $abc;
}
?>
<?php
// function call
echo sayHello();
echo sayHello('Sam!');
echo sayHello('Rose!');
?>
</body>
</html>The above code produces the following results
Outputs: Hello John!
Hello, Sam!
Hello, Rose!