PHP Function and Array
In PHP server side scripting language Functions and array concepts are very important to solve the many complex problems in real world.
In most of the programming language, a block of segment in a program that perform specific operations like (Insert, Execute, Delete, Calculate, etc.). This are known as Functions. A Function is a type of sub routine or procedure in a program. A Function will be executed by a call to the Function and the Function returns any data type values or NULL value to called Function in the part of respective program
Functions can be divided in to three types:
• User defined Function,
• Pre-defined or System or built-in Function, and
• Parameterized Function.
1. User Defined Function
User Defined Function (UDF) in PHP gives a privilege to user to write their own specific operation inside of existing program module. Two important steps the Programmer has to create for users define Functions are:
A user-defined Function declaration begins with the keyword “function”. User can write any custom logic inside the function block.
SYNTAX:
function functionName()
{
Custom Logic code to be executed;
}
Once a function is defined it is execulied by a function call. The programmer has to give a functions Call inside the respective program.
SYNTAX:
functionName();
Example:
<?php
function insertMsg() {
echo “Student Details Inserted Successfully!”;
}
insertMsg(); // call the function
?>
2. Pre- defined or system or built in function
Pre- defined or system or built in function PHP has a wide collection of built- in functions that can be called directly from with in a script, to perform a specific task. These built in function are what make PHP a very efficient and productive. Scripting language no installation is required to use these functions. The PHP built in function may be classified into various caligories like;
PHP string function - Functions that help us to manipulate strings
PHP array function - Functions that allow you to access and manipulate arrays.
PHP math function - Functions that help to perform mathematical operations
PHP MySQLi function - Functions that allows you to access MySQL database servers.
PHP file system function - Functions that allow you to access and manipulate the file system
Values can be passed from one function to another
function thru parameters.
The parameter is also called as arguments, it is
like variables.
The arguments are mentioned after the function name
and inside of the parenthesis.
There is no limit for sending arguments, just
separate them with a comma notation.
Example
1:
The following example has a function with one
Argument ($sfname):
<?php
function School_Name($sname) {
echo $sname.”in
Tamilnadu.<br>”;
}
School_Name (“Government Higher
Secondary School Madurai”);
School_Name (“Government Higher
Secondary School Trichy”);
School_Name (“Government Higher
Secondary School Chennai”);
School_Name (“Government Higher
Secondary School Kanchipuram”);
School_Name (“Government Higher
Secondary School Tirunelveli”);
?>
Example
2:
The following example has a function with two
arguments ($sname and $Strength):
<?php
function
School_Name($sname,$Strength) {
echo $sname.”in Tamilnadu and Student
Strength is”.$Strength;
}
School_Name (“Government Higher
Secondary School Madurai”,200);
School_Name (“Government Higher
Secondary School Trichy”,300);
School_Name (“Government Higher
Secondary School Chennai”,250);
School_Name (“Government Higher
Secondary School Kanchipuram”,100);
School_Name (“Government Higher
Secondary School Tirunelveli”,200);
?>
Example
3:
For a function to return a value, use the return
statement
<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}
echo “5 + 10 = “ . sum(5, 10) .
“<br>”;
echo “7 + 13 = “ . sum(7, 13) .
“<br>”;
echo “2 + 4 = “ . sum(2, 4);
?>
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.