SlideShare a Scribd company logo
1 of 18
PHP
Function creating, calling,
PHP built-in function
Function
• Sub routine.
• A function is a block of code that you can call
throughout your PHP script. Functions are
often used for common functionalities that
can occur in your PHP program. For an
example think that you write a program that
relates to seasonal offers. In many occasions,
you would need to know the final price after a
discount was given. You can define a function
for this as below.
Creating function
function functionName($param1, $param2)
{
code to be executed;
code to be executed;
return anyValue;
}
Optionally, datas to be
passed into function. It
can be unlimited
number of datas.
Optionally return
value back to the
one who call the
function.
Creating function- Example
function print_hello()
{
print “Hello”;
}
A function that does not require any external
datas to process. It done the job by just print
word “Hello” to the screen. It return nothing
to the place it is being called.
Creating function- Example
function transfer($account_a, $account_b, $amount)
{
$account_a = $account_a - $amount;
$account_b = $account_b + $amount;
return $account_b;
}
• A function that takes 3 external datas (parameters)
to process. The function subtract $amount from
$account_a then $amount add to $account_b. Finally
return the result to the place it is being called.
Calling function
/*** Functions declaration ***/
function print_hello()
{
print “Hello”;
}
function transfer($account_a, $account_b, $amount)
{
$account_a = $account_a - $amount;
$account_b = $account_b + $amount;
return $account_b;
}
/*** Using functions we just declare above here ***/
print_hello();
print transfer(500, 200, 50);
PHP Built-in Functions
in common use
Useful PHP Built-in functions
• (Q) What is PHP built-in functions?
• (A) Instant, Ready to use functions provided
by PHP producer. No need to create by
ourself.
phpinfo()
Benefit: Print PHP information, configuration,
settings out to screen
Example:
phpinfo();
Function for String :: trim()
Benefit: Remove whitespace from both ends of
string
Example:
$str = ‘ Hello There ’;
$new_str = trim($str);
print $new_str;
Result:
Hello There
Function for String :: explode()
Benefit: Explode string into array
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
print $pieces[0]; // piece1
print $pieces[1]; // piece2
Function for String :: implode()
Benefit: Implode array into string
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
print $comma_separated; // lastname,email,phone
Function for String :: strlen()
Benefit: Get string’s length (How many characters inside
the string)
$str = 'abcdef';
print strlen($str); // 6
$str = ' ab cd ';
print strlen($str); // 7
Function for String :: strstr()
Benefit: Find the first occurrence in the string
case-sensitive stristr() will be used for incase-
sensitive.
$email = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com
Function for Array :: count()
Benefit: Count all elements in array
$a = array();
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a);
print $result; // output 3
$food = array('fruits' => array('orange', 'banana', 'apple'),
'veggie' => array('carrot', 'collard', 'pea'));
// recursive count
print count($food, COUNT_RECURSIVE); // output 8
// normal count
print count($food); // output 2
Function for Array :: array_rand()
Benefit: Pick one or more random entries out of an array
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
print $rand_keys;
Function for Array :: print_r()
Benefit: Print structure of array.
Note that we should wrap it with
<pre></pre> for readability.
$a = array ('a' => 'apple',
'b' => 'banana',
'c' => array ('x', 'y', 'z'));
print “<pre>”;
print_r($a);
print “<pre>”;
/****
<pre>
Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
)
)
</pre>
****/

More Related Content

What's hot

Php pattern matching
Php pattern matchingPhp pattern matching
Php pattern matchingJIGAR MAKHIJA
 
Php server variables
Php server variablesPhp server variables
Php server variablesJIGAR MAKHIJA
 
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 Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021Ayesh Karunaratne
 
Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database ProgrammingAhmed Swilam
 
02 Php Vars Op Control Etc
02 Php Vars Op Control Etc02 Php Vars Op Control Etc
02 Php Vars Op Control EtcGeshan Manandhar
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requireTheCreativedev Blog
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisIan Macali
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?Nikita Popov
 

What's hot (20)

Functions in php
Functions in phpFunctions in php
Functions in php
 
Introduction in php
Introduction in phpIntroduction in php
Introduction in php
 
Php pattern matching
Php pattern matchingPhp pattern matching
Php pattern matching
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
 
