Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

PHP- Functions and Arrays

RAJAN NEUPANE
Definition
• A function is a named block of code that does
a specific task.
• It will be executed by a call to the function.
function NameOfFunction() {
echo "my name is Rajan";
}
RAJAN NEUPANE
NameOfFunction();
Types of Functions
• There are two types of functions in PHP:
1. Internal (built-in) Functions
2. User Defined Functions
RAJAN NEUPANE
Some Internal Functions
There are more than 1000 built-in functions

phpinfo() array()
print() date()
mysqli_connect() time()
error_reporting() strlen()
error_log() copy()
RAJAN NEUPANE
PHP Arrays
• An array is a data structure that stores one or
more similar type of values in a single value.
For example if you want to store 100 numbers
then instead of defining 100 variables its easy
to define an array of 100 length.
RAJAN NEUPANE
Types of Arrays
• Numeric array − An array with a numeric index.
Values are stored and accessed in linear fashion.

• Associative array − An array with strings as index.


This stores element values in association with key
values rather than in a strict linear index order.

• Multidimensional array − An array containing


one or more arrays and values are accessed using
multiple indices RAJAN NEUPANE
Indexed Array
• $fruits = array("Apple", "Mango", "Orange",
"Pineapple");
echo $fruits[0] . ", " . $fruits[1] . " , " .
$fruits[2] ." and ". $fruits[3]. " are fruits.";
RAJAN NEUPANE
Associative Arrays
• $population = array("Ghorahi"=>"350000",
"Tulsipur"=>"320000");
echo "Population of Ghorahi is " .
$population['Ghorahi'];

RAJAN NEUPANE
Multidimensional Arrays
• $multiarray = array(
array("Ram", "Sita", "Shyam"),
array("Tulsipur", "Ghorahi", "Khaira"),
);
print_r($multiarray);
RAJAN NEUPANE

You might also like