PHP III (Module)
PHP III (Module)
PHP function is a piece of code that can be reused many times. It can take input as argument list and return
value. There are thousands of built-in functions in PHP.
In PHP, we can define Conditional function, Function within Function and Recursive function also.
Less Code: It saves a lot of code because you don't need to write the logic many times. By the use of
function, you can write the logic only once and reuse it.
ADVERTISEMENT
Easy to understand: PHP functions separate the programming logic. So it is easier to understand the flow
of the application because every logic is divided in the form of functions.
Syntax
1. function functionname(){
2. //code to be executed
3. }
Note: Function name must be start with letter and underscore only like other labels in PHP. It can't be start with
numbers or special symbols.
File: function1.php
1. <?php
2. function sayHello(){
3. echo "Hello PHP Function";
4. }
5. sayHello();//calling function
6. ?>
Output:
PHP supports Call by Value (default), Call by Reference, Default argument values and Variable-length
argument list.
File: functionarg.php
1. <?php
2. function sayHello($name){
3. echo "Hello $name<br/>";
4. }
5. sayHello("Sonoo");
6. sayHello("Vimal");
7. sayHello("John");
8. ?>
Output:
Hello Sonoo
Hello Vimal
Hello John
File: functionarg2.php
1. <?php
2. function sayHello($name,$age){
3. echo "Hello $name, you are $age years old<br/>";
4. }
5. sayHello("Sonoo",27);
6. sayHello("Vimal",29);
7. sayHello("John",23);
8. ?>
Output:
They are specified inside the parentheses, after the function name.
The output depends upon the dynamic values passed as the parameters into the function.
PHP Parameterized Example 1
Addition and Subtraction
ADVERTISEMENT
In this example, we have passed two parameters $x and $y inside two functions add() and sub().
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Parameter Addition and Subtraction Example</title>
5. </head>
6. <body>
7. <?php
8. //Adding two numbers
9. function add($x, $y) {
10. $sum = $x + $y;
11. echo "Sum of two numbers is = $sum <br><br>";
12. }
13. add(467, 943);
14.
15. //Subtracting two numbers
16. function sub($x, $y) {
17. $diff = $x - $y;
18. echo "Difference between two numbers is = $diff";
19. }
20. sub(943, 467);
21. ?>
22. </body>
23. </html>
Output:
PHP Variable Scope
The scope of a variable is defined as its range in the program under which it can be accessed. In other
words, "The scope of a variable is the portion of the program within which it is defined and can be
accessed."
1. Local variable
2. Global variable
3. Static variable
Local variable
The variables that are declared within a function are called local variables for that function. These local
variables have their scope only in that particular function in which they are declared. This means that these
variables cannot be accessed outside the function, as they have local scope.
A variable declaration outside the function with the same name is completely different from the variable
declared inside the function. Let's understand the local variables with the help of an example:
File: local_variable1.php
1. <?php
2. function local_var()
3. {
4. $num = 45; //local variable
5. echo "Local variable declared inside the function is: ". $num;
6. }
7. local_var();
8. ?>
Output:
1. <?php
2. function mytest()
3. {
4. $lang = "PHP";
5. echo "Web development language: " .$lang;
6. }
7. mytest();
8. //using $lang (local variable) outside the function will generate an error
9. echo $lang;
10. ?>
Output:
Web development language: PHP
Notice: Undefined variable: lang in D:\xampp\htdocs\program\p3.php on line
28
Global variable
The global variables are the variables that are declared outside the function. These variables can be
accessed anywhere in the program. To access the global variable within a function, use the GLOBAL
keyword before the variable. However, these variables can be directly accessed or used outside the function
without any keyword. Therefore there is no need to use any keyword to access a global variable outside the
function.
Example:
File: global_variable1.php
1. <?php
2. $name = "Sanaya Sharma"; //Global Variable
3. function global_var()
4. {
5. global $name;
6. echo "Variable inside the function: ". $name;
7. echo "</br>";
8. }
9. global_var();
10. echo "Variable outside the function: ". $name;
11. ?>
Output:
Note: Without using the global keyword, if you try to access a global variable inside the function, it will generate
an error that the variable is undefined.
Example:
File: global_variable2.php
1. <?php
2. $name = "Sanaya Sharma"; //global variable
3. function global_var()
4. {
5. echo "Variable inside the function: ". $name;
6. echo "</br>";
7. }
8. global_var();
9. ?>
Output:
Another way to use the global variable inside the function is predefined $GLOBALS array.
Example:
File: global_variable3.php
1. <?php
2. $num1 = 5; //global variable
3. $num2 = 13; //global variable
4. function global_var()
5. {
6. $sum = $GLOBALS['num1'] + $GLOBALS['num2'];
7. echo "Sum of global variables is: " .$sum;
8. }
9. global_var();
10. ?>
Output:
If two variables, local and global, have the same name, then the local variable has higher priority than the
global variable inside the function.
Example:
File: global_variable2.php
1. <?php
2. $x = 5;
3. function mytest()
4. {
5. $x = 7;
6. echo "value of x: " .$x;
7. }
8. mytest();
9. ?>
Output:
Value of x: 7
Note: local variable has higher priority than the global variable.
Static variable
ADVERTISEMENT
ADVERTISEMENT
It is a feature of PHP to delete the variable, once it completes its execution and memory is freed.
Sometimes we need to store a variable even after completion of function execution. Therefore, another
important feature of variable scoping is static variable. We use the static keyword before the variable to
define a variable, and this variable is called as static variable.
Static variables exist only in a local function, but it does not free its memory after the program execution
leaves the scope. Understand it with the help of an example:
Example:
File: static_variable.php
1. <?php
2. function static_var()
3. {
4. static $num1 = 3; //static variable
5. $num2 = 6; //Non-static variable
6. //increment in non-static variable
7. $num1++;
8. //increment in static variable
9. $num2++;
10. echo "Static: " .$num1 ."</br>";
11. echo "Non-static: " .$num2 ."</br>";
12. }
13.
14. //first function call
15. static_var();
16.
17. //second function call
18. static_var();
19. ?>
Output:
Static: 4
Non-static: 7
Static: 5
Non-static: 7
You have to notice that $num1 regularly increments after each function call, whereas $num2 does not. This
is why because $num1 is not a static variable, so it freed its memory after the execution of each function
call.
................................................................................................
PHP Form Handling
We can create and use forms in PHP. To get form data, we need to use PHP superglobals $_GET and
$_POST.
The form request may be get or post. To retrieve data from get request, we need to use $_GET, for post
request $_POST.
Let's see a simple example to receive data from get request in PHP.
File: form1.html
1. <form action="welcome.php" method="get">
2. Name: <input type="text" name="name"/>
3. <input type="submit" value="visit"/>
4. </form>
File: welcome.php
1. <?php
2. $name=$_GET["name"];//receiving name field value in $name variable
3. echo "Welcome, $name";
4. ?>
The data passed through post request is not visible on the URL browser so it is secured. You can send large
amount of data through post request.
Let's see a simple example to receive data from post request in PHP.
File: form1.html
1. <form action="login.php" method="post">
2. <table>
3. <tr><td>Name:</td><td> <input type="text" name="name"/></td></tr>
4. <tr><td>Password:</td><td> <input type="password" name="password"/></td></tr>
5. <tr><td colspan="2"><input type="submit" value="login"/> </td></tr>
6. </table>
7. </form>
File: login.php
1. <?php
2. $name=$_POST["name"];//receiving name field value in $name variable
3. $password=$_POST["password"];//receiving password field value in $password variable
4.
5. echo "Welcome: $name, your password is: $password";
6. ?>
Output:
We can show all errors in PHP using the error_reporting() function. It sets
the error_reporting directive at runtime according to the level provided. If no level is provided, it will
return the current error reporting level. error_reporting(E_ALL) level represents all errors,
warnings, notices, etc.
PHP Code: If the level is set to zero, no error is reported even when an unavailable file is included.
PHP
<?php
error_reporting(0);
include(gfg.php);
?>
Output:
No output
No error is reported
PHP Code: When the level is set to E_ALL, all the errors including all warnings and notices are
reported.
..........................................................
<?php
// Turn off error reporting
error_reporting(0);
// Same as error_reporting(E_ALL);
ini_set("error_reporting", E_ALL);
PHP has many levels of errors, and using this function sets that level for the current script.
Syntax
error_reporting(level);