Php, mysq lpart3

1,370 views
1,278 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,370
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Php, mysq lpart3

  1. 1. Php MySql part -3 Subhasis Nayak CMC
  2. 2. Contents Using functions Defining functions Arguments & parameters Default argument values Variable scope Using library files Including library files
  3. 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. 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. 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); }
  6. 6. An example.
  7. 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.
  8. 8. An example
  9. 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. 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. 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. 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
  13. 13. An example Give output 250 Give an error
  14. 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. 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. 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. 17. An example  I write a function ―add_tax‖ in ―addTax.php‖
  18. 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.

×