Unit 2
PHP Arrays
An array stores multiple values in one single variable:
Example
$cars = array("Volvo", "BMW", "Toyota");
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
Working With Arrays
In this tutorial you will learn how to work with arrays, including:
Create Arrays
Access Arrays
Update Arrays
Add Array Items
Remove Array Items
Sort 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);
<!DOCTYPE html>
<html>
<body>
<?php
// function example:
function myFunction() {
echo "This text comes from a function";
}
// create array:
$myArr = array("Volvo", 15, ["apples", "bananas"], myFunction);
// calling the function from the array item:
$myArr[3]();
?>
</body>
</html>
O/P :
This text comes from a function
Array Functions
The real strength of PHP arrays are the built-in array functions, like
the count() function for counting array items:
Example
How many items are in the $cars array:
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
PHP Indexed Arrays
In indexed arrays each item has an index number.
By default, the first item has index 0, the second item has item 1, etc.
Example
Create and display an indexed array:
$cars = array("Volvo", "BMW", "Toyota");
var_dump($cars);
array(3) {
[0]=>
string(5) "Volvo"
[1]=>
string(3) "BMW"
[2]=>
string(6) "Toyota"
}
Access 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];
Volvo
Change Value
To change the value of an array item, use the index number:
Example
Change the value of the second item:
$cars = array("Volvo", "BMW", "Toyota");
$cars[1] = "Ford";
var_dump($cars);
array(3) {
[0]=>
string(5) "Volvo"
[1]=>
string(4) "Ford"
[2]=>
string(6) "Toyota"
}
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");
foreach ($cars as $x) {
echo "$x <br>";
Volvo
BMW
Toyota
Index Number
The key of an indexed array is a number, by default the first item is 0 and the
second is 1 etc., but there are exceptions.
New items get the next index number, meaning one higher than the highest
existing index.
So if you have an array like this:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
And if you use the array_push() function to add a new item, the new item will get
the index 3:
Example
array_push($cars, "Ford");
var_dump($cars);
array(4) {
[0]=>
string(5) "Volvo"
[1]=>
string(3) "BMW"
[2]=>
string(6) "Toyota"
[3]=>
string(4) "Ford"
}
But if you have an array with random index numbers, like this:
$cars[5] = "Volvo";
$cars[7] = "BMW";
$cars[14] = "Toyota";
And if you use the array_push() function to add a new item, what will be the
index number of the new item?
Example
array_push($cars, "Ford");
var_dump($cars);
The next array item gets the index 15:
array(4) {
[5]=>
string(5) "Volvo"
[7]=>
string(3) "BMW"
[14]=>
string(6) "Toyota"
[15]=>
string(4) "Ford"
}
PHP Associative Arrays
Associative arrays are arrays that use named keys that you assign to them.
Example
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
var_dump($car);
array(3)
{
["brand"]=>
string(4) "Ford"
["model"]=>
string(7) "Mustang"
["year"]=>
int(1964)
}
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"];
Mustang
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["year"] = 2024;
var_dump($car);
array(3) {
["brand"]=>
string(4) "Ford"
["model"]=>
string(7) "Mustang"
["year"]=>
int(2024)
}
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>";
brand: Ford
model: Mustang
year: 1964
Array Keys
When creating indexed arrays the keys are given automatically, starting at 0
and increased by 1 for each item, so the array above could also be created with
keys:
Example
$cars = [
0 => "Volvo",
1 => "BMW",
2 =>"Toyota"
];
array(3) {
[0]=>
string(5) "Volvo"
[1]=>
string(3) "BMW"
[2]=>
string(6) "Toyota"
}
As you can see, indexed arrays are the same as associative arrays, but
associative arrays have names instead of numbers:
Example
$myCar = [
"brand" => "Ford",
"model" => "Mustang",
"year" => 1964
];
array(3) {
["brand"]=>
string(4) "Ford"
["model"]=>
string(7) "Mustang"
["year"]=>
int(1964)
}
Declare Empty Array
You can declare an empty array first, and add items to it later:
Example
$cars = [];
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
array(3) {
[0]=>
string(5) "Volvo"
[1]=>
string(3) "BMW"
[2]=>
string(6) "Toyota"
}
The same goes for associative arrays, you can declare the array first, and then
add items to it:
Example
$myCar = [];
$myCar["brand"] = "Ford";
$myCar["model"] = "Mustang";
$myCar["year"] = 1964;
array(3) {
["brand"]=>
string(4) "Ford"
["model"]=>
string(7) "Mustang"
["year"]=>
int(1964)
}
Mixing Array Keys
You can have arrays with both indexed and named keys:
Example
$myArr = [];
$myArr[0] = "apples";
$myArr[1] = "bananas";
$myArr["fruit"] = "cherries";
array(3) {
[0]=>
string(6) "apples"
[1]=>
string(7) "bananas"
["fruit"]=>
string(8) "cherries"
}
Access Array Item
To access an array item, you can refer to the index number for indexed arrays,
and the key name for associative arrays.
Example
Access an item by referring to its index number:
$cars = array("Volvo", "BMW", "Toyota");
echo $cars[2];
Toyota
To access items from an associative array, use the key name:
Example
Access an item by referring to its key name:
$cars = array("brand" => "Ford", "model" => "Mustang", "year" =>
1964);
echo $cars["year"];
1964
Excecute a Function Item
Array items can be of any data type, including function.
To execute such a function, use the index number followed by parentheses ():
Example
Execute a function item:
function myFunction() {
echo "I come from a function!";
$myArr = array("Volvo", 15, myFunction);
$myArr[2]();
I come from a function!
Use the key name when the function is an item in a associative array:
Example
Execute function by referring to the key name:
function myFunction() {
echo "I come from a function!";
$myArr = array("car" => "Volvo", "age" => 15, "message" =>
myFunction);
$myArr["message"]();
I come from a function!
Loop Through an Associative Array
To loop through and print all the values of an associative array, you can 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>";
brand: Ford
model: Mustang
year: 1964
Loop Through an Indexed Array
To loop through and print all the values of an indexed array, you can use
a foreach loop, like this:
Example
Display all array items:
$cars = array("Volvo", "BMW", "Toyota");
foreach ($cars as $x) {
echo "$x <br>";
Volvo
BMW
Toyota
Update Array Item
To update an existing array item, you can refer to the index number for indexed
arrays, and the key name for associative arrays.
Example
Change the second array item from "BMW" to "Ford":
$cars = array("Volvo", "BMW", "Toyota");
$cars[1] = "Ford";
array(3) {
[0]=>
string(5) "Volvo"
[1]=>
string(4) "Ford"
[2]=>
string(6) "Toyota"
}
To update items from an associative array, use the key name:
Example
Update the year to 2024:
$cars = array("brand" => "Ford", "model" => "Mustang", "year" =>
1964);
$cars["year"] = 2024;
array(3) {
["brand"]=>
string(4) "Ford"
["model"]=>
string(7) "Mustang"
["year"]=>
int(2024)
}
Update Array Items in a Foreach Loop
There are different techniques to use when changing item values in
a foreach loop.
One way is to insert the & character in the assignment to assign the item value
by reference, and thereby making sure that any changes done with the array
item inside the loop will be done to the original array:
Example
Change ALL item values to "Ford":
$cars = array("Volvo", "BMW", "Toyota");
foreach ($cars as &$x) {
$x = "Ford";
unset($x);
var_dump($cars);
array(3) {
[0]=>
string(4) "Ford"
[1]=>
string(4) "Ford"
[2]=>
string(4) "Ford"
}
Note: Remember to add the unset() function after the loop.
Without the unset($x) function, the $x variable will remain as a reference to
the last array item.
To demonstrate this, see what happens when we change the value of $x after
the foreach loop:
Example
Demonstrate the consequence of forgetting the unset() function:
$cars = array("Volvo", "BMW", "Toyota");
foreach ($cars as &$x) {
$x = "Ford";
$x = "ice cream";
var_dump($cars);
array(3) {
[0]=>
string(4) "Ford"
[1]=>
string(4) "Ford"
[2]=>
&string(9) "ice cream"
}
Add Array Item
To add items to an existing array, you can use the bracket [] syntax.
Example
Add one more item to the fruits array:
$fruits = array("Apple", "Banana", "Cherry");
$fruits[] = "Orange";
array(4) {
[0]=>
string(5) "Apple"
[1]=>
string(6) "Banana"
[2]=>
string(6) "Cherry"
[3]=>
string(6) "Orange"
}
Associative Arrays
To add items to an associative array, or key/value array, use brackets [] for the
key, and assign value with the = operator.
Example
Add one item to the car array:
$cars = array("brand" => "Ford", "model" => "Mustang");
$cars["color"] = "Red";
array(3) {
["brand"]=>
string(4) "Ford"
["model"]=>
string(7) "Mustang"
["color"]=>
string(3) "Red"
}
Add Multiple Array Items
To add multiple items to an existing array, use the array_push() function.
Example
Add three item to the fruits array:
$fruits = array("Apple", "Banana", "Cherry");
array_push ($fruits, "Orange", "Kiwi", "Lemon");
array(6) {
[0]=>
string(5) "Apple"
[1]=>
string(6) "Banana"
[2]=>
string(6) "Cherry"
[3]=>
string(6) "Orange"
[4]=>
string(4) "Kiwi"
[5]=>
string(5) "Lemon"
}
Add Multiple Items to Associative
Arrays
To add multiple items to an existing array, you can use the += operator.
Example
Add two items to the cars array:
$cars = array("brand" => "Ford", "model" => "Mustang");
$cars += ["color" => "red", "year" => 1964];
array(4) {
["brand"]=>
string(4) "Ford"
["model"]=>
string(7) "Mustang"
["color"]=>
string(3) "red"
["year"]=>
int(1964)
}
PHP Multidimensional Arrays
We know that arrays are a single list of key/value pairs.
However, sometimes you want to store values with more than one key. For
this, we have multidimensional arrays.
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
<?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>";
?>
Volvo: In stock: 22, sold: 18.
BMW: In stock: 15, sold: 13.
Saab: In stock: 5, sold: 2.
Land Rover: In stock: 17, sold: 15.
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):
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>";
<?php
// PHP program to creating three
// dimensional array
// Create three nested array
$myarray = array(
array(
array(1, 2),
array(3, 4),
),
array(
array(5, 6),
array(7, 8),
),
);
// Display the array information
print_r($myarray);
?>
Definition and Usage
The implode() function returns a string from the elements of an array.
Note: The implode() function accept its parameters in either order. However, for
consistency with explode(), you should use the documented order of arguments.
Note: The separator parameter of implode() is optional. However, it is
recommended to always use two parameters for backwards compatibility.
Note: This function is binary-safe.
Syntax
implode(separator,array)
<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>
O/P
Hello World! Beautiful Day!
Parameter Values
Parameter Description
separator Optional. Specifies what to put between the array elements.
Default is "" (an empty string)
array Required. The array to join to a string
<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr)."<br>";
echo implode("+",$arr)."<br>";
echo implode("-",$arr)."<br>";
echo implode("X",$arr);
?>
PHP explode() Function
The explode() function is an inbuilt function in PHP used to split a string into
different strings.
The explode() function splits a string based on a string delimiter, i.e. this
function returns an array containing the strings formed by splitting the
original string.
Syntax
explode(separator,string,limit)
Parameter Values
Parameter Description
separator Required. Specifies where to break the string
string Required. The string to split
limit Optional. Specifies the number of array elements to return.
Possible values:
Greater than 0 - Returns an array with a maximum
of limit element(s)
Less than 0 - Returns an array except for the last -
limit elements()
0 - Returns an array with one element
<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>
O/P
Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day. )
<?php
$str = 'one,two,three,four';
// zero limit
print_r(explode(',',$str,0));
// positive limit
print_r(explode(',',$str,2));
// negative limit
print_r(explode(',',$str,-1));
?>
O/P
Array ( [0] => one,two,three,four )
Array ( [0] => one [1] => two,three,four )
Array ( [0] => one [1] => two [2] => three )
PHP array_flip() Function
Definition and Usage
The array_flip() function flips/exchanges all keys with their associated values in
an array.
Syntax
array_flip(array)
Example
Flip all keys with their associated values in an array:
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$result=array_flip($a1);
print_r($result);
?>
O/P
Array ( [red] => a [green] => b [blue] => c [yellow] => d )
Create a Function
A user-defined function declaration starts with the keyword function, followed by
the name of the function:
Call a Function
To call the function, just write its name followed by parentheses ():
Example
function myMessage() {
echo "Hello world!";
myMessage();
PHP Function Arguments
function familyName($fname) {
echo "$fname Refsnes.<br>";
familyName("Jani");
familyName("Hege");
function familyName($fname, $year) {
echo "$fname Refsnes. Born in $year <br>";
familyName("Hege", "1975");
familyName("Stale", "1978");
PHP Default Argument Value
The following example shows how to use a default parameter. If we call the
function setHeight() without arguments it takes the default value as argument:
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
The height is : 350
The height is : 50
The height is : 135
The height is : 80
PHP Functions - Returning values
To let a function return a value, use the return statement:
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);
5 + 10 = 15
7 + 13 = 20
2+4=6
Passing Arguments by Reference
In PHP, arguments are usually passed by value, which means that a copy of the
value is used in the function and the variable that was passed into the function
cannot be changed.
When a 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:
function add_five(&$value) {
$value += 5;
$num = 2;
add_five($num);
echo $num;
Variable Number of Arguments
By using the ... operator in front of the function parameter, the function
accepts an unknown number of arguments. This is also called a variadic
function.
The variadic function argument becomes an array.
function sumMyNumbers(...$x) {
$n = 0;
$len = count($x);
for($i = 0; $i < $len; $i++) {
$n += $x[$i];
return $n;
$a = sumMyNumbers(5, 2, 6, 2, 7, 7);
echo $a;
29
Anonymous Function in PHP
Anonymous Function, also known as closures, are functions in PHP that
do not have a specific name and can be defined inline wherever they are
needed. They are useful for situations where a small, one-time function is
required, such as callbacks for array functions, event handling, or arguments
to other functions.
Syntax:
$anonymousFunction = function($arg1, $arg2, ...) {
// Function body
};
Important Points
Anonymous functions are declared using the function keyword followed
by the list of parameters and the function body.
They can capture variables from the surrounding scope using
the use keyword.
Anonymous functions can be assigned to variables, passed as arguments
to other functions, or returned from other functions.
Usage
Inline Definition: Anonymous functions can be defined directly within the
code, eliminating the need for named function declarations.
Flexibility: They can be used as callbacks or event handlers where a
small, reusable function is required.
Closure Scope: Anonymous functions can access variables from the
enclosing scope using the use keyword, allowing for the encapsulation of
data.
Example:
// Define and use an anonymous function
$sum = function($a, $b) {
return $a + $b;
};
// Output: 5
echo $sum(2, 3);
Variable functions
if a variable name has parentheses appended to it, PHP will look for a function with
the same name as whatever the variable evaluates to, and will attempt to execute
it. Among other things, this can be used to implement callbacks, function tables,
and so forth.
String functions
The str_word_count() function counts the number of words in a string.
Syntax
str_word_count(string,return,char)
Count the number of words found in the string "Hello World!":
<?php
echo str_word_count("Hello world!");
?>
PHP strlen() Function
The strlen() function returns the length of a string.
Syntax
strlen(string)
<?php
echo strlen("Hello");
?>
PHP strrev() Function
Definition and Usage
The strrev() function reverses a string.
Syntax
strrev(string)
<?php
echo strrev("Hello World!");
?>
!dlroW olleH
PHP strrpos() Function
The strrpos() function finds the position of the last occurrence of a string inside
another string.
Note: The strrpos() function is case-sensitive.
Related functions:
strpos() - Finds the position of the first occurrence of a string inside
another string (case-sensitive)
stripos() - Finds the position of the first occurrence of a string inside
another string (case-insensitive)
strripos() - Finds the position of the last occurrence of a string inside
another string (case-insensitive)
Syntax
strrpos(string,find,start)
Parameter Description
string Required. Specifies the string to search
find Required. Specifies the string to find
start Optional. Specifies where to begin the search
Find the position of the last occurrence of "php" inside the string:
<?php
echo strrpos("I love php, I love php too!","php");
?>
19
PHP str_replace() Function
Syntax
str_replace(find,replace,string,count)
Parameter Values
Parameter Description
find Required. Specifies the value to find
replace Required. Specifies the value to replace the value in find
string Required. Specifies the string to be searched
count Optional. A variable that counts the number of replacements
Replace the characters "world" in the string "Hello world!" with "Peter":
<?php
echo str_replace("world","Peter","Hello world!");
?>
Hello Peter!
PHP ucwords() Function
The ucwords() function converts the first character of each word in a string to
uppercase.
Note: This function is binary-safe.
Related functions:
ucfirst() - converts the first character of a string to uppercase
lcfirst() - converts the first character of a string to lowercase
strtoupper() - converts a string to uppercase
strtolower() - converts a string to lowercase
Syntax
ucwords(string, delimiters)
Parameter Values
Parameter Description
string Required. Specifies the string to convert
delimiters Optional. Specifies the word separator character
Convert the first character of each word to uppercase:
<?php
echo ucwords("hello world");
?>
Hello World
<?php
echo ucwords("hello|world", "|");
?>
Hello|World
PHP strcmp() Function
Compare two strings (case-sensitive):
<?php
echo strcmp("Hello world!","Hello world!");
?>
0
If this function returns 0, the two strings are equal.
This function returns:
0 - if the two strings are equal
<0 - if string1 is less than string2
>0 - if string1 is greater than string2
PHP | imagecreate() Function
The imagecreate() function is an inbuilt function in PHP which is used to
create a new image. This function returns the blank image of given size. In
general imagecreatetruecolor() function is used instead of imagecreate()
function because imagecreatetruecolor() function creates high quality
images.
imagecreate( $width, $height )
Parameters: This function accepts two parameters as mentioned above and
described below:
$width: It is mandatory parameter which is used to specify the image
width.
$height: It is mandatory parameter which is used to specify the image
height.
Return Value: This function returns an image resource identifier on success,
FALSE on errors.
<?php
// Create the size of image or blank image
$image = imagecreate(500, 300);
// Set the background color of image
$background_color = imagecolorallocate($image, 0, 153, 0);
// Set the text color of image
$text_color = imagecolorallocate($image, 255, 255, 255);
Syntax:
int imagecolorallocate ( $image, $red, $green, $blue )
// Function to create image which contains string.
imagestring($image, 5, 180, 100, "GeeksforGeeks", $text_color);
imagestring($image, 3, 160, 120, "A computer science portal", $text_color);
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
?>
PHP | imagefilledrectangle() Function
The imagefilledrectangle() function is an inbuilt function in PHP
which is used to create a filled rectangle. This function creates a
rectangle filled with a given color in the image. The top left corner of
the image is (0, 0).
Syntax:
bool imagefilledrectangle( $image, $x1, $y1, $x2, $y2, $color )
Parameters: This function accepts six parameters as mentioned above and
described below:
$image: It is returned by one of the image creation functions, such as
imagecreatetruecolor(). It is used to create size of image.
$x1: This parameter is used to set the x-coordinate for point 1.
$y1: This parameter is used to set the y-coordinate for point 1.
$x2: This parameter is used to set the x-coordinate for point 2.
$y2: This parameter is used to set the y-coordinate for point 2.
$color: This parameter contains the filled color identifier. A color identifier
created with imagecolorallocate() function.
Return Value: This function returns True on success or False on failure.
<?php
// Create an image of given size
$image = imagecreatetruecolor(500, 300);
$green = imagecolorallocate($image, 0, 153, 0);
// Draw the rectangle of green color
imagefilledrectangle($image, 20, 20, 480, 280, $green);
// Output image in png format
header("Content-type: image/png");
imagepng($image);
// Free memory
imagedestroy($image);
?>
How to generate PDF file using PHP
The FPDF class includes many features like page formats, page headers,
footers, automatic page break, line break, image support, colors, links, and
many more.
You need to download the FPDF class from the FPDF website and
include it in your PHP script.
require('fpdf/fpdf.php');
Instantiate and use the FPDF class according to your need as shown in
the following examples.
$pdf=new FPDF();
<?php
ob_end_clean();
require('fpdf/fpdf.php');
// Instantiate and use the FPDF class
$pdf = new FPDF();
//Add a new page
$pdf->AddPage();
// Set the font for the text
$pdf->SetFont('Arial', 'B', 18);
// Prints a cell with given text
$pdf->Cell(60,20,'Hello GeeksforGeeks!');
// return the generated output
$pdf->Output();
?>