0% found this document useful (0 votes)
79 views

PHP Unit 2 PPT

Uploaded by

anushkapjpatel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

PHP Unit 2 PPT

Uploaded by

anushkapjpatel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

PHP and WordPress

DU # 2302CS402

Unit-02
Functions and Form

Prof. Payal H Hinsu


Department of Computer
Engineering
Darshan Institute of Engineering & Technology for Diploma Studies, Rajkot
[email protected]
7096979963
Topics
 •Looping
User Defined Functions
• Recursion
• Include and Require
• Include_once and Require_once
• Form Attributes
• Form Processing
• PHP String
• PHP String Functions
User Defined Functions
Section - 1
PHP Functions (User Defined Functions)
 A function is a piece of code which takes zero or more input in the form of
parameter and does some processing and returns a value.
 Creating PHP function
 Begins with keyword function and then the space and then the name of
the function then parentheses”()” and then code block “{}”

<?php
function functionName()
{
//code to be
executed;
}
?>
Note: Function name can start with a letter or underscore "_", but not a number!

#2302CS402 (PHP and WordPress)  Unit 02 – Functions


Prof. Payal H Hinsu 4
PHP Functions (Cont…)
 Where to put the function implementation? Here function call is before
 In PHP a function could be defined before or after it is called. implementation, which is also valid

<?php <?php
function functionName() functionName();
{
//code to be function functionName()
executed; {
} //code to be
executed;
functionName(); }
?> ?>

Here function call is after


implementation, which is valid

#2302CS402 (PHP and WordPress)  Unit 02 – Functions


Prof. Payal H Hinsu 5
PHP Functions (User Defined Functions)
 PHP has a large number of built-in functions such as mathematical,
string, date, array functions etc. It is also possible to define a function
as per specific requirement. Such function is called user defined function.
 A function is a reusable block of statements that performs a specific
task. This block is defined with function keyword and is given a name
that starts with an alphabet or underscore. This function may be
called from anywhere within the program any number of times.
 Function may be defined with optional but any number of arguments.
However, same number of arguments must be provided while calling.
 Function's body can contain any valid PHP code i.e. conditionals,
loops etc. (even other functions or classes may be defined inside a
function).
 After executing statements in the block, program control goes back to the
location from which it was invoked irrespective of presence of last
statement of function block as return. An expression in front of return
statement returns its value to calling environment.
#2302CS402 (PHP and WordPress)  Unit 02 – Functions
Prof. Payal H Hinsu 6
PHP Functions (User Defined Functions)
Syntax
//define a function
function myfunction($arg1, $arg2, ... $argn)
{
statement1;
statement2;
..
..
return $val;
}
//call function
$ret=myfunction($arg1, $arg2, ... $argn);

#2302CS402 (PHP and WordPress)  Unit 02 – Functions


