Saturday, May 16, 2009

FunctionsUsage In PHP

Functions are one of the most useful features of PHP and also in other languages. They allow a programmer to encapsulate sections of code within a structure which can then be executed from anywhere else. Functions are instantiated by use of the function keyword, followed by the user-defined function name including a list of parameters between the parentheses and the body of the function is contained between braces and there's a statements.
when defining variables, specific naming conventions must be adhered to. A function name can contain letters, numbers and underscores, and cannot start with a number like (2abx,45khan etc). They are also, like variables, case-sensitive. e.g

1 function my_first_function()
2 {

3 echo "Hello world";
4 $a = 2;
5 echo "The value of A is $a";
6 }
7 my_first_function();

The Output
Hello world
The value of A is 2

Line 1 : Created a function “my_function”. The empty brackets () are used to pass arguements.
Lines 2-6 : The starting brace is from line 2 to Ending Brace on line 6.Inside the braces you can place whatever code you desire
Line 7 : This is a function call and to execute the function at this point. This differs from the function declaration in that we are not using the function keyword to create a function. This function call can occur anywhere within the code, as long as it occurs after the function has been declared



Global and Local Scope
Scope is defined, the context within which a variable, or a function is defined. For the most part, any variable defined within the flow of a program is available throughout the rest of that program, or within the global scope of the program. There are a few cases where scope changes though, and one of these is when entering a function definition.

You can think of functions as self-contained sections of code, and while writing a function, you don’t need to worry about what might be happening outside the function definition, you can concentrate on its inner workings. Every function has its own scope inside which, variables are “hidden from the outside world”, and vice-versa.

More precisely, any variables which you declare within the function will not be available from outwith the function. So, the following code


function my_function(){
$a = 5;
}
echo $a;

Output

That’s right, nothing! (PHP by default treats undefined variables as if they are null, or blank) This also happens in reverse. Let’s try referencing a variable created outside a function from inside the function


$b = 20;
function my_function()
{
echo $b;
$b = 10;
echo $b;
}
my_function();

Output

20

10

Here, we see the value of $b that was created inside and out side the function. From the function’s point of view, when we reference $b in line 4, it doesn’t exist. We can fix this by use of the global keyword

The use of the “global” keyword

$b = 20; //defined in the "global scope"
function my_function(){
global $b;
echo $b;
$b = 10; //assigns $b = 10 _in the global scope_
echo $b;
}
my_function();
echo $b;

Output

20
10
10
the use of “global” tells the function to use the version of $b that has been defined globally, that is, the version that has been defined outside the function but within the same scope as the function definition.

You might wonder though, why the value of $b in line 9 is printed as “10″. That’s because, by defining $b as global within the function, we are telling the function to treat any expression assigning $b a value (such as “$b = 10″ on line 5) to be working with the global variable $b, and not a local variable.

Function Parameters

let’s look at that pair of parentheses () which was empty in our initial function declarations. We’ll now move on to see what happens when we fill this space up with variables

function my_function($a)
{
$b = 20;
echo $b+$a;
}
my_function(21);
my_function(10);


you’ll see on line 1, we’ve introduced the variable $a into the function definition, but this isn’t treated as a variable in the same way we’ve seen so far. Instead, the definition of $a here is treated as an argument to the function, and this means that the function can now accept user values defined at the time of the function call (in lines 5 & 6 in the above example)

The above code will produce the output

41
30

we’re passing the values 21 and 10 as arguments to the function, and these arguments are then assigned as locally defined variables within the scope of the function. Let’s see what happens when we reintroduce the “global” keyword here

$a = 50;
function my_function($a)
{
global $a;
$b = 20;
echo $b+$a;
}
my_function(21);


output
70

The globally defined $a over-rides the locally defined $a, so you can see there how variable scope affects the execution of PHP code if you’re not careful


Passing Variables Into Functions

We can also pass the values of variables into functions, and this is a very useful way of creating PHP code where functions are used to perform complex calculations on values. Here, we’ll also make use of the “return” keyword, to return a value from the function. This essentially turns the function into a calculator which can take in values, and also output values back into the program

