0% found this document useful (0 votes)
2 views19 pages

Module 4 Function Regular Expression Form Handling

A function in PHP is a reusable block of code that can accept parameters, execute statements, and return values. PHP supports both built-in and user-defined functions, allowing for flexible coding practices. Additionally, PHP provides tools for form processing and regular expressions for string manipulation, enhancing its capabilities for web development.

Uploaded by

Shreyash Gavali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views19 pages

Module 4 Function Regular Expression Form Handling

A function in PHP is a reusable block of code that can accept parameters, execute statements, and return values. PHP supports both built-in and user-defined functions, allowing for flexible coding practices. Additionally, PHP provides tools for form processing and regular expressions for string manipulation, enhancing its capabilities for web development.

Uploaded by

Shreyash Gavali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Explain function in PHP

• A function in PHP is a self-contained block of code that performs a


specific task.
• It can accept inputs (parameters), execute a set of statements, and
optionally return a value.
• PHP functions allow code reusability by encapsulating a block of code
to perform specific tasks.
• Functions can accept parameters and return values, enabling dynamic
behavior based on inputs.
• PHP supports both built-in functions and user-defined functions,
enhancing flexibility and modularity in code.
Syntax to create function:

function functionName($parameter1, $parameter2)


{
// code to be executed

return $value; // optional


}

Example:
function myMessage()
{
echo "Hello world!";
}
Write a php program to perform addition of two numbers using function
<?php

function addNumbers($a, $b)


{

$result = $a + $b;

return $result;
}

echo “addition is: “ . addNumbers(5, 10); // Calling the function

Out[put
?>
PHP Function Parameters and Arguments
• Functions can accept input values, known as parameters or
arguments.
• Parameters are variables defined in the function declaration that
accept values.
• Arguments are the actual values passed to the function when it is
called.

parameters

arguments
Passing values to the function:
1. Passing by Value
• When you pass a variable by value, PHP makes a copy of that
variable for use inside the function.
• Any changes made inside the function do not affect the
original variable outside.
<?php
function add($x, $y)
{
$x = $x + $y;
return $x;
}

$a=2;
echo add($a, 3); // Outputs: 5
echo $a; //2
?>
2. Passing by reference
• When you pass a variable by reference, the function works
directly with the original variable.
• Any changes made inside the function will affect the original
variable outside the function.
<?php
function add(&$x, $y)
{
$x = $x + $y;
return $x;
}
$a = 5;
echo add($a, 3); //Outputs: 8
echo $a; // Outputs: 8
?>
PHP Form Processing:

• Form handling is the process of collecting and processing information


that users submit through HTML forms.
• In PHP, we use special tools called $_POST and $_GET to gather the
data from the form.
• Which tool to use depends on how the form sends the data—either
through the POST method (more secure, hidden in the background) or
the GET method (data is visible in the URL).
Form attributes:
1. action: The action attribute specifies the URL where the form data
will be sent when the form is submitted.
2. method: The method attribute specifies the HTTP method (GET or
POST) to use when sending form data.
3. name: The name attribute is crucial in PHP form handling, as it is used
to refer to the data submitted by the form fields.
4. target: The target attribute specifies where to display the response
after submitting the form. It determines where the resulting page (or
response) will appear once the form is submitted.
Example:
[Link]

<form method="post" action="process_form.php" target="_blank">


<input type="text" name="username" required>
<input type="submit" value="Submit">
</form>
process_form.php

<?php
$name="";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$user = $_POST["username"];
}
echo "<h2>Welcome". $user.”</h1>”;
?>
Explain PHP Regular Expressions

• A regular expression is a sequence of characters that forms a


search pattern.
• When you search for data in a text, you can use this search pattern
to describe what you are searching for.
• A regular expression can be a single character, or a more
complicated pattern.
• Regular expressions can be used to perform all types of text
search and text replace operations.
$variable = "/pattern/modifiers"

• pattern - the character sequence to search for


• modifiers - how the search is performed
i Performs a case-insensitive search
m Performs a multiline search (patterns that search for a match at the
beginning or end of a string will now match the beginning or end
of each line)
u Enables correct matching of UTF-8 encoded patterns

example:
$exp = "/ratnagiri/i";
Regular Expression Patterns
Expression Description
[abc] Find one or many of the characters inside the brackets
[^abc] Find any character NOT between the brackets
[a-z] Find any character alphabetically between two letters

[A-z] Find any character alphabetically between a specified upper-case


letter and a specified lower-case letter

[A-Z] Find any character alphabetically between two upper-case letters.

[123] Find one or many of the digits inside the brackets

[0-9] Find any digits


Regular Expression Patterns

Metacharacter Description
| Find a match for any one of the patterns separated by | as
in: cat|dog|fish
^ Finds a match as the beginning of a string as in: ^Hello
$ Finds a match at the end of the string as in: World$

\d Find any digits

\D Find any non-digits


\s Find any whitespace character
Regular Expression Patterns

Quantifier Description
n+ Matches any string that contains at least one n
n* Matches any string that contains zero or more occurrences
of n
n{3} Matches any string that contains a sequence of 3 n's

n{2, 5} Matches any string that contains a sequence of at least 2,


but not more that 5 n's
Regular Expression Function
1. preg_match()
This function searches string for pattern, returns true if pattern
exists, otherwise returns false.

<?php
$str = "Visit Ratnagiri";
$pattern = "/ratnagiri/i";
echo preg_match($pattern, $str);
?>

Output
1
2. preg_match_all()
This function searches for all the occurrences of string pattern
against the string.

<?php
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern = "/ain/i";
echo preg_match($pattern, $str)."\n"; //output 1
echo preg_match_all($pattern, $str); //output 4
?>

Output
3. preg_grep() Function

The preg_grep() function returns an array containing only elements


from the input that match the given pattern.

<?php
$input = ["Red","Pink","Green","Blue","Purple"];
$result = preg_grep("/^p/i", $input);
print_r($result);
?>

Output
4. preg_replace() Function

The preg_replace() function replaces all the matches of the pattern


in a string with another string

<?php
$str = "Visit Microsoft!";
$pattern = "/microsoft/i";
echo preg_replace($pattern, "FAMT", $str);

?>

Output

You might also like