Php server variables
Php server variablesPhp server variables
Php server variables
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Data Types In PHP
Data Types In PHPData Types In PHP
Data Types In PHP
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
Php functions
Php functionsPhp functions
Php functions
 
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)
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
 
Php string function
Php string function Php string function
Php string function
 
PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database Programming
 
02 Php Vars Op Control Etc
02 Php Vars Op Control Etc02 Php Vars Op Control Etc
02 Php Vars Op Control Etc
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
 

Viewers also liked

4 - statement
4  - statement4  - statement
4 - statementskiats
 
การใช้งาน phpMyadmin
การใช้งาน phpMyadminการใช้งาน phpMyadmin
การใช้งาน phpMyadminskiats
 
PHPBarcelona Conference - Optimización aplicaciones PHP - Client side
PHPBarcelona Conference - Optimización aplicaciones PHP - Client sidePHPBarcelona Conference - Optimización aplicaciones PHP - Client side
PHPBarcelona Conference - Optimización aplicaciones PHP - Client sidemaguilar
 
Servidor Web Apache, PHP, MySQL.
Servidor Web Apache, PHP, MySQL.Servidor Web Apache, PHP, MySQL.
Servidor Web Apache, PHP, MySQL.Ángel Acaymo M. G.
 

Viewers also liked (8)

php
phpphp
php
 
PHP Tutorial (array)
PHP Tutorial (array)PHP Tutorial (array)
PHP Tutorial (array)
 
4 - statement
4  - statement4  - statement
4 - statement
 
PHP Tutorial (funtion)
PHP Tutorial (funtion)PHP Tutorial (funtion)
PHP Tutorial (funtion)
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
การใช้งาน phpMyadmin
การใช้งาน phpMyadminการใช้งาน phpMyadmin
การใช้งาน phpMyadmin
 
PHPBarcelona Conference - Optimización aplicaciones PHP - Client side
PHPBarcelona Conference - Optimización aplicaciones PHP - Client sidePHPBarcelona Conference - Optimización aplicaciones PHP - Client side
PHPBarcelona Conference - Optimización aplicaciones PHP - Client side
 
Servidor Web Apache, PHP, MySQL.
Servidor Web Apache, PHP, MySQL.Servidor Web Apache, PHP, MySQL.
Servidor Web Apache, PHP, MySQL.
 

Similar to php 2 Function creating, calling, PHP built-in function

Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functionsmussawir20
 
Web app development_php_06
Web app development_php_06Web app development_php_06
Web app development_php_06Hassen Poreya
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
Ch1(introduction to php)
Ch1(introduction to php)Ch1(introduction to php)
Ch1(introduction to php)Chhom Karath
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxChandrakantDivate1
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest UpdatesIftekhar Eather
 
Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5Stephan Schmidt
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboardsDenis Ristic
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7Rumman Ansari
 
PHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return TypesPHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return TypesEric Poe
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxamrit47
 
PHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptxPHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptxShaliniPrabakaran
 

Similar to php 2 Function creating, calling, PHP built-in function (20)

Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
 
Web app development_php_06
Web app development_php_06Web app development_php_06
Web app development_php_06
 
lab4_php
lab4_phplab4_php
lab4_php
 
lab4_php
lab4_phplab4_php
lab4_php
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Ch1(introduction to php)
Ch1(introduction to php)Ch1(introduction to php)
Ch1(introduction to php)
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 
Python basic
Python basicPython basic
Python basic
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
 
Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5
 
Function in c program
Function in c programFunction in c program
Function in c program
 
07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 
Php
PhpPhp
Php
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 
PHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return TypesPHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return Types
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
PHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptxPHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptx
 

More from tumetr1

ตัวอย่างประวัติผู้วิจัย เล่มโปรเจ็ค
ตัวอย่างประวัติผู้วิจัย เล่มโปรเจ็คตัวอย่างประวัติผู้วิจัย เล่มโปรเจ็ค
ตัวอย่างประวัติผู้วิจัย เล่มโปรเจ็คtumetr1
 
