Php, mysq lpart3
Click here to load reader
-
Upload
subhasis-nayak -
Category
Technology
-
view
1.110 -
download
2
Transcript of Php, mysq lpart3
![Page 1: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/1.jpg)
Subhasis Nayak
CMC
Php MySql part -3
![Page 2: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/2.jpg)
Contents
Using functions
Defining functions
Arguments & parameters
Default argument values
Variable scope
Using library files
Including library files
![Page 3: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/3.jpg)
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.
![Page 4: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/4.jpg)
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.
![Page 5: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/5.jpg)
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);
}
![Page 6: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/6.jpg)
An example.
![Page 7: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/7.jpg)
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 calledarguments.
Well, arguments are used to pass the outside variables as localvariables of local variables of function.
If we want to use GLOBAL variable use GLOBAL keywordbefore it.
![Page 8: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/8.jpg)
An example
![Page 9: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/9.jpg)
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.
![Page 10: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/10.jpg)
Cont’d …..
function myFunc($a=0){
Return a;
}
echo myFunc();
As I am not passing
any argument it will
take default one.
function myFunc($a){
Return a;
}
echo myFunc();
As I am not passing
any argument it will
give an error.
![Page 11: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/11.jpg)
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.
![Page 12: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/12.jpg)
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
![Page 13: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/13.jpg)
An example
Give output 250
Give an error
![Page 14: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/14.jpg)
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.
![Page 15: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/15.jpg)
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.
![Page 16: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/16.jpg)
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.
![Page 17: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/17.jpg)
An example
I write a function ―add_tax‖ in ―addTax.php‖
![Page 18: Php, mysq lpart3](https://reader038.fdocuments.in/reader038/viewer/2022100602/55862137d8b42a7d428b514e/html5/thumbnails/18.jpg)
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
yourblock. I used in this way.