Notes of PHP Part 3

Arrays In PHP

Array store more then one data element of same data type with a single name. so we say that array is just a storage area where you hold numbers and text. The problem is, a variable will hold only one value. You can store a single number in a variable, or a single string.
An array is like a special variable, which can hold more than one number, or more than one string, at a time. If you have a list of items (like a list of customer orders, for example), and you need to do something with them, then it would be quite cumbersome to do this:


$Order1 = "Black shoes";
$Order2 = "Tan shoes";
$Order3 = "Red shoes";
$Order4 = "Blue shoes";


In the above code , we had four items, and all with a different variable name: $Order1, $Orde2, $Order3, and $Order4. With an rray, you can just use a single name.we declare an array is the following way:


$Order= array( );


First you type out what you want your array to be called ($Order, in the array above) and, after an equals sign, you type this:
array( );
You can use two basic methods to put something into an array.
First Way:
The first method involvesstyping your values between the round brackets of array(). In the code below, we're setting up an array to hold the seasons of the year:
Arrays work by having a position, and some data for that position. In the above array, "Autumn" is in position zero, "Winter" is in position 1, "Spring" is in position 2, and "Summer" is in position 3.
The first position is always zero, unless you tell PHP otherwise. But the position is know as a Key. The Key then has a value attached to it. You can specify your own numbers for the Keys. If so, you do it like this:
$seasons = array( 1 => "Autumn", 2 => "Winter", 3 => "Spring", 4 => "Summer" );
So you type a number for your key, followed by the equals sign and a right angle bracket ( => ). In the array above, the first Key is now 1 and not 0. The item stored under key 1 is "Autumn". The last key is 4, and the item stored under key 4 is "Summer". Careful of all the commas, when you set up an array like this. Miss one out and you'll get error messages. Here's the keys and values that are set up in the array above:



1=> "Autumn",
2=> "Winter",
3=> "Spring",
4=> "Summer"

2nd Way:
Another way to put values into an array is like this:
$seasons = array();
$seasons[ ]="Autumn";
$seasons[ ]="Winter";
$seasons[ ]="Spring";
$seasons[ ]="Summer";
Here, the array is first set up with $seasons = array();. This tells PHP that you want to create an array with the name of $seasons. To store values in the array you first type the name of the array, followed by a pair of square brackets:
$seasons[ ]
After the equals sign, you type out what you want to store in this position. Because no numbers were typed in between the square brackets, PHP will assign the number 0 as the first key:


0=> "Autumn",
1=> "Winter",
2=> "Spring",
3=> "Summer"

This is exactly the same as the array you saw earlier. If you want different numbers for your keys, then simply type them between the square brackets:
$seasons[1]="Autumn";
$seasons[2]="Winter";
$seasons[3]="Spring";
$seasons[4]="Summer";
PHP will then see your array like this:


1=> "Autumn",
2=> "Winter",
3=> "Spring",
4=> "Summer"

This method of creating arrays can be very useful for assigning values to an array within a loop. Here's some code:

</pre> $start = 1; $times = 2; $answer = array(); for ($start; $start < 11; $start++) { $answer[$start] = $start * $times; }

PHP Function

A function is just a segment of code, which is written as separate from the other code and execute when we call it other wise this code not execute. we can use it over and over when we need it. It's a chunk of code that you think is useful, and want to use again. Functions save you from writing the code over and over.
Function must have a unique name. and then each function have a body with some statment. body start with { and end with } Whatever you function does goes between the curly brackets. Here's a simple example that just print something out:
Suppose you need to display the message and it is possible that we display this message more then one time then we make a function for this purpose:

function display_error_message()
{
print "Error Detetceted";
}


Calling a function in PHP

if you want to call this function then write the function name
display_error_message( );
You can write function anywhere in the page. But for neatness and readability's sake, it's better to put all of your function either at the top or bottom of your scripts. Or better yet, in a separate PHP file. You can then use another inbuilt function called "Include" (which we'll discuses in other tutorial)

Passing Functions Arguments in PHP

Information may be passed to functions via the argument . Function can be handed variables, so that you can do something with what's inside of them. You pass the variable over to your functions by typing them inside of the round brackets of the function name and also directly value to the function. when we pass variable we say " Passing Arguments by Reference ". If we pass value as argument the we say "passing Argument by Value".
You can pass as many as argument your like. These argument work like variables inside your function.
Following example takes two integer parameters and add them together and then print them.


<?php
function addFunction($num1, $num2)
{
$sum = $num1 + $num2;
echo "Sum of the two numbers is : $sum";
}
addFunction(10, 20);
?>

This will display following result:
Sum of the two numbers is : 30
PHP Functions returning value:
A function can return a value using the return statement in conjunction with a value or object. return stops the execution of the function and sends the value back to the calling code.
You can return more than one value from a function using return array(1,2,3,4). We can store the return value for further use.
For example:
We takes two integer parameters and add them together and then returns their sum to the calling program.


<?php
function addFunction($num1, $num2)
{
$sum = $num1 + $num2;
return $sum;
}
$return_value = addFunction(10, 20);
echo "Returned value from the function : $return_value";
?>
Default Values for Function Parameters
You can set a parameter to have a default value if the function's caller doesn't pass it.
<?php
function display($name = NULL)
{
print $name;
}
display("hello PHP");
display();
?>

when first time display call the function it pass the "Hello PHP " and so its print "hello PHP" but Next time when we call the function display then its return Null Value.

Header Function in PHP

The PHP header() function supplies raw HTTP headers to the browser and can be used to redirect it to another location. The redirection script should be at the very top of the page to prevent any other part of the page from loading. The target is specified by the Location: header as the argument to the header() function. After calling this function the exit() function can be used to halt parsing of rest of the code.
Syntax:
header('Location: url);
There are two special-case header calls.
The first is a header that starts with the string "HTTP/" (case is not significant), which will be used to figure out the HTTP status code to send. For example, if you have configured Apache to use a PHP script to handle requests for missing files (using the ErrorDocument directive), you may want to make sure that your script generates the proper status code.

<?php
header("HTTP/1.0 404 Not Found");
?>
The second special case is the "Location:" header.
 in this case we specify the location and force to redirect the page to an other page.<br>
<br>
<?php
header("Location:http://www.learninghints.com/index.php");                /* Redirect browser */
exit;  /* Make sure that code below does not get executed when we redirect. */

?>
You can also pass the some value through the header function
header("location:url?variable=value);
for example
header("location:test.php?id=101");

Note:- if you are passing the long text then must use urlencode(), perhaps in combination withhtmlentities(), these two function will be discussed other Tutorials.

Include Function in PHP

You can include the content of a PHP file into another PHP file with the help of include function. When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward.
For example if you have a file which have some content and you want to use these content in other file then you have two way first you copy the content and past in new file and other method is use include function. with including all the content of that file is become part of new file.
This is a strong point of PHP which helps in creating functions, headers, footers, or elements that can be reused on multiple pages. This will help developers to make it easy to change the layout of complete website with minimal effort. If there is any change required then instead of changing thousand of files just change included file.
For example
if you have a common menu for your web site then make a file which contain some content of you menu and then just include on the top of you every file.
For Example if your menu file is menue.php then
when you want to include this file in new file then your code will be:


<html>
<body>
<?php
include "menue.php" ;
?>
<p> from this point new file begin </p>
</bod>
</html>