Note that it doesn’t matter what the name of the variable is that you pass into the function, since the function will recreate the value of the variable within its body, and assign it a locally defined name.


function my_function($a)
{
$a = $a + 50;
return $a;
}
$b = 100;
$X = my_function($b);
echo $X;

Output

150

we are assigning the value of 100 to the variable $b in line 5. We are then passing the value of $b (100) into the function, where it is accessible via the locally defined variable $a. We then add 100 onto the value of $a in line 2, and then line 3 is where we introduce the return keyword.

The “Return” Keyword in Functions

Return is used when we wish to end the execution of a function and return a result. Any code inside the function placed after a return statement will not be executed. Instead, program execution returns to the point where the function call was started. In this case, returning to line
6, where the value of the function is assigned to the variable $X.

function my_function($a)
{
$a = $a + 100;
return $a;
}
$b = 100;
$X = my_function($b);
echo $X;

The value of the function here is equal to the value of $a on line 3, at the point when the return statement was called. $a equals 200 here, and so the function returns the value “200″ for storage inside the $X variable.

When passing variables to functions, there are more things to consider though, and the calculation potential of functions isn’t just limited to processing numbers. In the next few sections, I’ll outline how you can use functions to take greater control over the contents of your data structures, and also to process more complex information.


Passing By Reference and Passing By Value

The above example is a case of “passing by value” into a function declaration. But what does that mean? Simply put, it means we were passing the values “21″, “10″ or “100″ into the function for processing. This would work for any other type of variable, you could pass the string value “Hello world”, or the floating point number 1.2345. The only exception here is with arrays, and we’ll cover the behaviour of those in a future tutorial

What is meant by “Passing by reference” though, the opposite of “Passing by value”? If you remember, I mentioned in the earlier tutorial about variables, that it’s possible to reference the address of a variable, rather than the value contained within the variable. The same can be said of the parameters list for functions.

function my_function($a, &$b)
{
//$b passed _by parameter_
$b = 10;
$b = $b+$a; //if $b was passed _by value_ this wouldn't affect the global definition of $b=100
}
$x = 100;
my_function(21, $x);
echo $x;


Possibly a slightly unexpected result? Indeed, you may think so. But consider this - by defining the second argument to the function as &$b we are saying that the function shouldn’t accept the value of $b as an argument, but should instead accept $b itself, and perform any calculations on the variable as if it were globally defined. If we were passing by value (and not by reference) we would ensure that the value of $b was not changed outside the function. But by passing by reference, we can allow the function to alter the value of $b within the global scope.

An important point to make here is that you can’t pass a value by reference. So, for example, the following code

function passed(&$a)

{
echo $a;
}
passed(5);

the will not work...

function passed(&$a)
{
echo $a;
}
$x = 5;
passed($x);

will work without a problem (note how we’re passing the address of the variable $x in the second example. In the first example, the value 5 cannot have an address, since it cannot have an address location

Sunday, January 25, 2009

Ghalib

It's compelling beauty makes all menEnjoy the glory of the bloom;
But the eye should take in every view,
The brighter shades, the shades of gloom.


Although love's pangs may fatal be,
There can be no way out
Without love too this heart would grieve
For want of things to grieve about.

To whom, alas, shall I complain
If luck with me does not abide?
"O, give me death" was all I sought,
That blessing too I was denied.


Ask me not why I am sad,
What grief doth clutch my heart.
My heart hath built me a prison-cell
And raised grim walls of narrow truths,
Of cramping loves and hates.

It shuts in the horizon of my thought
And clips my fancy's wings.
Love has left me,
O Ghalib,
a good-for-nothing,
Otherwise a useful man I was.

Ghalib

Wohee aik baat hai joo yaan nafas,waan nikahat-e-Gul hai

Chaamaan ka jalwa, Bahes hai meri rangeenee nawayee ka...

Poet Ghalib