0% found this document useful (0 votes)
106 views23 pages

PHP Array and Function Basics

The document provides information about PHP functions and arrays. It discusses creating indexed and associative arrays in PHP and using functions like count() to get the length of an array. It also covers multidimensional arrays, sorting arrays, and defining user functions in PHP with arguments and return values. Functions can be used to manipulate and return array data.

Uploaded by

srinivas890
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views23 pages

PHP Array and Function Basics

The document provides information about PHP functions and arrays. It discusses creating indexed and associative arrays in PHP and using functions like count() to get the length of an array. It also covers multidimensional arrays, sorting arrays, and defining user functions in PHP with arguments and return values. Functions can be used to manipulate and return array data.

Uploaded by

srinivas890
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

K Mahesh

RCEW, Pasupula (V), Nandikotkur Road,


Near Venkayapalli, KURNOOL
Syllabi
• Introduction to PHP: The problem with other
Technologies ( Servelets and JSP),
• Downloading, installing, configuring PHP,
Programming in a Web environment and the
anatomy of a PHP Page.
• Overview of PHP Data types and Concepts: Variables
and data types, Operators, Expressions and
Statements, Strings, Arrays and Functions.
• PHP Advanced Concepts: Using Cookies, Using HTTP
Headers, Using Sessions, Authenticating users, Using
Environment and Configuration variables, Working
with Date and Time.
PHP Arrays
• An array stores multiple values in one single variable.
• An array can hold many values under a single name,
and access the values by referring to an index
number.
• In PHP, the array() function is used to create an array.
• 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
The count()
• The count() function is used to return the length (the
number of elements) of an array.
<!DOCTYPE html>
<html>
<body>

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>

</body>
</html>
PHP Indexed Arrays
• There are two ways to create indexed arrays:
– The index can be assigned automatically (index
always starts at 0)
$cars = array("Volvo", "BMW", "Toyota");
– or the index can be assigned manually
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<body> <body>

<?php <?php
$cars = array(11,22,33,44,55); $cars = array(11,22,33,44,55);
foreach($cars as $i) for($i=0;$i<count($cars);$i++)
echo "$i","</br>"; echo "cars[$i] = $cars[$i]","</br>";
?> ?>

</body> </body>
</html> </html>
PHP Associative Arrays
• Associative arrays are arrays that use named
keys that you assign to them.
• There are two ways to create an associative
array
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
• Or
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
<!DOCTYPE html>
<html>
<body>

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $x_value) {


echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>

</body>
</html>
OUTPUT
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43
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.
• The dimension of an array indicates the number of indices 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

$cars = array (
  array("Volvo",22,18),
  array("BMW",15,13),
  array("Saab",5,2),
  array("Land Rover",17,15)
);
<!DOCTYPE html>
<html>
<body>

<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);

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

</body>
</html>
<!DOCTYPE html>
<html>
<body>

<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13), OUTPUT
array("Saab",5,2), Row number 0
array("Land Rover",17,15) Volvo
); 22
18
for ($row = 0; $row < 4; $row++) { Row number 1
echo "<p><b>Row number $row</b></p>"; BMW
echo "<ul>"; 15
for ($col = 0; $col < 3; $col++) { 13
echo "<li>".$cars[$row][$col]."</li>"; Row number 2
} Saab
echo "</ul>"; 5
} 2
?> Row number 3
Land Rover
</body> 17
</html> 15
PHP - Sort Functions For Arrays
• sort() - sort arrays in ascending order
• rsort() - sort arrays in descending order
• asort() - sort associative arrays in ascending
order, according to the value
• ksort() - sort associative arrays in ascending
order, according to the key
• arsort() - sort associative arrays in descending
order, according to the value
• krsort() - sort associative arrays in descending
order, according to the key
<!DOCTYPE html>
<html>
<body>

<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);

$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>
OUTPUT
</body> Volvo
</html> Toyota
BMW
Previous Questions on Array

• Write a PHP script to sort the elements in


descending order of an array
PHP - 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.
• PHP User Defined 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.
• A user-defined function declaration starts with the
word function
function functionName() {
  code to be executed;
}
<!DOCTYPE html>
<html>
<body>

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

writeMsg();
?>

</body>
</html>
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
<!DOCTYPE html>
comma. <html>
<body>
<?php
function printName($fname,$lname) {
echo "$fname $lname<br>";
}
printName("mahesh","k");
printName("aaa","bb");
?>
</body>
</html>
PHP Function Arguments
• PHP is a Loosely Typed Language
<?php
function addNumbers(int $a, int $b) {
return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is NOT enabled "5 days" is changed to int(5), and it will return 10
?>

<?php declare(strict_types=1);  // strict requirement

function addNumbers(int $a, int $b) {


  return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is enabled and "5 days" is not an integer, an error will be thrown
?>
PHP Default Argument Value
• If we call the function without arguments it takes the
default value as argument.
<?php declare(strict_types=1); // strict requirement ?>
<!DOCTYPE html>
<html>
<body>

<?php
function setHeight(int $minheight = 50) {
echo "The height is : $minheight <br>";
}

setHeight(350);
setHeight(); OUTPUT
?> The height is : 350
The height is : 50
</body>
</html>
<?php declare(strict_types=1); // strict requirement ?>
<!DOCTYPE html>
<html>
<body>

<?php
function sum(int $x=5, int $y=10) {
$z = $x + $y;
return $z;
}

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


echo "13 + 10 = " . sum(13) . "<br>";
echo "2 + 4 = " . sum(2,4);
?>

</body>
</html> OUTPUT
5 + 10 = 15
13 + 10 = 23
2+4=6
PHP Functions - Returning values
• use the return statement

<?php declare(strict_types=1); // strict requirement ?>


<!DOCTYPE html>
<html>
<body>

<?php
function sum(int $x, int $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);
?>

</body>
</html>
PHP Return Type Declarations
• To declare a type for the function return, add a colon ( : ) and the type
right before the opening curly ( { )bracket when declaring the function.

<?php declare(strict_types=1); // strict requirement


function addNumbers(float $a, float $b) : float {
  return $a + $b;
}
echo addNumbers(1.2, 5.2);
?>
OR

<?php declare(strict_types=1); // strict requirement


function addNumbers(float $a, float $b) : int {
  return (int)($a + $b);
}
echo addNumbers(1.2, 5.2);
?>
Passing Arguments by Reference
• function argument is passed by reference, changes to the argument
also change the variable that was passed in.
• To turn a function argument into a reference, the & operator is used:
<!DOCTYPE html>
<html>
<body>

<?php
function add_five(&$value) {
$value += 5;
}

$num = 2;
add_five($num);
echo $num;
?>

</body> OUTPUT
</html> 7

You might also like