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:
function display_error_message() { print "Error Detetceted"; }
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)
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.
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.
Default Values for Function Parameters You can set a parameter to have a default value if the function's caller doesn't pass it.
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.
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.
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.
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:
from this point new file begin
Notes of PHP for Beginners Part 1
Notes of PHP for Beginners Part 2
Notes of PHP for Beginners Part 3