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

What's hot

Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database Programming
Ahmed Swilam
 
PHP - Introduction to PHP
PHP -  Introduction to PHPPHP -  Introduction to PHP
PHP - Introduction to PHP
Vibrant Technologies & Computers
 
Php Basics
Php BasicsPhp Basics
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
Ahmed Swilam
 
Php mysql
Php mysqlPhp mysql
Php mysql
Manish Jain
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
Muthuselvam RS
 
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
tumetr1
 
Introduction in php
Introduction in phpIntroduction in php
Introduction in php
Bozhidar Boshnakov
 
Practical approach to perl day1
Practical approach to perl day1Practical approach to perl day1
Practical approach to perl day1
Rakesh Mukundan
 
Php pattern matching
Php pattern matchingPhp pattern matching
Php pattern matching
JIGAR MAKHIJA
 
Format String
Format StringFormat String
Format String
Wei-Bo Chen
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
Bozhidar Boshnakov
 
Apache Velocity 1.6
Apache Velocity 1.6Apache Velocity 1.6
Apache Velocity 1.6
Henning Schmiedehausen
 
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
Ayesh Karunaratne
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
Dave Cross
 
Perl_Tutorial_v1
Perl_Tutorial_v1Perl_Tutorial_v1
Perl_Tutorial_v1
tutorialsruby
 
Perl bhargav
Perl bhargavPerl bhargav
Perl bhargav
Bhargav Reddy
 
Migrating to Puppet 4.0
Migrating to Puppet 4.0Migrating to Puppet 4.0
Migrating to Puppet 4.0
Puppet
 
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)
Bozhidar Boshnakov
 
Php Lecture Notes
Php Lecture NotesPhp Lecture Notes
Php Lecture Notes
Santhiya Grace
 

What's hot (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 to Web Application Development using PHP Chapter 3

Php basics
Php basicsPhp basics
Php basics
Jamshid Hashimi
 
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
Mohammed Mushtaq Ahmed
 
Php(report)
Php(report)Php(report)
Php(report)
Yhannah
 
PHP Basics and Demo HackU
PHP Basics and Demo HackUPHP Basics and Demo HackU
PHP Basics and Demo HackU
Anshu Prateek
 
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
AAFREEN SHAIKH
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
BUDNET
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
Vibrant Technologies & Computers
 
UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
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 and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
Tiji Thomas
 
Materi Dasar PHP
Materi Dasar PHPMateri Dasar PHP
Materi Dasar PHP
Robby Firmansyah
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
Gheyath M. Othman
 
Php
PhpPhp
05php
05php05php
05php
05php05php
05php
05php05php
php fundamental
php fundamentalphp fundamental
php fundamental
zalatarunk
 
php
phpphp
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 to 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
phpphp
php
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of php
 

More from Mohd Harris Ahmad Jaal

Fundamentals of Programming Chapter 7
Fundamentals of Programming Chapter 7Fundamentals of Programming Chapter 7
Fundamentals of Programming Chapter 7
Mohd Harris Ahmad Jaal
 
Fundamentals of Programming Chapter 6
Fundamentals of Programming Chapter 6Fundamentals of Programming Chapter 6
Fundamentals of Programming Chapter 6
Mohd Harris Ahmad Jaal
 
Fundamentals of Programming Chapter 4
Fundamentals of Programming Chapter 4Fundamentals of Programming Chapter 4
Fundamentals of Programming Chapter 4
Mohd Harris Ahmad Jaal
 
Fundamentals of Programming Chapter 3
Fundamentals of Programming Chapter 3Fundamentals of Programming Chapter 3
Fundamentals of Programming Chapter 3
Mohd Harris Ahmad Jaal
 
Fundamentals of Programming Chapter 2
Fundamentals of Programming Chapter 2Fundamentals of Programming Chapter 2
Fundamentals of Programming Chapter 2
Mohd Harris Ahmad Jaal
 
Fundamentals of Programming Chapter 1
Fundamentals of Programming Chapter 1Fundamentals of Programming Chapter 1
Fundamentals of Programming Chapter 1
Mohd Harris Ahmad Jaal
 
Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5
Mohd Harris Ahmad Jaal
 
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
Mohd Harris Ahmad Jaal
 
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
Mohd Harris Ahmad Jaal
 
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
Mohd Harris Ahmad Jaal
 
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
Mohd Harris Ahmad Jaal
 
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
Mohd Harris Ahmad Jaal
 
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
Mohd Harris Ahmad Jaal
 
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
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 10
Fundamentals of Computing Chapter 10Fundamentals of Computing Chapter 10
Fundamentals of Computing Chapter 10
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 9
Fundamentals of Computing Chapter 9Fundamentals of Computing Chapter 9
Fundamentals of Computing Chapter 9
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 8
Fundamentals of Computing Chapter 8Fundamentals of Computing Chapter 8
Fundamentals of Computing Chapter 8
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 7
Fundamentals of Computing Chapter 7Fundamentals of Computing Chapter 7
Fundamentals of Computing Chapter 7
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 6
Fundamentals of Computing Chapter 6Fundamentals of Computing Chapter 6
Fundamentals of Computing Chapter 6
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 5
Fundamentals of Computing Chapter 5Fundamentals of Computing Chapter 5
Fundamentals of Computing Chapter 5
Mohd Harris Ahmad Jaal
 

More from 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
 

Recently uploaded

World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 

Recently uploaded (20)

World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 

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)