UNIT II---WORKING WITH ARRAYS AND FUNCTIONS
WORKING
WITH ARRAYS
Arrays are collections of related values.
such as the data submitted from a form, the
names of students in a class, or the populations of a list of cities..
EXAMPLE
You can create an array
using the array() function, like this:
<?php
$array = array();
?>
ARRAY
DEFINITION
An array is a data structure that
stores one or more similar type of values in a single value.
If you wish, you may send
the desired new elements of the array along as a parameter.
* Numeric array
* Associative array
* Multidimensional array
NUMERIC
ARRAY
These arrays can store numbers, strings
and any object but their index will be represented by numbers. By default array index starts from
zero.
Syntax
array(Value1, Value2, ValueN);
Example
<?php
$subject = array("C", "C++", "JAVA", "PHP");
echo $subject[3];
Associative
Arrays
These type of arrays are
similar to the indexed arrays but instead of linear storage, every value can be
assigned with a user-defined key of string type.
Example
<?php
$ages = array("Peter"=>22, "Clark"=>32, "John"=>28);
?>
MULTIDIMENSIONAL ARRAY
As we have already seen how to create an
associative array, and multidimensional array being an array of arrays, will
hold the associative arrays inside it.
Syntax
array ( array (elements...), array (elements...), ...
EXAMPLE
<?php
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
?>
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
?>
CREATING
ARRAY
It's easy to create an array within a PHP script.
To create an array, you use array() construct: $myArray = array( values );
To create an indexed array, just list the array values inside the parentheses,
separated by commas.
EXAMPLE
<?php
$array = array(1, 1, 1, 1, 1, 8 => 1, 4 => 1, 19, 3 => 13);
print_r($array);
?>
$array = array(1, 1, 1, 1, 1, 8 => 1, 4 => 1, 19, 3 => 13);
print_r($array);
?>
ARRAY
RELATED FUNCTION WORKING WITH FUNCTION
arrays are also a big part of the Perl
language.
Perl has a lot of functions to help you work with them.
Some of the actions arrays perform include deleting
elements, checking for the existence of an element, reversing all of the the elements in an array, and sorting the elements.
EXAMPLE
sizeof($arr) This function returns the size of the array or the number of data
elements stored in the array. ...
is_array($arr) ...
in_array($var, $arr) ...
print_r($arr) ...
array_merge($arr1, $arr2) ...
array_values($arr) ...
array_keys($arr) ...
array_pop($arr)
CALLING
FUNCTION
variable name using bracket and
parameters and the other is by
using call_user_func()
Function but in both methodvariable name is to be used.
Using eval() Function:
The eval() function is an
inbuilt function in PHP which is used to evaluate string
as PHP code.
DEFINITING
FUNCTION
A function
name can start with a letter or underscore (not a number).
A user-defined
function declaration starts with the word function:
Syntax
function functionName()
{
code to be executed;
}
code to be executed;
}
EXAMPLE
<?php
function writeMsg() {
echo "Hello world!";
}
echo "Hello world!";
}
RETRUNING
VALUE FROM USER DEFINE FUNCTION
The return values must be specified in the variable.
If the statement is called within a function the function is terminated immediately and
pass control back to the previous position from which it was called.
The returnstatement can return any type of Data.
SYNTAX
function function-name()
{
statement 1 :
statement 2 :
statement 3 : ......
}
EXAMPLE
<?php
function myfunction()
{ echo "Good Morning"; }
myfunction();
?>
writeMsg(); // call the function
?>
?>
Comments
Post a Comment