ตัวอย่างภาคผนวก เล่มโปรเจ็ค
ตัวอย่างภาคผนวก เล่มโปรเจ็คตัวอย่างภาคผนวก เล่มโปรเจ็ค
ตัวอย่างภาคผนวก เล่มโปรเจ็คtumetr1
 
ตัวอย่างบรรณานุกรม เล่มโปรเจ็ค
ตัวอย่างบรรณานุกรม เล่มโปรเจ็คตัวอย่างบรรณานุกรม เล่มโปรเจ็ค
ตัวอย่างบรรณานุกรม เล่มโปรเจ็คtumetr1
 
ตัวอย่างบทที่1 บทนำ เล่มโปรเจ็ค
ตัวอย่างบทที่1 บทนำ เล่มโปรเจ็คตัวอย่างบทที่1 บทนำ เล่มโปรเจ็ค
ตัวอย่างบทที่1 บทนำ เล่มโปรเจ็คtumetr1
 
ตัวอย่างสารบัญ เล่มโปรเจ็ค
ตัวอย่างสารบัญ เล่มโปรเจ็คตัวอย่างสารบัญ เล่มโปรเจ็ค
ตัวอย่างสารบัญ เล่มโปรเจ็คtumetr1
 
ตัวอย่างกิตติกรรมประกาศ เล่มโปรเจ็ค
ตัวอย่างกิตติกรรมประกาศ เล่มโปรเจ็คตัวอย่างกิตติกรรมประกาศ เล่มโปรเจ็ค
ตัวอย่างกิตติกรรมประกาศ เล่มโปรเจ็คtumetr1
 
ตัวอย่างบทคัดย่อเล่มโปรเจ็ค
ตัวอย่างบทคัดย่อเล่มโปรเจ็คตัวอย่างบทคัดย่อเล่มโปรเจ็ค
ตัวอย่างบทคัดย่อเล่มโปรเจ็คtumetr1
 
file transfer and access utilities
file transfer and access utilitiesfile transfer and access utilities
file transfer and access utilitiestumetr1
 
retrieving the mail
retrieving the mailretrieving the mail
retrieving the mailtumetr1
 
connectivity utility
connectivity utilityconnectivity utility
connectivity utilitytumetr1
 
network hardware
network hardwarenetwork hardware
network hardwaretumetr1
 
ระบบเครือข่ายไร้สาย (wireless lan)
ระบบเครือข่ายไร้สาย (wireless lan)ระบบเครือข่ายไร้สาย (wireless lan)
ระบบเครือข่ายไร้สาย (wireless lan)tumetr1
 
the transport layer
the transport layerthe transport layer
the transport layertumetr1
 
ระดับชั้นเน็ตเวิร์ก
ระดับชั้นเน็ตเวิร์กระดับชั้นเน็ตเวิร์ก
ระดับชั้นเน็ตเวิร์กtumetr1
 
ระดับชั้นดาต้าลิงค์
ระดับชั้นดาต้าลิงค์ระดับชั้นดาต้าลิงค์
ระดับชั้นดาต้าลิงค์tumetr1
 
สถาปัตยกรรมเครือข่ายคอมพิวเตอร์และบริการ
สถาปัตยกรรมเครือข่ายคอมพิวเตอร์และบริการสถาปัตยกรรมเครือข่ายคอมพิวเตอร์และบริการ
สถาปัตยกรรมเครือข่ายคอมพิวเตอร์และบริการtumetr1
 
การส่งข้อมูลผ่านสายส่งและเทคนิคการส่งข้อมูลผ่านเครือข่าย
การส่งข้อมูลผ่านสายส่งและเทคนิคการส่งข้อมูลผ่านเครือข่ายการส่งข้อมูลผ่านสายส่งและเทคนิคการส่งข้อมูลผ่านเครือข่าย
การส่งข้อมูลผ่านสายส่งและเทคนิคการส่งข้อมูลผ่านเครือข่ายtumetr1
 
ความรู้พื้นฐานของระบบการสื่อสารข้อมูล
ความรู้พื้นฐานของระบบการสื่อสารข้อมูลความรู้พื้นฐานของระบบการสื่อสารข้อมูล
ความรู้พื้นฐานของระบบการสื่อสารข้อมูลtumetr1
 
