0% found this document useful (0 votes)
36 views30 pages

Web Based Programming: BCA Semester: II L-11-14

Uploaded by

niftyelbakyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views30 pages

Web Based Programming: BCA Semester: II L-11-14

Uploaded by

niftyelbakyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

INSTITUTE OF INFORMATION TECHNOLOGY & MANAGEMENT

Accredited ‘A’ Grade by NAAC &Recognised U/s 2(f) of UGC act


Rated Category `A+’ by SFRC & `A’ by JAC Govt. of NCT of Delhi
Approved by AICTE & Affiliated to GGS Indraprastha University, New Delhi

Web Based Programming


Dr.Ramandeep Kaur
Associate Professor-CS
BCA
Semester: II
L-11-14
Contents:
Functions in PHP: Defining a Function , Calling A function, Parameters to function,
returning value, array and lists, Anonymous Functions, Variable Scope(Global
variable& Static Variables)

1 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Functions
 can be called directly, from within a script, to perform a specific
task.
 Functions are based on old programming dictum: DIVIDE and
CONQUER
 A function is a piece of code in a larger program.
 The function performs a specific task.
 The advantages of using functions are:
 Reducing duplication of code
 Decomposing complex problems into simpler pieces
 Improving clarity of the code
 Code Reusability-create once, use multiple times, within same or
different programs
 Information hiding

2 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Functions in PHP
• PHP offers two constructs to help us to reduce code
duplication in our program:
1.user defined functions: user defined functions are reusable
code segments that are created by programmers to perform
specific task
2.Classes: classes provides a more formal, object oriented
approach to reusability and encapsulation.
There are four basic types of
functions.
 user-defined functions
 Function arguments
 Returning values
 Variable functions
 Internal (built-in) functions
 Anonymous functions
User Defined Functions
 A function is a block of statements that can be used
repeatedly in a program.
 A function will not execute automatically when a page loads.
 A function will be executed by a call to the function.

5 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Creating Functions
 A user-defined function declaration starts with the keyword
function, followed by the name of the function
 Syntax
function function_name([argument_list…])
{
[statements]
[return return_value;]
}
function display()
Example: { echo "Hello world!";
}

6 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Calling the function
 To call the function, just write its name followed by parentheses ()
<?php
function display() { //creating a user-defined function
echo "Hello world!";
}
display(); //calling the user-defined function
?>

 The opening curly brace { indicates the beginning of the function


code, and the closing curly brace } indicates the end of the
function.
 The function outputs "Hello world!".
7 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Passing Arguments to Function

<?php
function familyName($fname, $year) //function with arguments
{
echo "$fname Chatterjee. Born in $year <br>";
}

