Functions in PHP
|
Just like we have learned in mathematics, the basic way to think of and understand functions is to think of it as a machine, you feed it with raw materials and the machine goes through processing these materials into finished and possibly usable product. An easy way to look at functions is to consider the way we use baking ovens in households, to make bread you would mix all the ingredients and stick the uncooked dow in the oven. After a while you pop the oven and there is you bread, well baked and tasty.
Functions in PHP
In PHP and other programming languages that use functions, a function is considered to be a self-contained or autonomous block of code that can be called by other programs or scripts to perform a certain task. When called the function executes the code enclosed in its parenthesis and may or may not return a value to the calling code.
Built-in functions
Built-in functions are part of the development language. These are aimed at easing the development process by shielding the developer from complex programming aimed at producing certain results. An example of this are the print(), echo (), addSlashes () and many others, these functions themselves have complex programming involved. Instead of letting every developer suffer through the pain of developing their own code to perform these functions. The language infrastructure allow developers to simply call the function name and pass it the values.
User defined functions
User defined functions are those created by the developer. The whole purpose of using functions in programming is the ability to reuse the code. Without using functions, the developer will have to duplicate code whenever needed. If there is a piece of code that is frequently used, you may consider putting it in a function, that way all you have to do when you need the same functionality is simply call the function. The general format for defining a function is as follows:
function function_name($argument1, argument2,…){
//put the function code here
}
Practical example of how to use functions
first we are going to define a function and the I will show you how that function can be called to perform a certain task:
function definition:
function add_numbers($number1,$number2){
$sum = $number1 + $number2;
return $sum;
}
To call this function you could do the following:
echo add_numbers(3,5);
This will call the function add_numbers, that function adds the two numbers together and return the sum. This statement here then prints the returned value.
|
RELATED ARTICLES: Functions Notation
Array related PHP Functions The PHP language has a lot of functions that are used to populate, retrieve and manipulate array variables. Bellow are some of those functions, we will be using some of them as we continue to learn. Functi
MySQL String functions MySQL relational Database system has a lot of built-in functions, some of which are used to retrieve and manipulate data, while others are simply used for server administrative purposes. In this posting we will explore how String functions are used in MySQL.
String length function
PHP Flow control functions
PHP String functions So far we have only displayed strings directly to the browser without changing the format. We have successfully done that using the PHP echo () command. In addition to the echo () command, PHP also has two other print functions that allow users to format the output. The formatting may come in the fo
Trigonometry functions
Trigonometry is a branch of mathematics that deals with triangles. It is more like geometry, but it deals particularly with right angle triangles. The concept of trigonometry deals with the relationship between angles and sides of the triangle. To accurately descri
PHP Flow control functions
Most scripts we have looked at so far are linear and flow only in one direction. For example we have looked at scripts that would take two numbers add them together and display the answer. In real world things hardly work that way. In most cases you are faced with different possibilities and
php functions
Oracle Procedures, Functions and looping constructs
Procedures and functions in PL/SQL are types of blocks that are stored in the database in a compiled form. This is among the major differences between ordinary queries and procedures and functions (otherwise known as subprograms). Creating a function or procedure requires the u
User sessions in PHP Sessions in PHP are used to store and acquire data for use during the session. Say for example, you have a value that you use throughout your visit to a session enable web site. Instead of having to pass around this value from one page to another and risk the chance of loosing the value, you could s
| |
|