SlideShare a Scribd company logo
CN5109
WEB APPLICATION
DEVELOPMENT
Chapter 3
PHP Functions
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;
}
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.
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()
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
?>
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");
?>
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!");
?>
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>";
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()
Numeric Functions
• The PHP number_format() function used to formats a numeric value
using digit separators and decimal points.
<?php
echo number_format(2509663);
?>
Numeric Functions
• The PHP rand() function used to generate a random number.
<?php
echo rand(0,10);
?>
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);
?>
Numeric Functions
• The PHP sqrt() function used to returns the square root of a number
<?php
echo sqrt(100);
?>
Numeric Functions
• The PHP pi() function used to returns the value of PI
<?php
echo pi();
?>
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");
?>
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");
?>
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.
PHP User Define Functions
• Example:
<?php
function writeMsg() {
echo "Hello world!";
}
writeMsg(); // call the function
?>
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.
PHP Function Arguments
• Example:
<?php
function familyName($fname) {
echo "$fname Johnson.<br>";
}
familyName("Jane");
familyName(“Mary");
familyName(“Kim");
familyName(“Allan");
familyName(“David");
?>
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");
?>
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);
?>
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);
?>
Lab Practical
a. Hello Function
<?php
function hello()
{
echo "Hello, World!";
}
hello();
?>
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);
?>
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)

More Related Content

PPT
Class 2 - Introduction to PHP
PDF
Php Tutorials for Beginners
PPT
Groovy unleashed
PDF
DIG1108C Lesson3 Fall 2014
KEY
Using PHP
PPTX
PHP for hacks
PPTX
Php Intermediate
PPTX
Phphacku iitd
Class 2 - Introduction to PHP
Php Tutorials for Beginners
Groovy unleashed
DIG1108C Lesson3 Fall 2014
Using PHP
PHP for hacks
Php Intermediate
Phphacku iitd

What's hot (20)

PPTX
Class 8 - Database Programming
PPT
PHP - Introduction to PHP
PPT
Class 3 - PHP Functions
PPT
Php mysql
ODP
PHP Web Programming
PPT
php 2 Function creating, calling, PHP built-in function
PPTX
Introduction in php
PDF
Practical approach to perl day1
PPTX
Php pattern matching
PPTX
Format String
PPTX
Introduction in php part 2
KEY
Apache Velocity 1.6
PDF
PHP 8.1 - What's new and changed
ODP
Advanced Perl Techniques
PDF
Perl_Tutorial_v1
PPTX
Perl bhargav
PPTX
Migrating to Puppet 4.0
PPTX
Object-Oriented Programming with PHP (part 1)
PPT
Php Lecture Notes
Class 8 - Database Programming
PHP - Introduction to PHP
Class 3 - PHP Functions
Php mysql
PHP Web Programming
php 2 Function creating, calling, PHP built-in function
Introduction in php
Practical approach to perl day1
Php pattern matching
Format String
Introduction in php part 2
Apache Velocity 1.6
PHP 8.1 - What's new and changed
Advanced Perl Techniques
Perl_Tutorial_v1
Perl bhargav
Migrating to Puppet 4.0
Object-Oriented Programming with PHP (part 1)
Php Lecture Notes
Ad

Similar to Web Application Development using PHP Chapter 3 (20)

PPTX
function in php using like three type of function
PPTX
function in php like control loop and its uses
PPTX
Php basics
PPTX
Introduction To PHP000000000000000000000000000000.pptx
PPTX
Lecture 9 - Intruduction to BOOTSTRAP.pptx
PPT
PHP complete reference with database concepts for beginners
PPT
Php(report)
PPTX
PHP Basics and Demo HackU
PDF
Hsc IT 5. Server-Side Scripting (PHP).pdf
PPT
MIND sweeping introduction to PHP
PPT
Php classes in mumbai
PPTX
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
PPTX
UNIT II (7).pptx
PPTX
UNIT II (7).pptx
PPSX
Php and MySQL
PDF
Materi Dasar PHP
PDF
Web Development Course: PHP lecture 1
PPTX
PPTX
Introduction to PHP_ Lexical structure_Array_Function_String
PPT
function in php using like three type of function
function in php like control loop and its uses
Php basics
Introduction To PHP000000000000000000000000000000.pptx
Lecture 9 - Intruduction to BOOTSTRAP.pptx
PHP complete reference with database concepts for beginners
Php(report)
PHP Basics and Demo HackU
Hsc IT 5. Server-Side Scripting (PHP).pdf
MIND sweeping introduction to PHP
Php classes in mumbai
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
UNIT II (7).pptx
UNIT II (7).pptx
Php and MySQL
Materi Dasar PHP
Web Development Course: PHP lecture 1
Introduction to PHP_ Lexical structure_Array_Function_String
Ad

More from Mohd Harris Ahmad Jaal (20)

PPT
Fundamentals of Programming Chapter 7
PPT
Fundamentals of Programming Chapter 6
PPT
Fundamentals of Programming Chapter 4
PPT
Fundamentals of Programming Chapter 3
PPT
Fundamentals of Programming Chapter 2
PPT
Fundamentals of Programming Chapter 1
PPT
Fundamentals of Programming Chapter 5
PPTX
Web Application Development using PHP Chapter 8
PPTX
Web Application Development using PHP Chapter 7
PPTX
Web Application Development using PHP Chapter 6
PPTX
Web Application Development using PHP Chapter 5
PPTX
Web Application Development using PHP Chapter 4
PPTX
Web Application Development using PHP Chapter 2
PPTX
Web Application Development using PHP Chapter 1
PPT
Fundamentals of Computing Chapter 10
PPT
Fundamentals of Computing Chapter 9
PPT
Fundamentals of Computing Chapter 8
PPT
Fundamentals of Computing Chapter 7
PPT
Fundamentals of Computing Chapter 6
PPT
Fundamentals of Computing Chapter 5
Fundamentals of Programming Chapter 7
Fundamentals of Programming Chapter 6
Fundamentals of Programming Chapter 4
Fundamentals of Programming Chapter 3
Fundamentals of Programming Chapter 2
Fundamentals of Programming Chapter 1
Fundamentals of Programming Chapter 5
Web Application Development using PHP Chapter 8
Web Application Development using PHP Chapter 7
Web Application Development using PHP Chapter 6
Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 1
Fundamentals of Computing Chapter 10
Fundamentals of Computing Chapter 9
Fundamentals of Computing Chapter 8
Fundamentals of Computing Chapter 7
Fundamentals of Computing Chapter 6
Fundamentals of Computing Chapter 5

Recently uploaded (20)

PDF
Insiders guide to clinical Medicine.pdf
PDF
Business Ethics Teaching Materials for college
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Classroom Observation Tools for Teachers
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Institutional Correction lecture only . . .
PDF
Basic Mud Logging Guide for educational purpose
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Cell Structure & Organelles in detailed.
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Insiders guide to clinical Medicine.pdf
Business Ethics Teaching Materials for college
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Supply Chain Operations Speaking Notes -ICLT Program
Classroom Observation Tools for Teachers
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Institutional Correction lecture only . . .
Basic Mud Logging Guide for educational purpose
102 student loan defaulters named and shamed – Is someone you know on the list?
Cell Structure & Organelles in detailed.
Renaissance Architecture: A Journey from Faith to Humanism
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Cell Types and Its function , kingdom of life
FourierSeries-QuestionsWithAnswers(Part-A).pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Microbial diseases, their pathogenesis and prophylaxis
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Pre independence Education in Inndia.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf

Web Application Development using PHP Chapter 3

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