familyName(“Chandrashekhar", "1975"); //calling the function


familyName(“Bankim Chandra", "1978");
familyName("Kim John", "1983");
?>

8 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Default Argument Value
<?php
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}

setHeight(350);
setHeight();
setHeight(135);
setHeight(80);
?>

9 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Functions with return values
 Example-1
function sum($x, $y)
{ $z = $x + $y;
return $z; }
echo "5 + 10 = " . sum(5, 10) . "<br>";
echo "7 + 13 = " . sum(7, 13) . "<br>";
echo "2 + 4 = " . sum(2, 4);

 Example 2: recursive functions


function fact($n)
{ if ($n <= 1) return 1;
return ($n * fact($n-1)); }
echo(fact(5)); // Outputs 120

10 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Returning Arrays
<?php
function create_array($num) Define a Function
{
for ($i=0; $i<$num; $i++)
{
$array[]=$i; Save data in array
}
return $array; Return array to function
}
$arr_data=create_array(3);
echo "The Final Created Array is <br>";
print_r($arr_data);
?>
11 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Returning Lists
<?php
function create_list($num)
{
for ($i=0; $i<$num; $i++)
{
$array[]=$i;
}
return $array;
}
list($first,$second,$third)=create_list(3);
echo "The Final Created list is <br>";
echo "$first,$second,$third";
?>© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
12
Dr.Ramandeep Kaur
Anonymous Functions
 those functions that we create without a name
 suppose we need to pass a function as a parameter to another
function , create an anonymous function and assign it to a
variable. Then, we can pass this variable as a parameter.
<?php
$v=function($x, $y){ return Example:1
$x+$y;};
echo $v(3, 4);
?>

<?php
$var = function ($x) {return pow($x,3);}; Example:2
echo "cube of 3 = " . $var(3);
?>
13 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Variable Scope
 Functions provide a medium of hiding unnecessary data from
others
 Variables can be wrapped in function to control accessibility
 Scope decides where the variable can be accessed/
referenced in the script
 PHP has three different variable scopes:
 local
 global
 static

14 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Global and Local Scope

<?php
$outer=5;
function scope()
{
$inner=10;
echo "This is a variable with local Scope ", $inner, "<br>";
echo"The outer variable is out of scope and gives error",$outer,"<br>";
}
scope();
echo "This is a variable with global Scope ",$outer;
?>
15 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
The global Keyword
 The global keyword is used to access a global variable from
within a function.
 To do this, use the global keyword before the variables
(inside the function):
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest(); // run function
echo $y; // output the new value for variable $y =15
?>
16 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
 HP also stores all global variables in an array called
$GLOBALS[index].
 The index holds the name of the variable.
 This array is also accessible from within functions and can be
used to update global variables directly.
$x = 5; $y = 10;
function myTest()
{ $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y']; }
myTest();
echo $y; // outputs 15

17 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
The static Keyword
 Normally, when a function is completed/executed, all of its
variables are deleted.
 However, sometimes we want a local variable NOT to be
deleted.
 Inorder to retain the value of a variable, static keyword is
used

18 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Example
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}

myTest(); //output:0
echo "<br>";
myTest(); //output:1
echo "<br>";
myTest(); //output:2
?>

19 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Cal By Value
 PHP allows you to call function by value and reference both.
In case of PHP call by value, actual value is not modified if it
is modified inside the function.

Example-1 Example-2
<?php <?php
function adder($str2) function increment($i)
{ {
$str2 .= 'Call By Value'; $i++;
} }
$str = 'Hello '; $i = 10;
adder($str); increment($i);
echo $str; //OUTPUT: Hello echo $i; //OUTPUT: 10
?> ?>
20 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Call By Reference
 Incall by reference, actual value is modified if it is modified
inside the function.
 In such case, you need to use & (ampersand) symbol with
formal arguments.
 The & represents reference of the variable.
Example-1 Example-2
function adder(&$str2) function increment(&$i)
{ {
$str2 .= 'Call By Reference'; $i++;
} }
$str = 'This is '; $i = 10;
adder($str); increment($i);
echo $str; echo $i;
//Output:This is Call By Reference //Output: 11
21 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
include and require Statements
 It is possible to insert the content of one PHP file into another
PHP file (before the server executes it), with the include or
require statement.
 The include and require statements are identical, except upon
failure:
 require will produce a fatal error (E_COMPILE_ERROR) and stop
the script
 include will only produce a warning (E_WARNING) and the script
will continue
 So, if you want the execution to go on and show users the output,
even if the include file is missing, use the include statement.
 you can create a standard header, footer, or menu file for all your
web pages. Then, when the header needs to be updated, you can
only update the header include file.

22 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
File 1: footer.php
Syntax
<?php include 'filename';
echo "<p>Copyright &copy; 1999-" . or
date("Y") . " W3Schools.com</p>"; require 'filename';
?>

File 2: Use_footer.php
<html>
<body>

<h1>Welcome to my home page!</h1>


<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>

</body>
</html>
23 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Example 2: menu.php
<?php
echo '<a href="/default.asp">Home</a> -
<a href="/html/default.asp">HTML Tutorial</a> -
<a href="/css/default.asp">CSS Tutorial</a> -
<a href="/js/default.asp">JavaScript Tutorial</a> -
<a href="default.asp">PHP Tutorial</a>';
?>
Webpage.php
<html>
<body>
<div class="menu">
<?php include 'menu.php';?>
</div>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
</body>
</html>
© Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
24
Dr.Ramandeep Kaur
Include versus require
 when a file is included with the include statement and PHP cannot
find it, the script will continue to execute
 In case of require statement, the echo statement will not be
executed because the script execution dies after the require
statement returned a fatal error
<?php include 'noFileExists.php';
echo "I have a $color $car.";
?>

Include Require

25 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Include_once()
 The include_once keyword is used to embed PHP code from
another file. If the file is not found, a warning is shown and
the program continues to run. If the file was already included
previously, this statement will not include it again.
<html>
<body>

<h1>Welcome to my home page!</h1>


<p>Some text.</p>
<p>Some more text.</p>
<?php include_once 'footer.php';?>

</body>
</html>
26 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
require_once()
 The require_once keyword is used to embed PHP code from
another file.
 If the file is not found, a fatal error is thrown and the
program stops.
 If the file was already included previously, this statement will
not include it again.
<html> <body>

<h1>Welcome to my home page!</h1>


<p>Some text.</p>
<p>Some more text.</p>
<?php require_once 'footer.php';?>
</body> </html>

27 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
$ and $$ Variables
 Te $var (single dollar) is a normal variable with the name var
that stores any value like string, integer, float, etc.
 The $$var (double dollar) is a reference variable that stores
the value of the $variable inside it.
Example:1 Example:2
<?php
$x = "abc"; <?php
$$x = 200; $name="Cat";
echo $x."<br/>"; //outpur abc ${$name}="Dog";
echo $$x."<br/>"; //outpur 200 ${${$name}}="Monkey";
echo $abc; //outpur 200 echo $name. "<br>";
?> echo ${$name}. "<br>";
echo $Cat. "<br>";
echo ${${$name}}. "<br>";
echo $Dog. "<br>";
?>
28 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
Difference between print and echo
echo print
1. It has no return value 1. It has a return value of 1 so it
2. It can take multiple parameters can be used in expressions
3. echo is marginally faster than print 2. It can take one argument
4. Examples 3. print is marginally slower than
1. echo "<h2>PHP is Fun!</h2>"; echo
2. echo "Hello world!<br>"; 4. Examples
3. echo "I'm about to learn
PHP!<br>";
1. print "<h2>PHP is
Fun!</h2>";
4. echo "This ", "string ", "was ",
"made ", "with multiple 2. print "Hello world!<br>";
parameters."; 3. print "I'm about to learn PHP!";

29 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur
=, == and === in PHP
 The assignment equality = operator only assigns values.
 The equality == does not assign values, but compares them
without checking their data types.
 The triple equals sign operator === won't do assignment,
but will check for equality of values and the data type.

30 © Institute of Information Technology and Management, D-29, Institutional Area, Janakpuri, New Delhi-110058
Dr.Ramandeep Kaur

You might also like