พัฒนาเศรษฐกิจ
พัฒนาเศรษฐกิจพัฒนาเศรษฐกิจ
พัฒนาเศรษฐกิจtumetr1
 

More from tumetr1 (20)

ตัวอย่างประวัติผู้วิจัย เล่มโปรเจ็ค
ตัวอย่างประวัติผู้วิจัย เล่มโปรเจ็คตัวอย่างประวัติผู้วิจัย เล่มโปรเจ็ค
ตัวอย่างประวัติผู้วิจัย เล่มโปรเจ็ค
 
ตัวอย่างภาคผนวก เล่มโปรเจ็ค
ตัวอย่างภาคผนวก เล่มโปรเจ็คตัวอย่างภาคผนวก เล่มโปรเจ็ค
ตัวอย่างภาคผนวก เล่มโปรเจ็ค
 
ตัวอย่างบรรณานุกรม เล่มโปรเจ็ค
ตัวอย่างบรรณานุกรม เล่มโปรเจ็คตัวอย่างบรรณานุกรม เล่มโปรเจ็ค
ตัวอย่างบรรณานุกรม เล่มโปรเจ็ค
 
ตัวอย่างบทที่1 บทนำ เล่มโปรเจ็ค
ตัวอย่างบทที่1 บทนำ เล่มโปรเจ็คตัวอย่างบทที่1 บทนำ เล่มโปรเจ็ค
ตัวอย่างบทที่1 บทนำ เล่มโปรเจ็ค
 
ตัวอย่างสารบัญ เล่มโปรเจ็ค
ตัวอย่างสารบัญ เล่มโปรเจ็คตัวอย่างสารบัญ เล่มโปรเจ็ค
ตัวอย่างสารบัญ เล่มโปรเจ็ค
 
ตัวอย่างกิตติกรรมประกาศ เล่มโปรเจ็ค
ตัวอย่างกิตติกรรมประกาศ เล่มโปรเจ็คตัวอย่างกิตติกรรมประกาศ เล่มโปรเจ็ค
ตัวอย่างกิตติกรรมประกาศ เล่มโปรเจ็ค
 
ตัวอย่างบทคัดย่อเล่มโปรเจ็ค
ตัวอย่างบทคัดย่อเล่มโปรเจ็คตัวอย่างบทคัดย่อเล่มโปรเจ็ค
ตัวอย่างบทคัดย่อเล่มโปรเจ็ค
 
file transfer and access utilities
file transfer and access utilitiesfile transfer and access utilities
file transfer and access utilities
 
retrieving the mail
retrieving the mailretrieving the mail
retrieving the mail
 
connectivity utility
connectivity utilityconnectivity utility
connectivity utility
 
network hardware
network hardwarenetwork hardware
network hardware
 
ระบบเครือข่ายไร้สาย (wireless lan)
ระบบเครือข่ายไร้สาย (wireless lan)ระบบเครือข่ายไร้สาย (wireless lan)
ระบบเครือข่ายไร้สาย (wireless lan)
 
routing
routingrouting
routing
 
the transport layer
the transport layerthe transport layer
the transport layer
 
ระดับชั้นเน็ตเวิร์ก
ระดับชั้นเน็ตเวิร์กระดับชั้นเน็ตเวิร์ก
ระดับชั้นเน็ตเวิร์ก
 
ระดับชั้นดาต้าลิงค์
ระดับชั้นดาต้าลิงค์ระดับชั้นดาต้าลิงค์
ระดับชั้นดาต้าลิงค์
 
สถาปัตยกรรมเครือข่ายคอมพิวเตอร์และบริการ
สถาปัตยกรรมเครือข่ายคอมพิวเตอร์และบริการสถาปัตยกรรมเครือข่ายคอมพิวเตอร์และบริการ
สถาปัตยกรรมเครือข่ายคอมพิวเตอร์และบริการ
 
การส่งข้อมูลผ่านสายส่งและเทคนิคการส่งข้อมูลผ่านเครือข่าย
การส่งข้อมูลผ่านสายส่งและเทคนิคการส่งข้อมูลผ่านเครือข่ายการส่งข้อมูลผ่านสายส่งและเทคนิคการส่งข้อมูลผ่านเครือข่าย
การส่งข้อมูลผ่านสายส่งและเทคนิคการส่งข้อมูลผ่านเครือข่าย
 
