This document discusses PHP functions and how to define, call, and use them. It covers defining functions, arguments and parameters, default argument values, variable scope, and including library files. Functions allow reusable blocks of code and avoid duplicating code. Arguments are values passed into a function, while parameters are variable names used inside the function. Default arguments can be set so functions still work if not all arguments are passed. Variable scope determines where variables can be accessed. Library files allow storing functions in one place and including them elsewhere.
3. Using functions
Functions are used to do certain actions. It consists block of code.
When we call a function it runs the code and give a result,
It may takes outside values.
It may return values to outside.
When we call a function it jumps to the definition of that function
and start processing codes which are inside of the ―{}‖ and once
completes it jumps back to the next line from where it was called.
PHP contains many wide range built in functions which can be
used to do solve many tasks.
4. User defined function
We will put some reusable code into function and use it again
and again. Here we can change the function if we want to
change how does it work or processing the block of code.
We can maintain our code easily and quickly.
Reduce the duplication of code.
Reduce the effort and time.
Changing the function definition effect all over where it is
called.
5. Defining function
In php to define function we need the key word ―function‖
and then name of the function.
Here we do not need any return type. What ever we
returned by default it will get it’s data type.
But when we want to return from the function we have to
use keyword ‖return‖.
function add(int x, int y){
return (x+y);
}
7. Arguments & parameters
When we define the function we used variables with in the
first bracket.
On that time those variables are known as parameter lists.
When we call function using variables those are called
arguments.
Well, arguments are used to pass the outside variables as local
variables of local variables of function.
If we want to use GLOBAL variable use GLOBAL keyword
before it.
9. Default arguments
Let’s we use 2 – parameters. If we do not pass two arguments
we will get an error.
In complex logic sometimes we do not need all the
arguments. On this case what will we do.
Thanks to PHP it provides us a feature through which we can
make our arguments default.
To write a default argument, we have to initialize the
parameters when we use them on first bracket. Let’s see.
10. Cont’d …..
function myFunc($a=0){
As I am not passing
Return a; any argument it will
} take default one.
echo myFunc();
function myFunc($a){
As I am not passing
Return a; any argument it will
} give an error.
echo myFunc();
11. Variable scope
The reason values have to be passed in to functions as
arguments has to do with variable scope—the rules that
determine what sections of script are able to access which
variables.
The basic rule is that any variables are:
Variable defined in the main body of the script cannot be used
inside a function.
any variables used inside a function cannot be seen by the main
script.
12. Cont’d ……
Local – variables within a function are said to be local
variables or that their scope is local to that function.
Global - Variables that are not local are called global
variables.
Local and global variables can have the same name and
contain different values.
To access global variable inside the function use keyword
‖global‖ as prefix of the variable
14. Using library files
If we want to use it again in other scripts. Rather than copy
the function definition into each script that needs to use it,
you can use a library file so that your function needs to be
stored and maintained in only one place.
A library file needs to enclose its PHP code inside <?php tags
just like a regular script; otherwise, the contents will be
displayed as HTML when they are included in a script.
To do so create a ―.php‖ file put your all functions.
15. Including library files
To incorporate an external library file into another script,
you use the include keyword.
The include path Setting By default, include searches only the
current directory and a few system locations for files to be
included.
If you want to include files from another location, you can
use a path to the file.
You can use the include_once keyword if you want to
make sure that a library file is loaded only once.
16. Cont’d …..
If a script attempts to define the same function a second
time, an error will result.
Using include_once helps to avoid this, particularly when
files are being included from other library files.let’ds do it
practically.
17. An example
I write a function ―add_tax‖ in ―addTax.php‖
18. Next write a file “useTax.php”
Use include_once/include to add like below:
include_once "addTax.php";
Then call the function “add_tax()” any where in
your block. I used in this way.