0% found this document useful (0 votes)
2 views7 pages

PHP FunctionArray

The document provides an overview of PHP functions, highlighting the existence of over 1000 built-in functions and the ability to create custom functions. It explains how to define and call functions, pass arguments, and return values, along with details on PHP arrays, including indexed, associative, and multidimensional arrays. Examples are provided to illustrate the usage of functions and arrays in PHP programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

PHP FunctionArray

The document provides an overview of PHP functions, highlighting the existence of over 1000 built-in functions and the ability to create custom functions. It explains how to define and call functions, pass arguments, and return values, along with details on PHP arrays, including indexed, associative, and multidimensional arrays. Examples are provided to illustrate the usage of functions and arrays in PHP programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PHP Functions

The real power of PHP comes from its functions.

PHP has more than 1000 built-in functions, and in addition you can create your own custom
functions.

PHP Built-in 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.

PHP User Defined Functions

Besides the built-in PHP functions, it is possible to create your own functions.

• A function is a block of statements that can be used repeatedly in a program.


• A function will not execute automatically when a page loads.
• A function will be executed by a call to the function.
Create a Function

A user-defined function declaration starts with the keyword function, followed by the name of the
function:
Example

function myMessage() {

echo "Hello world!";

Call a Function

To call the function, just write its name followed by parentheses ():
Example

function myMessage() {

echo "Hello world!";

myMessage();
Example:
<html>
<body>

<?php
function myMessage() {
echo "Hello world!";
}

myMessage();
?>

</body>
</html>

In our example, we create a function named myMessage().

The opening curly brace { indicates the beginning of the function code, and the closing curly
brace } indicates the end of the function.

The function outputs "Hello world!".

PHP Function Arguments

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) {

echo "$fname Refsnes.<br>";

familyName("Jani");

familyName("Hege");

familyName("Stale");

familyName("Kai Jim");

familyName("Borge");
PHP Functions - Returning values

To let a function return a value, use the return statement:


Example

function sum($x, $y) {

$z = $x + $y;

return $z;

echo "5 + 10 = " . sum(5, 10) . "<br>";

echo "7 + 13 = " . sum(7, 13) . "<br>";

echo "2 + 4 = " . sum(2, 4);

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.

PHP Array Types

In PHP, there are three types of arrays:

• Indexed arrays - Arrays with a numeric index


• Associative arrays - Arrays with named keys
• Multidimensional arrays - Arrays containing one or more arrays
Array Items

Array items can be of any data type.

The most common are strings and numbers (int, float), but array items can also be objects, functions
or even arrays.

You can have different data types in the same array.


Example

Array items of four different data types:

$myArr = array("Volvo", 15, ["apples", "bananas"], myFunction);

PHP Indexed Arrays

To access an array item you can refer to the index number.


Example

Display the first array item:

$cars = array("Volvo", "BMW", "Toyota");

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

Display all array items:

$cars = array("Volvo", "BMW", "Toyota",”gchgc”);

foreach ($cars as $x) {

echo "$x <br>";

PHP Associative Arrays

Associative arrays are arrays that use named keys that you assign to them.
Access Associative Arrays

To access an array item you can refer to the key name.


Example

Display the model of the car:

$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);

echo $car["model"];

$car[0];
Change Value

To change the value of an array item, use the key name:


Example

Change the year item:

$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);

$car["model"] = “Maruti”;

var_dump($car);

Loop Through an Associative Array

To loop through and print all the values of an associative array, you could use a foreach loop, like
this:
Example

Display all array items, keys and values:

$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);

foreach ($car as $x => $y) {

echo "$x: $y <br>";

}
PHP - Multidimensional Arrays

A multidimensional array is an array containing one or more 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.

• For a two-dimensional array you need two indices to select an element


• For a three-dimensional array you need three indices to select an element
PHP - Two-dimensional Arrays

A two-dimensional array is an array of arrays (a three-dimensional array is an array of arrays of


arrays).

First, take a look at the following table:

Name Stock Sold

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 $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>";


We can also put a for loop inside another for loop to get the elements of the $cars array (we still have
to point to the two indices):
Example

for ($row = 0; $row < 4; $row++) {

echo "<p><b>Row number $row</b></p>";

echo "<ul>";

for ($col = 0; $col < 3; $col++) {

echo "<li>".$cars[$row][$col]."</li>";

echo "</ul>";

You might also like