ความรู้พื้นฐานของระบบการสื่อสารข้อมูล
ความรู้พื้นฐานของระบบการสื่อสารข้อมูลความรู้พื้นฐานของระบบการสื่อสารข้อมูล
ความรู้พื้นฐานของระบบการสื่อสารข้อมูล
 
พัฒนาเศรษฐกิจ
พัฒนาเศรษฐกิจพัฒนาเศรษฐกิจ
พัฒนาเศรษฐกิจ
 

Recently uploaded

80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationNeilDeclaro1
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactisticshameyhk98
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answersdalebeck957
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 

Recently uploaded (20)

80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 

php 2 Function creating, calling, PHP built-in function

  • 1. PHP
  • 3. Function • Sub routine. • A function is a block of code that you can call throughout your PHP script. Functions are often used for common functionalities that can occur in your PHP program. For an example think that you write a program that relates to seasonal offers. In many occasions, you would need to know the final price after a discount was given. You can define a function for this as below.
  • 4. Creating function function functionName($param1, $param2) { code to be executed; code to be executed; return anyValue; } Optionally, datas to be passed into function. It can be unlimited number of datas. Optionally return value back to the one who call the function.
  • 5. Creating function- Example function print_hello() { print “Hello”; } A function that does not require any external datas to process. It done the job by just print word “Hello” to the screen. It return nothing to the place it is being called.
  • 6. Creating function- Example function transfer($account_a, $account_b, $amount) { $account_a = $account_a - $amount; $account_b = $account_b + $amount; return $account_b; } • A function that takes 3 external datas (parameters) to process. The function subtract $amount from $account_a then $amount add to $account_b. Finally return the result to the place it is being called.
  • 7. Calling function /*** Functions declaration ***/ function print_hello() { print “Hello”; } function transfer($account_a, $account_b, $amount) { $account_a = $account_a - $amount; $account_b = $account_b + $amount; return $account_b; } /*** Using functions we just declare above here ***/ print_hello(); print transfer(500, 200, 50);
  • 9. Useful PHP Built-in functions • (Q) What is PHP built-in functions? • (A) Instant, Ready to use functions provided by PHP producer. No need to create by ourself.
  • 10. phpinfo() Benefit: Print PHP information, configuration, settings out to screen Example: phpinfo();
  • 11. Function for String :: trim() Benefit: Remove whitespace from both ends of string Example: $str = ‘ Hello There ’; $new_str = trim($str); print $new_str; Result: Hello There
  • 12. Function for String :: explode() Benefit: Explode string into array $pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; $pieces = explode(" ", $pizza); print $pieces[0]; // piece1 print $pieces[1]; // piece2
  • 13. Function for String :: implode() Benefit: Implode array into string $array = array('lastname', 'email', 'phone'); $comma_separated = implode(",", $array); print $comma_separated; // lastname,email,phone
  • 14. Function for String :: strlen() Benefit: Get string’s length (How many characters inside the string) $str = 'abcdef'; print strlen($str); // 6 $str = ' ab cd '; print strlen($str); // 7
  • 15. Function for String :: strstr() Benefit: Find the first occurrence in the string case-sensitive stristr() will be used for incase- sensitive. $email = 'name@example.com'; $domain = strstr($email, '@'); echo $domain; // prints @example.com
  • 16. Function for Array :: count() Benefit: Count all elements in array $a = array(); $a[0] = 1; $a[1] = 3; $a[2] = 5; $result = count($a); print $result; // output 3 $food = array('fruits' => array('orange', 'banana', 'apple'), 'veggie' => array('carrot', 'collard', 'pea')); // recursive count print count($food, COUNT_RECURSIVE); // output 8 // normal count print count($food); // output 2
  • 17. Function for Array :: array_rand() Benefit: Pick one or more random entries out of an array $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank"); $rand_keys = array_rand($input, 2); print $rand_keys;
  • 18. Function for Array :: print_r() Benefit: Print structure of array. Note that we should wrap it with <pre></pre> for readability. $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z')); print “<pre>”; print_r($a); print “<pre>”; /**** <pre> Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) ) </pre> ****/