2. PHP Functions
• PHP functions are similar to other programming languages.
• A function is a piece of code which takes one more input in
the form of parameter and does some processing and returns
a value.
• Note: A function name can start with a letter or underscore
(not a number).
• Tip: Give the function a name that reflects what the function
does!
Syntax:
function functionName() {
code to be executed;
}
3. Advantages of using Functions
• Better code organization – functions allow us to group blocks
of related code that perform a specific task together.
• Reusability – once defined, a function can be called by a
number of scripts in our PHP files. This saves us time of
reinventing the wheel when we want to perform some routine
tasks such as connecting to the database
• Easy maintenance- updates to the system only need to be
made in one place.
4. Strings Functions
• A string is a sequence of characters, like "Hello world!".
• PHP string functions are used to manipulate string values.
• Examples of String Functions:
• strlen()
• str_word_count()
• strrev()
• strpos()
• str_replace()
5. Strings Functions
• The PHP strlen() function returns the length of a string.
• The PHP str_word_count() function counts the number of
words in a string.
<?php
echo strlen("Hello world!");
?>
<?php
echo str_word_count("Hello world!"); // outputs 2
?>
6. Strings Functions
• The PHP strrev() function reverses a string.
• The PHP strpos() function searches for a specific text within a
string.
• If a match is found, the function returns the character position
of the first match. If no match is found, it will return FALSE.
<?php
echo strrev("Hello world!");
?>
<?php
echo strpos("Hello world!", "world");
?>
7. Strings Functions
• The PHP str_replace() function replaces some characters with
some other characters in a string.
<?php
echo str_replace("world", "Dolly", "Hello world!");
?>
8. Quiz
• Using all string functions, write the PHP code with the text
“FTMS COLLEGE”.
$x = "FTMS College";
$y = "College";
$z = "<u>Kolej</u>";
echo "String Length: ", strlen($x), "<br>";
echo "String Word Count: ", str_word_count($x), "<br>";
echo "String Reverse: ", strrev($x), "<br>";
echo "String Position: ", strpos($x, $y), "<br>";
echo "String Replace: ", str_replace($y, $z, $x), "<br>";
9. Numeric Functions
• Numeric functions are function that return numeric results.
• Numeric php function can be used to format numbers, return
constants, perform mathematical computations etc.
• Examples of Numeric Functions:
• number_format()
• rand()
• round()
• sqrt()
• pi()
10. Numeric Functions
• The PHP number_format() function used to formats a numeric value
using digit separators and decimal points.
<?php
echo number_format(2509663);
?>
11. Numeric Functions
• The PHP rand() function used to generate a random number.
<?php
echo rand(0,10);
?>
12. Numeric Functions
• The PHP round() function used to round off a number with decimal
points to the nearest whole number.
<?php
echo round(3.49);
?>
13. Numeric Functions
• The PHP sqrt() function used to returns the square root of a number
<?php
echo sqrt(100);
?>
14. Numeric Functions
• The PHP pi() function used to returns the value of PI
<?php
echo pi();
?>
15. Date and Time Functions
• The required format parameter of the date() function specifies
how to format the date (or time).
• Here are some characters that are commonly used for dates:
• d - Represents the day of the month (01 to 31)
• m - Represents a month (01 to 12)
• y - Represents a year
• l (lowercase 'L') - Represents the day of the week
<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l");
?>
16. Date and Time Functions
• Here are some characters that are commonly used for times:
• h - 12-hour format of an hour with leading zeros (01 to 12)
• i - Minutes with leading zeros (00 to 59)
• s - Seconds with leading zeros (00 to 59)
• a - Lowercase Ante meridiem and Post meridiem (am or pm)
<?php
echo "The time is " . date("h:i:sa");
?>
17. PHP User Define Functions
• We can create our own functions.
• A function is a block of statements that can be used
repeatedly in a program.
• A function will not execute immediately when a page loads.
• A function will be executed by a call to the function.
18. PHP User Define Functions
• Example:
<?php
function writeMsg() {
echo "Hello world!";
}
writeMsg(); // call the function
?>
19. 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.
20. PHP Function Arguments
• Example:
<?php
function familyName($fname) {
echo "$fname Johnson.<br>";
}
familyName("Jane");
familyName(“Mary");
familyName(“Kim");
familyName(“Allan");
familyName(“David");
?>
21. PHP Function Arguments
• Example:
<?php
function familyName($fname, $year) {
echo "$fname Johnson. Born in $year <br>";
}
familyName(“James", "1975");
familyName(“Mary", "1978");
familyName(“Sarah", "1983");
?>
22. PHP Function Arguments
• 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:
<?php
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>
23. PHP Function Returning Value
• A function can return a value using the return statement in
conjunction with a value or object.
• Return stops the execution of the function and sends the
value back to the calling code.
<?php
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);
?>
24. Lab Practical
a. Hello Function
<?php
function hello()
{
echo "Hello, World!";
}
hello();
?>
25. Lab Practical
b. Calculate Rectangle Area Function
<?php
function recArea($l, $w)
{
$area = $l * $w;
echo "A rectangle of length $l and width $w has an area of $area.";
return $area;
}
recArea(2, 4);
?>
26. Quiz
Write the PHP Code using function for the following program
a. Calculate Total Marks (Total Marks = CW + Exam)
b. Display student grade (Mark > 39 = Pass, Else = Fail)