Firstly apologies for keeping Day2 so small. I know it was a cake walk for you, but I was a bit busy and did not get chance to write more. Anyway, lets leave what happened in past and move onto todays work. Lets try to make up for yesterday's loss.
Functions
Syntax: some_function($var1, $var2,...,$varn)
Example:
print("some data"); is a function
abs($var1); is also a function.
<html>
<head>
<title></title>
</head>
<body>
<?php
$var1 = -10;
print (abs($var1));
?>
</body>
</html>
Result: 10
Although we passed negative value to the variable, yet it returned 10 value, reason being - abs() function is an absolute function. It returns absolute value without signs.
----------------------------------------------------------------------------------------
Defining a Function
function some_function($var1, $var2)
{
//some logic
}
On similar lines is returning the value from the function. Below mentioned example will show you how to define a function and how to return a value from a function.
Example:
<html>
<head>
<title></title>
</head>
<body>
<?php
$var1 = 10;
$var2 = 11;
function myfirstfunction($var1, $var2)
{
//addition
$var3 = $var1 + $var2;
print $var3;
}
myfirstfunction(10,20);
//function returning value
function mysecondfunction($var1,$var2)
{
$var3 = $var1 + $var2;
return $var3;
}
$var4 = mysecondfunction (20,40);
print "<br>".$var4;
?>
</body>
</html>
Result:
30
60
------------------------------------------------------------------------------
Variable Scope:
Scope of the variable remains confined to its function. Similarly, a variable declared outside the function will not be accessible within the function.
Global Variables:
These are variables which are available to everyone in every function i.e. also outside the function in which it has been defined.
Syntax: global $var1;
Static Variables:
These variables can maintain their state in the event of repeated calls to the same function.
<html>
<head>
<title></title>
</head>
<body>
<?php
$var1 = 10;
$var2 = 11;
print $var3;
//function returning value
function myfunc2($var1,$var2)
{
static $count01 = 1;
$var3 = $var1 + $var2;
print $count01++."<br>";
return $var3;
}
myfunc2(20,40);
myfunc2(10,40);
myfunc2(234,340);
?>
</body>
</html>
Result:
1
2
3
Function with an Optional Argument
Example:
function fontWrap( $txt, $size=3 )
{
//some statements
}
This function can work with 2 arguments supplied as well as just 1 argrument supplied.
i.e.
fontWrap("test data", 5); // in this case $size will assume value 5
OR
fontWrap("test data123"); // in this case $size will have the default value as 3.
CALL BY VALUE AND CALL BY REFERENCE
This is again standard thing.
Call by value is something which we have been doing in all our examples above.
Call by refrence has been shown with an example below:
<html>
<head>
<title></title>
</head>
<body>
<?php
//function returning value
function myfunc3($var1)
{
$var1++;
}
$var2 = 10;
myfunc3(&$var2);
print $var2;
?>
</body>
</html>
Result: 11
<html>
<head>
<title></title>
</head>
<body>
<?php
//function returning value
function myfunc3(&$var1)
{
$var1++;
}
$var2 = 121;
myfunc3($var2);
print $var2;
?>
</body>
</html>
Result: 122
------------------------------------------------------------------------------
TIP
You can also access global variables anywhere in your scripts with a built-in associative array called $GLOBALS. To access a global variable called $test within a function, you could reference it as $GLOBALS[test].
No comments:
Post a Comment