SlideShare una empresa de Scribd logo
1 de 26
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)

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database Programming
 
PHP - Introduction to PHP
PHP -  Introduction to PHPPHP -  Introduction to PHP
PHP - Introduction to PHP
 
Php Basics
Php BasicsPhp Basics
Php Basics
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
php 2 Function creating, calling, PHP built-in function
php 2 Function creating, calling,PHP built-in functionphp 2 Function creating, calling,PHP built-in function
php 2 Function creating, calling, PHP built-in function
 
Introduction in php
Introduction in phpIntroduction in php
Introduction in php
 
Practical approach to perl day1
Practical approach to perl day1Practical approach to perl day1
Practical approach to perl day1
 
Php pattern matching
Php pattern matchingPhp pattern matching
Php pattern matching
 
Format String
Format StringFormat String
Format String
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
 
Apache Velocity 1.6
Apache Velocity 1.6Apache Velocity 1.6
Apache Velocity 1.6
 
PHP 8.1 - What's new and changed
PHP 8.1 - What's new and changedPHP 8.1 - What's new and changed
PHP 8.1 - What's new and changed
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Perl_Tutorial_v1
Perl_Tutorial_v1Perl_Tutorial_v1
Perl_Tutorial_v1
 
Perl bhargav
Perl bhargavPerl bhargav
Perl bhargav
 
Migrating to Puppet 4.0
Migrating to Puppet 4.0Migrating to Puppet 4.0
Migrating to Puppet 4.0
 
Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)
 
Php Lecture Notes
Php Lecture NotesPhp Lecture Notes
Php Lecture Notes
 

Similar a Web Application Development using PHP Chapter 3

Php(report)
Php(report)Php(report)
Php(report)
Yhannah
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
BUDNET
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeString handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of php
pooja bhandari
 

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

Php basics
Php basicsPhp basics
Php basics
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginnersPHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
 
Php(report)
Php(report)Php(report)
Php(report)
 
PHP Basics and Demo HackU
PHP Basics and Demo HackUPHP Basics and Demo HackU
PHP Basics and Demo HackU
 
Hsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdfHsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdf
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
 
UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
 
UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeString handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Materi Dasar PHP
Materi Dasar PHPMateri Dasar PHP
Materi Dasar PHP
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
 
Php
PhpPhp
Php
 
05php
05php05php
05php
 
05php
05php05php
05php
 
05php
05php05php
05php
 
php fundamental
php fundamentalphp fundamental
php fundamental
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of php
 
php
phpphp
php
 

Más de Mohd Harris Ahmad Jaal

Más de Mohd Harris Ahmad Jaal (20)

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

Último

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Último (20)

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 

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)