PHP FunctionArray
PHP FunctionArray
PHP has more than 1000 built-in functions, and in addition you can create your own custom
functions.
PHP has over 1000 built-in functions that can be called directly, from within a script, to perform a
specific task.
Please check out our PHP reference for a complete overview of the PHP built-in functions.
Besides the built-in PHP functions, it is possible to create your own functions.
A user-defined function declaration starts with the keyword function, followed by the name of the
function:
Example
function myMessage() {
Call a Function
To call the function, just write its name followed by parentheses ():
Example
function myMessage() {
myMessage();
Example:
<html>
<body>
<?php
function myMessage() {
echo "Hello world!";
}
myMessage();
?>
</body>
</html>
The opening curly brace { indicates the beginning of the function code, and the closing curly
brace } indicates the end of the function.
Information can be passed to functions through arguments. An argument is just like a variable.
Arguments are specified after the function name, inside the parentheses. You can add as many
arguments as you want, just separate them with a comma.
The following example has a function with one argument ($fname). When the familyName() function
is called, we also pass along a name, e.g. ("Jani"), and the name is used inside the function, which
outputs several different first names, but an equal last name:
Example
function familyName($fname) {
familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
PHP Functions - Returning values
$z = $x + $y;
return $z;
PHP Arrays
$cars = array("Volvo", "BMW", "Toyota");
<html>
<body>
<pre>
<?php
$cars = array("Volvo", "BMW", "Toyota");
var_dump($cars);
?>
What is an Array?
An array is a special variable that can hold many values under a single name, and you can access the
values by referring to an index number or name.
The most common are strings and numbers (int, float), but array items can also be objects, functions
or even arrays.
echo $cars[0];
Loop Through an Indexed Array
To loop through and print all the values of an indexed array, you could use a foreach loop, like this:
Example
Associative arrays are arrays that use named keys that you assign to them.
Access Associative Arrays
echo $car["model"];
$car[0];
Change Value
$car["model"] = “Maruti”;
var_dump($car);
To loop through and print all the values of an associative array, you could use a foreach loop, like
this:
Example
}
PHP - Multidimensional Arrays
PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However,
arrays more than three levels deep are hard to manage for most people.
The dimension of an array indicates the number of indices you need to select an element.
Volvo 22 18
BMW 15 13
Saab 5 2
Land Rover 17 15
We can store the data from the table above in a two-dimensional array, like this:
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
Now the two-dimensional $cars array contains four arrays, and it has two indices: row and column.
To get access to the elements of the $cars array we must point to the two indices (row and column):
To get access to the elements of the $cars array we must point to the two indices (row and column):
Example
echo "<ul>";
echo "<li>".$cars[$row][$col]."</li>";
echo "</ul>";