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>";
?>
CREATING ARRAY 
It's easy to create an array within a PHP script.
To create an array, you use array() construct: $myArrayarray( values );
To create an indexed array, just list the array values inside the parentheses, separated by commas.
EXAMPLE 
<?php
$array 
= array(1111,  1=> 1,  => 119=> 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;
}
EXAMPLE 
<?php
function writeMsg() {
    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