How To Use function In PHP
How to use PHP functions
Syntax: function functionname([arguments]) { }
Functions can be placed anywhere in a page and will be available even if
called above the actual function being created. The exception to this rule is if
the function is only defined as part of a conditional statement, and is not
available to be called until that conditional statement has been evaluated.
Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
Example: declare of function without arguments
<?php
function greetMsg() {
echo "Hello PHP!";
}
greetMsg(); // call the function?>
<?phpfunction foo()
{
function bar()
{
echo "I don't exist until foo() is called.\n";
}
}/* We can't call bar() yet
since it doesn't exist. */foo();/* Now we can call bar(),
foo()'s processing has
made it accessible. */bar();?>
Note: All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.
PHP function with arguments:
<?php
function mySkill($skill) {
echo "I have $skill \t skill<br>";
}
mySkill("PHP");
mySkill("HTML");
mySkill("CSS");
?>
PHP function with default arguments
<?php
function myAge($year = 30) {
echo "I am $year \t years old <br>";
}
myAge(29);
myAge(); // will use the default value of 30myAge(32);
?>
PHP function with returning values
If you want to return a value from function, use return statement
<?php
function add($a, $b) {
$c = $a + $b;
return $c;
}
echo "5 + 10 = " . add(5, 10) . "<br>";
echo "7 + 13 = " . add(7, 13) . "<br>";
echo "2 + 4 = " . add(2, 4);
?>
PHP function also return a variable
<?php
function add($a, $b) {
$c = $a + $b;
return $c;
}
$a = 15;
$b = 15;
$c = add($a, $b);
echo $c;
?>
Tips: If multiple pages will refer to the same functions,create a separate
functions.phpfile (name it whatever you like) and require()or
require_once()with pages that will need to use those functions. For speed
and file size, page specific functions should be included directly on the
necessary page.
Syntax: function functionname([arguments]) { }
Functions can be placed anywhere in a page and will be available even if
called above the actual function being created. The exception to this rule is if
the function is only defined as part of a conditional statement, and is not
available to be called until that conditional statement has been evaluated.
Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
Example: declare of function without arguments
<?php
function greetMsg() {
echo "Hello PHP!";
}
greetMsg(); // call the function?>
Note: Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.
Otherwise you will get: Fatal error: Call to undefined function greetMsg()
When a function is defined in a conditional manner such as the two examples shown. Its definition must be processed prior to being called.
Example: Conditional function
<?php
$makefoo = true;/* We can't call foo() from here
since it doesn't exist yet,
but we can call bar() */bar();
if ($makefoo) {
function foo()
{
echo "I don't exist until program execution reaches me.\n";
}
}/* Now we can safely call foo()
since $makefoo evaluated to true */if ($makefoo) foo();
function bar()
{
echo "I exist immediately upon program start.\n";
}?>
Example: Function inside function$makefoo = true;/* We can't call foo() from here
since it doesn't exist yet,
but we can call bar() */bar();
if ($makefoo) {
function foo()
{
echo "I don't exist until program execution reaches me.\n";
}
}/* Now we can safely call foo()
since $makefoo evaluated to true */if ($makefoo) foo();
function bar()
{
echo "I exist immediately upon program start.\n";
}?>
<?phpfunction foo()
{
function bar()
{
echo "I don't exist until foo() is called.\n";
}
}/* We can't call bar() yet
since it doesn't exist. */foo();/* Now we can call bar(),
foo()'s processing has
made it accessible. */bar();?>
Note: All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.
PHP function with arguments:
<?php
function mySkill($skill) {
echo "I have $skill \t skill<br>";
}
mySkill("PHP");
mySkill("HTML");
mySkill("CSS");
?>
PHP function with default arguments
<?php
function myAge($year = 30) {
echo "I am $year \t years old <br>";
}
myAge(29);
myAge(); // will use the default value of 30myAge(32);
?>
PHP function with returning values
If you want to return a value from function, use return statement
<?php
function add($a, $b) {
$c = $a + $b;
return $c;
}
echo "5 + 10 = " . add(5, 10) . "<br>";
echo "7 + 13 = " . add(7, 13) . "<br>";
echo "2 + 4 = " . add(2, 4);
?>
PHP function also return a variable
<?php
function add($a, $b) {
$c = $a + $b;
return $c;
}
$a = 15;
$b = 15;
$c = add($a, $b);
echo $c;
?>
Tips: If multiple pages will refer to the same functions,create a separate
functions.phpfile (name it whatever you like) and require()or
require_once()with pages that will need to use those functions. For speed
and file size, page specific functions should be included directly on the
necessary page.
Comments
Post a Comment