Prof. Payal H Hinsu 7
PHP Function Arguments
With Argument, With With Argument, No
Return Type Return Type
<?php <?php
function cube($n){ function cube($n){
return $n*$n*$n; $c = $n*$n*$n;
} echo "cube = ".$c;
echo "Cube of 3 is: }
".cube(3); cube(3);
No Argument, No Return
?> ?>
Type No Argument, With
<?php Return Type
function cube(){ <?php
$n=3; function cube(){
$c = $n*$n*$n; $n=3;
echo "cube = ".$c; return $n*$n*$n;
} }
cube(); echo "Cube of 3 is: ".cube();
?>Prof. Payal H Hinsu #2302CS402 (PHP and WordPress)  Unit 02 – Functions
8
Recursion
Section - 2
Recursion
 Recursion is define as function call itself.
 A recursive function usually consists of two main parts:
 Base Case: The simplest instance of the problem, which can be solved directly
without recursion.
 Recursive Case: The part where the function calls itself to tackle the remaining
Syntax
sub-problems.
function recursiveFunction($param) {
// Base case: Condition to stop the recursion
if (/* base condition */) {
return /* some value */;
} else {
// Recursive case: The function calls itself
return recursiveFunction(/* modified
parameter */);
}
} #2302CS402 (PHP and WordPress)  Unit 02 – Functions
Prof. Payal H Hinsu 10
Recursion (Cont…)
Example Output
<?php
function factorial($n) {
// Base case: if $n is 0, return 1
if ($n == 0) {
return 1;
}
else {
// Recursive case: return $n multiplied by
the factorial of $n-1
return $n * factorial($n - 1);
}
}

// Example usage
echo factorial(5); // Output: 120
?>

#2302CS402 (PHP and WordPress)  Unit 02 – Functions


Prof. Payal H Hinsu 11
Include and Require
Section - 3
Include and Require
 The include or require statement takes all the text/code/markup that
exists in the specified file and copies it into the file that uses the include
statement.
 Including files is very useful when you want to include the same PHP,
HTML, or text on multiple pages of a website.
 It is possible to insert the content of one PHP file into another PHP file with
the include or require statement.
 The include and require statements are identical, except upon failure:
 require will produce a fatal error (E_COMPILE_ERROR) and stop
the script.
 include will only produce a warning (E_WARNING) and the script
will continue.
 So, if you want the execution to go on and show users the output, even if
the included file is missing, use the include statement.
 Otherwise, use the require statement
#2302CS402 (PHP and WordPress)  to include
Unit 02 – Functions a file to the flow of
Prof. Payal H Hinsu 13
Include and Require (Cont…)
Example
 Including files saves a lot of
header.php
work, as you can create a <h1 style="color: blue;">Header
standard header, footer, or menu Content</h1>
file for all your web pages. footer.php
<h1 style="color: green;">Footer
 Then, when the header needs to Content</h1>
index.php
be updated, you can only update <!DOCTYPE html>
the
Syntax
header fileOutputand it will <html>
reflect
include in all pages. <head></head>
<body>
'filename'; <?php
require include 'header.php';
'filename'; ?>
<h1>Main Content</h1>
<?php
require 'footer.php';
?>
</body>
</html>
#2302CS402 (PHP and WordPress)  Unit 02 – Functions
Prof. Payal H Hinsu 14
Include_once and Require_once
 The include_once and require_once keyword is used to embed PHP code
from another file similar to include and required respectively.
 The difference is include_once and require_once keyword does not
include same file if it is already included in a page.
Syntax
include_once 'filename
';
require_once 'filename’;

#2302CS402 (PHP and WordPress)  Unit 02 – Functions


Prof. Payal H Hinsu 15
Form Attributes
Section - 4
Form Attributes
Attribute Description
accept-charset Specifies the character encodings used for form submission
action Specifies where to send the form-data when a form is submitted
autocomplete Specifies whether a form should have auto complete on or off
enctype Specifies how the form-data should be encoded when submitting it
to the server (available only when we use method=“POST")
method Specifies the HTTP method to use when sending form-data (GET /
name POST)
Specifies the name of the form
novalidate Specifies that the form should not be validated when submitted
rel Specifies the relationship between a linked resource and the current
target document
Specifies where to display the response that is received after
submitting the form

#2302CS402 (PHP and WordPress)  Unit 02 – Functions


Prof. Payal H Hinsu 17
Form Processing
Section - 5
Form Processing
 There are two ways the browser client can send information to the
web server.
 GET Method and POST Method
 Both GET and POST create an array and hold key/value pairs (e.g. array
( key => value, key2 => value2, key3 => value3, ...)).
 $_GET
 $_GET variable is a superglobal variable used to collect data sent via URL
parameters in an HTTP GET request.
 When you are using GET method in the form collection element the information will
be send to the destination file through URL using the concept of Query String.
 Collection of names and value pairs is called query string and every query
string starts with in URL.
 Name and value separated by ampersand (&) sign.
 $_POST
 $_POST variable is a superglobal variable used to collect form data sent to the
server via an HTTP POST request.
 The data sent via $_POST is not
#2302CS402 visible
(PHP and WordPress)in
 the
Unit 02 URL, making it more secure
– Functions for
Prof. Payal H Hinsu 19
Form Processing (Cont…)
 We can access form data using inbuilt PHP associative array.
 $_GET => in case we have used get method in the form
 $_POST => in case we have used post method in the form
 $_REQUEST => in both the cases
 $_GET
Example
html receive.php
<form action="receive.php" <?php
method="get"> $u = $_GET['UserName'];
<input type="text" echo($u);
name="UserName"> ?>
<input type="submit">
</form>

#2302CS402 (PHP and WordPress)  Unit 02 – Functions


Prof. Payal H Hinsu 20
Form Processing (Cont…)
 $_POST
Example
html receive.php
<form action="receive.php" <?php
method="post"> $u = $_POST['UserName'];
<input type="text" echo($u);
name="UserName"> ?>
<input type="submit">
</form>
 $_REQUEST
 $_REQUEST variable is a superglobal variable, which can hold the content of both
$_GET and $_POST variable.
Example
html receive.php
<form action="receive.php" <?php
method="post"> $name =
<input type="text" name="fName"> $_REQUEST['fname'];
<input type="submit"> echo ($name);
</form>
Prof. Payal H Hinsu
#2302CS402 (PHP and WordPress)  ?>02 – Functions
Unit
21
PHP String
Section - 6
PHP String
 A string is a sequence of characters, like "Hello world!".
 Strings in PHP are surrounded by either double quotation marks, or
single quotation marks.
 Double quoted string literals perform operations for special characters:
 E.g. when there is a variable in the string, it returns
Example Output the value of the variable:
<?php
$x = "Good Morning";
echo "Hello $x";
?>

 Single quoted strings does not perform such actions, it returns the string like it
Output
was written, with the variable name:
Example
<?php
$x = "Good Morning";
echo 'Hello $x';
?>
#2302CS402 (PHP and WordPress)  Unit 02 – Functions
Prof. Payal H Hinsu 23
String Functions
Section - 7
String Functions
 String functions in PHP are built-in functions used to manipulate and
process strings.
 PHP provides a wide range of these functions to perform various
operations, such as searching, replacing, formatting, and
analyzing strings.
 String can be think as a array of characters, so it is possible to do
something like this,
$mystring = “Welcome to Darshan College of Engineering”;
print ($mystring[11]) ; // which will print ‘D’
 This uses an index as an offset from the beginning of the string
starting at 0

#2302CS402 (PHP and WordPress)  Unit 02 – Functions


Prof. Payal H Hinsu 25
String Functions (Cont…)
String Function Purpose
strlen($string) Returns length of string.
strstr($str1,$str2) Finds str2 inside str1 (returns false if not found or returns
portion of string1 that contains it).
strpos($str1,$str2) Finds str2 inside str1 and returns index.
str_replace($search,$replace, Looks for $search within $str and replaces with #replace,
$str,$count) returning the number of times this is done in $count
substr($string,$startposition, Returns string from either start position to end or the section
$length) given by $startpos to $startpos+$length.
trim($string) Trims away white space, including tabs, newlines and spaces,
rtrim($string) from both beginning and end of a string.
ltrim($string) ltrim is for the start of a string only and
rtrim for the end of a string only.
strip_tags($string, Strips out HTML tags within a string, leaving only those within
$tagsallowed) $tagsallowed intact
stripslashes($string) Strips out inserted backslashes.

#2302CS402 (PHP and WordPress)  Unit 02 – Functions


Prof. Payal H Hinsu 26
String Functions (Cont…)
String Function Purpose
explode($delimiters,$string) It will breaks $string up into an array at the points marked by
implode($glue,$array) the $delimiters.
Function returns combined string from an array.
strtolower($string) Converts all characters in $string to lowercase.
strtoupper($string) Converts all characters in $string to uppercase.
ucword($string) Converts all the first letters in a string to uppercase.

#2302CS402 (PHP and WordPress)  Unit 02 – Functions


Prof. Payal H Hinsu 27
PHP and WordPress
DU # 2302CS402

Thank
You
Prof. Payal H Hinsu
Department of Computer
Engineering
Darshan Institute of Engineering & Technology for Diploma Studies, Rajkot
[email protected]
7096979963

You might also like