Functions in PHP - In the world of programming, in any language, the use of a function is absolute, therefore it is important to learn it. The function in the programming language is the program code designed to accomplish a specific task, and is part of the main program.
There are several main things to note in the function declaration in PHP.
function Function_name (parameter1,...,n) {
statement2;
}
To better understand it, here I give examples of program functions in php.
Program 1
File Name: function01.php
Description: A simple program defines and invokes functions without parameters and without return value.
Open notepad, then type the following php code.
<?php
function odd_print () {
for ($i=0; $i<100; $i++) {
if ($i%2 == 1) {
echo "$i ";
}
}
}
//Function call
odd_print();
?>
Save the php code in the htdocs folder named function01.php.
To see the result, open the browser type http://localhost/function01.php in the address bar then enter. Then the result will look like the following.
Program 2
File Name: function02.php
Description: Program function with parameters.
<?php
function odd_print ($early, $end) {
for ($i=$early; $i<$end; $i++) {
if ($i%2 == 1) {
echo "$i ";
}
}
}
//Function call
$a = 10;
$b = 50;
echo "<b>An odd number from $a to $b : </b><br>";
odd_print($a, $b);
?>
Program 3
File Name: function03.php
Description: Program function that returns a value.
<?php
function wide_circle ($radius) {
return 3.14 * $radius * $radius;
}
//Function call
$r = 10;
echo "Broad circle with radius $r = ";
echo wide_circle($r);
?>
Program 4
File Name: function04.php
Description: Program passing value in function.
<?php
function added_string ($str) {
$str = $str . ", Madison";
return $str;
}
//
$str = "University of Wisconsin";
echo "\$str = $str<br>";
echo added_string ($str). "<br>";
echo "\$str = $str<br>";
?>
Program 5
File Name: function05.php
Description: Program passing by reference in function.
<?php
function added_string (&$str) {
$str = $str . ", Madison";
return $str;
}
//
$str = "University of Wisconsin";
echo "\$str = $str<br>";
echo added_string ($str). "<br>";
echo "\$str = $str<br>";
?>
Program 6
File Name: function06.php
Description: The program displays the defined-function supported by PHP.
<?php
function wide_circle ($radius) {
return 3.14 * $radius * $radius;
}
$arr = get_defined_functions();
echo "<pre>";
print_r($arr);
echo "</pre>";
?>
Program 7
File Name: function07.php
Description: Program checks a function available in PHP or not.
<?php
if (function_exists('exif_read_data')) {
echo "Function exif_read_data() Is in PHP.<br />\n";
} else {
echo "Function exif_read_data() Not in PHP.<br />\n";
}
?>
That's the article that I can explain about Functions in PHP, hopefully can increase our knowledge of php programming language. Learn also articles about Array Functions Examples in PHP.
There are several main things to note in the function declaration in PHP.
- Function name
- Parameters
- Function body
- Defines its own function, if the function we want has not been provided by PHP
- Call function, both built-in function and user-defined function how to call it the same ie write the name of the function then followed by parentheses, for example we call the print function, then we write: print ()
function Function_name (parameter1,...,n) {
statement2;
}
To better understand it, here I give examples of program functions in php.
Program 1
File Name: function01.php
Description: A simple program defines and invokes functions without parameters and without return value.
Open notepad, then type the following php code.
<?php
function odd_print () {
for ($i=0; $i<100; $i++) {
if ($i%2 == 1) {
echo "$i ";
}
}
}
//Function call
odd_print();
?>
Save the php code in the htdocs folder named function01.php.
To see the result, open the browser type http://localhost/function01.php in the address bar then enter. Then the result will look like the following.
![]() |
Program view |
Program 2
File Name: function02.php
Description: Program function with parameters.
<?php
function odd_print ($early, $end) {
for ($i=$early; $i<$end; $i++) {
if ($i%2 == 1) {
echo "$i ";
}
}
}
//Function call
$a = 10;
$b = 50;
echo "<b>An odd number from $a to $b : </b><br>";
odd_print($a, $b);
?>
![]() |
Program view |
Program 3
File Name: function03.php
Description: Program function that returns a value.
<?php
function wide_circle ($radius) {
return 3.14 * $radius * $radius;
}
//Function call
$r = 10;
echo "Broad circle with radius $r = ";
echo wide_circle($r);
?>
![]() |
Program view |
Program 4
File Name: function04.php
Description: Program passing value in function.
<?php
function added_string ($str) {
$str = $str . ", Madison";
return $str;
}
//
$str = "University of Wisconsin";
echo "\$str = $str<br>";
echo added_string ($str). "<br>";
echo "\$str = $str<br>";
?>
![]() |
Program view |
Program 5
File Name: function05.php
Description: Program passing by reference in function.
<?php
function added_string (&$str) {
$str = $str . ", Madison";
return $str;
}
//
$str = "University of Wisconsin";
echo "\$str = $str<br>";
echo added_string ($str). "<br>";
echo "\$str = $str<br>";
?>
![]() |
Program view |
Program 6
File Name: function06.php
Description: The program displays the defined-function supported by PHP.
<?php
function wide_circle ($radius) {
return 3.14 * $radius * $radius;
}
$arr = get_defined_functions();
echo "<pre>";
print_r($arr);
echo "</pre>";
?>
![]() |
Program view |
Program 7
File Name: function07.php
Description: Program checks a function available in PHP or not.
<?php
if (function_exists('exif_read_data')) {
echo "Function exif_read_data() Is in PHP.<br />\n";
} else {
echo "Function exif_read_data() Not in PHP.<br />\n";
}
?>
![]() |
Program view |
That's the article that I can explain about Functions in PHP, hopefully can increase our knowledge of php programming language. Learn also articles about Array Functions Examples in PHP.
Comments
Post a Comment