PHP 7-8
PHP 7-8
Date: _________________
Practical No.7: Built-In functions
a. Write PHP script to demonstrate the use of various strings handling
function.
b. Write a PHP script to demonstrate the use of Include() and
require() function.
c. Write PHP script to demonstrate the use of Array functions.
d. Write PHP script to demonstrate the use of fopen(), fread(),
fwrite() and fclose() file functions.
A. Objective:
Built-in functions are predefined functions in PHP that exist in the PHP library. These
PHP inbuilt functions make PHP a very efficient and productive scripting language. The
built in functions of PHP classified into many categories. Students will be able to learn
various built-in functions like string functions, array functions, file functions etc.
1|Pag e
Web Development using PHP (4341604)
E. Practical Outcome(PRo)
Students will be able to use different built-in functions of string, file and array.
G. Prerequisite Theory:
PHP String
Functions
echo
Use: To output/display one or more strings or variables to the screen.
Note: The echo() function is not actually a function, so you are not
required to use parentheses with it. However, if you want to pass more
than one parameter to echo(), using parentheses will generate an error.
Syntax: echo(strings)
Example: <?php
echo "Hello world";
?>
Output: Hello world
print
Use: To output/display one or more strings or variables to the screen.
Note: The print() function is not actually a function, so you are not
required to use parentheses with it.
Syntax: print(strings)
Example: <?php
print "Hello world";
?>
Output: Hello world
strlen()
Use: To find the length of strings.
Return: The length of a string.
Syntax: strlen(String)
2|Pag e
Web Development using PHP (4341604)
Example: <?php
echo strlen("Hello world");
?>
Output: 11
str_word_count()
Use: To count the words of strings.
Return: The number of words in the string.
Syntax: str_word_count(String)
Example: <?php
echo str_word_count("Hello world");
?>
Output: 2
strrev()
Use: To reverse the given strings.
Return: The reversed string.
Syntax: strrev(String)
Example: <?php
echo strrev("Hello world");
?>
Output: dlrow olleH
str_replace()
Use: To replace some of the characters in a string with some other.
Return: The string with replacements.
Syntax: str_replace(search, replace, string)
Here;
Search: Required. This will be the string to search for replacing.
Replace: Required. This will replace the searched string.
String: Required. This will the string which we have to search and
replace.
Example: <?php
echo str_replace("Hello","Hi","Hello world");
?>
Output: Hi world
strstr()
Use: To display the part of the string from the first search occurrence.
Note: This function is case-sensitive. For a case-insensitive search, use
stristr() function.
Return: The rest of the string.
Syntax: strstr(string,search,before_search)
3|Pag e
Web Development using PHP (4341604)
Here;
String: Required. This will be the string to search.
Search: Required. Specifies the string to search for. If this is a number, it
will search for the character matching the ASCII value of the number.
Before_search: Optional. A boolean value whose default is "false". If set to
"true", it returns the part of the string before the first occurrence of the
search parameter.
Example: <?php
echo strstr("Hello world","world") . "<br/>";
echo strstr("Hello world","world",true);
?>
Output: world
Hello
substr()
Use: To extract the string.
Return: The a part of the string.
Syntax: substr(string,start,length)
Here;
String: Required. This will be the string to return a part of.
Start: Required. Specifies where to start in the string
- A positive number - Start at a specified position in the string
- A negative number - Start at a specified position from the end of the
string
- 0 - Start at the first character in string
Length: Optional. Specifies the length of the returned string.
- A positive number - The length to be returned from the start
- Negative number - The length to be returned from the end
- If the length parameter is 0, NULL, or FALSE - returns empty string
Example: <?php
echo substr("Hello world",7) ."<br/>";
echo substr("Hello world",-7) ."<br/>";
echo substr("Hello world",7,2) ."<br/>";
echo substr("Hello world",1,-7) ."<br/>";
echo substr("Hello world",-7,-1);
?>
Output: orld
o world
or
ell
o worl
strtolower()
4|Pag e
Web Development using PHP (4341604)
strtoupper()
Use: To extract the string.
Return: String in upper case.
Syntax: strtoupper(string)
Example: <?php
echo strtoupper("Hello World");
?>
Output: HELLO WORLD
ucwords()
Use: To convert the first character of each word in a string to uppercase.
Return: String with first character of each word in uppercase.
Syntax: ucwords(string, delimiters)
Here;
String: Required. This will be the string to convert.
Delimiters: Optional. Word separator character.
Example: <?php
echo ucwords("hello world") ."<br/>";
echo ucwords("hello-world", "-");
?>
Output: Hello World
Hello-World
strpos()
Use: To find the position of the first occurrence of a string inside another
string.
Note: The strpos() function is case-sensitive.
Return: The position of the first occurrence of a string inside another string, or
FALSE if the string is not found. (String positions start at 0).
Syntax: strpos(string, find, start)
Here;
String: Required. This will be the string to search.
Find: Required. This will be the string to find.
Start: Optional. Specifies where to begin the search. (If start is a negative
number, it counts from the end of the string.)
5|Pag e
Web Development using PHP (4341604)
Example: <?php
echo ucwords("hello world") ."<br/>";
echo ucwords("hello-world", "-");
?>
Output: Hello World
Hello-World
ltrim()
Use: To remove whitespace and other predefined characters from left side of
a string.
Return: String after removing of space/characters.
Syntax: ltrim(string, charlist)
Here;
String: Required. This will be the string to check.
Charlist: Optional. This will be characters to remove from the string.
Example: <?php
echo ltrim("Hello world","Held");
?>
Output: o world
rtrim()
Use: To remove whitespace and other predefined characters from right side
of a string.
Return: String after removing of space/characters.
Syntax: rtrim(string, charlist)
Here;
String: Required. This will be the string to check.
Charlist: Optional. This will be characters to remove from the string.
Example: <?php
echo rtrim("Hello world","Held");
?>
Output: Hello wor
trim()
Use: To remove whitespace and other predefined characters from both sides
of a string.
Return: String after removing of space/characters.
Syntax: trim(string, charlist)
Here;
String: Required. This will be the string to check.
Charlist: Optional. This will be characters to remove from the string.
Example: <?php
6|Pag e
Web Development using PHP (4341604)
implode()
Use: To convert the array to strings. It takes an array and converts those
array to strings by using any separator.
Return: String from elements of an array.
Syntax: implode(separator, array)
Here;
Separator: Optional. This will be the separator to put between the array
elements. Default is "" (an empty string).
Array: Required. This will be an array.
Example: <?php
$arr = array('Hello','World');
echo implode(" ",$arr) ."<br/>";
echo implode("-",$arr);
?>
Output: Hello World
Hello-World
explode()
Use: To break a string into an array.
Return: An array of strings.
Syntax: explode(separator, string, limit)
Here;
Separator: Required. This will be the for where to break the string.
String: Required. This will be string to split.
Limit: This will be the number of array elements to return.
limit can be, >0 - Returns an array with a maximum of limit element(s)
<0 - Returns an array except for the last -limit element(s)
0 - Returns an array with one element
Example: <?php
$str = "Welcome to the PHP world.";
print_r (explode(" ",$str));
echo "<br/>";
print_r (explode(" ",$str,0));
echo "<br/>";
print_r (explode(" ",$str,3));
echo "<br/>";
print_r (explode(" ",$str,-1));
?>
7|Pag e
Web Development using PHP (4341604)
Output: Array ( [0] => Welcome [1] => to [2] => the [3] => PHP [4] => world. )
Array ( [0] => Welcome to the PHP world. )
Array ( [0] => Welcome [1] => to [2] => the PHP world. )
Array ( [0] => Welcome [1] => to [2] => the [3] => PHP )
Syntax:
include 'filename';
or
require 'filename';
Let’s assume that we have a file named ‘two.php’ and we want to include it in our page
named ‘one.php’.
two.php:
<?php
echo "<p>Thank you for visiting our website.</p>";
?>
one.php:
<html>
<body>
</body>
</html>
Output:
8|Pag e
Web Development using PHP (4341604)
Same way you can use require() function and test it.
Here;
Search: Required. This will be the what to search for.
Array: Required. This will be array to search.
Type: Optional. If this is set to TRUE, it searches for the search-string and
specific type in the array.
Example: <?php
$dept = array("IT", "CE", "ME", 16);
if (in_array("16", $dept, TRUE)) {
echo "Match found<br>";
}
else {
echo "Match not found<br>";
}
if (in_array("IT",$dept))
{ echo "Match
found<br>";
}
else {
echo "Match not found<br>";
}
?>
Output: Match not found
Match found
in_array()
Use: To merge one or more arrays into one array.
Return: Merged array.
Syntax: array_merge(array1, array2, …., arrayN)
Example: <?php
$a1=array("IT","CE");
$a2=array("ME","EE");
print_r(array_merge($a1,$a2));
?>
Output: Array ( [0] => IT [1] => CE [2] => ME [3] => EE )
array_push()
Use: To insert one or more elements to the end of an array.
9|Pag e
Web Development using PHP (4341604)
array_pop()
Use: To delete the last element of an array.
Return: The new array after deletion.
Syntax: array_pop(array)
Example: <?php
$a=array("IT","CE","ME");
array_pop($a);
print_r($a);
?>
Output: Array ( [0] => IT [1] => CE )
array_replace()
Use: To replace the values of the first array with the values from following
arrays.
Return: The replaced array, or NULL if an error occurs.
Syntax: array_replace(array1, array2, …)
Example: <?php
$a1=array("IT","CE");
$a1=array("EE","ME");
print_r(array_replace($a1,$a2));
?>
Output: Array ( [0] => EE [1] => ME )
array_reverse()
Use: To reverse the specified arrays.
Return: The reversed array.
Syntax: array_reverse(array)
Example: <?php
$a=array("IT","CE");
print_r(array_reverse($a));
?>
Output: Array ( [0] => CE [1] => IT )
array_search()
Use: To search an array for a value.
Return: The key of a value if it is found in the array, and FALSE otherwise.
10 | P a g e
Web Development using PHP (4341604)
Here;
Value: Required. This will be the value to search for.
Array: Required. This will be array to search.
Strict: Optional. If this parameter is set to TRUE, then this function will
search for identical elements in the array. Default is FALSE.
Example: <?php
$a=array("a"=>"IT","b"=>"CE");
echo array_search("IT",$a);
?>
Output: a
count()
Use: To count of elements in an array.
Return: The number of elements in an array.
Syntax: count(array, mode)
Here;
Array: Required. This will be an array.
Mode: Optional. It will be 0 or 1. Default is 0 and it does not count all
elements of multidimensional arrays. If it is 1 then it counts the array
recursively (counts all the elements of multidimensional arrays).
Example: <?php
$a=array("IT","CE");
echo (count($a));
?>
Output: 2
11 | P a g e
Web Development using PHP (4341604)
list()
Use: To assign values to a list of variables in one operation.
Return: The assigned array.
Syntax: list(var1, var2, var3, …)
Example: <?php
$arr = array("IT","CE");
list($a, $b) = $arr;
echo "Our departments are $a and $b.";
?>
Output: Our departments are IT and CE.
sort()
Use: To sort an indexed array in ascending order.
Return: TRUE on success. FALSE on failure
Syntax: sort(array)
Example: <?php
$arr=array("IT","CE","ME","EE");
sort($arr);
$alen=count($arr);
for($x=0;$x<$alen;$x++)
{
echo $arr[$x] . "<br/>”;
}
?>
Output: CE
EE
IT
ME
12 | P a g e
Web Development using PHP (4341604)
Here;
Array: Required. This will be file or URL to open.
Mode: Required. This will be the type of access you require to the file.
Modes can be r, r+, w, w+, a, a+, x, x+, c, c+.
fclose()
Use: To close a file.
Return: TRUE on success, FALSE on failure.
Syntax: fclose(filepointer)
fread()
Use: To read from an open file.
Return: A file pointer resource on success, FALSE and an error on failure.
Syntax: fread(file, length)
Here;
File: Required. This will be file to read from.
Length: Required. This will be the maximum number of bytes to read.
Example: <?php
$file = fopen("test.txt","r");
fread($file,"5");
fclose($file);
?>
fwrite()
Use: To write to an open file.
Return: The number of bytes written, FALSE on failure.
Syntax: fwrite(file, string, length)
Here;
File: Required. This will be file to write to.
string: Required. This will be string to write in file.
Length: Optional. This will be maximum number of bytes to write.
Example: <?php
$file = fopen("test.txt","w");
fwrite($file,"Hello World")
fclose($file);
?>
Output: 11
13 | P a g e
Web Development using PHP (4341604)
14 | P a g e
Web Development using PHP (4341604)
H. Resources/Equipment Required
Sr. Instrument/Equipment/
Specification Quantity
No. Components/Trainer kit
Computer (i3-i5 preferable), RAM
1 Hardware: Computer System
minimum 2 GB and onwards
2 Operating System Windows/ Linux/ MAC
XAMPP server (PHP, Web server, As Per
3 Software
Database) Batch Size
Notepad, Notepad++, Sublime Text or
4 Text Editor
similar
J. Source code:
A) Write PHP script to demonstrate the use of various strings handling function.
15 | P a g e
Web Development using PHP (4341604)
Output:
B) Write a PHP script to demonstrate the use of Include() and require() function.
<?php
// Including another PHP file using include()
echo "<h2>Using include()</h2>";
include 'include.php';
?>
<?php
// Requiring another PHP file using require()
echo "<h2>Using require()</h2>";
require 'required.php';
?>
Included file:
<?php
echo "<p>This is included content using include()
function.</p>";
?>
Output:
16 | P a g e
Web Development using PHP (4341604)
17 | P a g e
Web Development using PHP (4341604)
18 | P a g e
Web Development using PHP (4341604)
<?php
$filenm= "Hello.txt";
} else {
echo "Error opening the file for writing.";
}
} else {
echo "Error opening the file for reading.";
}
?>
Output:
19 | P a g e
Web Development using PHP (4341604)
20 | P a g e
Web Development using PHP (4341604)
2. function is used for replacing the whole string with an alternate string.
<?php
$a=array("a"=>"IT","b"=>array("CE"));
echo (count($a,1));
?>
L. References / Suggestions
1) https://www.w3schools.com/php/default.asp
2) https://www.guru99.com/php-tutorials.html
3) https://www.tutorialspoint.com/php/
4) https://tutorialehtml.com/en/php-tutorial-introduction/
5) www.tizag.com/phpT/
6) https://books.goalkicker.com/PHPBook/
7) https://spoken-tutorial.org/tutorial- search/?
search_foss=PHP+and+MySQL&search_language=English
8) https://codecourse.com/watch/php-basics
9) https://onlinecourses.swayam2.ac.in/aic20_sp32/preview
M.Assessment-Rubrics
Faculty
Marks Obtained Date
Signature
Program Implementation Student’s engagement
Correctness and Presentation in practical activities Total
(4) Methodology (3) (3) (10)
R1 R2 R3
21 | P a g e
Web Development using PHP (4341604)
Date: _________________
Practical No.8: Form Handling
a. Create student registration form using text box, check box, radio
button, select, submit button. And display user inserted value in
new PHP page using GET or POST Method.
b. Write a PHP script to explain the concept of $_REQUEST.
A. Objective:
Forms are important component of the web application that allows to collect
information from the users. Forms are used for various tasks such as login,
registration, contact us, and application specific information collection. This practical
will help students to design a form and to collect data entered in form input by users
using PHP.
22 | P a g e
Web Development using PHP (4341604)
CO3: Design and develop a Web site using form controls for presenting web-based
content.
E. Practical Outcome(PRo)
Students will be able to use a form and to collect data entered in form input by users
using PHP.
G. Prerequisite Theory:
From Tag
It is used to design form which contains various input controls that allows user to
input information.
Input Element
It allows you to place various controls in the form to accept input from user.
23 | P a g e
Web Development using PHP (4341604)
Textbox Syntax:-
<input type=“text” name=“Text Name” value=“Default value”>
Hidden field Syntax:-
<input type=“hidden” name=“field Name” value=“Default value”>
Password Syntax:-
<input type=“password” name=“psw Name” value=“Default value”>
TextArea Syntax:-
<textarea name =“Text name” rows=“rowsize” cols=“columnsize”>
checkbox Syntax:-
<input type=“checkbox” name=“checkBoxName” value=“Default value”
checked> TextToDisplay </input>
radio Button Syntax:-
<input type=“radio” name=“radioname” value=“Default value”
checked> TextToDisplay </input>
List box Syntax:-
<select name=“List name” size=“value”>
<option>Value1</option>
<option>Value1</option>
</select>
Submit Button Syntax:-
<input type=“submit” >
Image Button Syntax:-
<input type=“image” SRC=“URL”>
Syntax:
$VariableName = $_GET[“fieldname”];
24 | P a g e
Web Development using PHP (4341604)
Disadvantages:-
Information trying to pass to destination file is visible in URL so it is Insecure.
Transfer limited amount of Information.
GET can't be used to send binary data, like images or word documents, to the
server.
The GET method is restricted to send up to 1024 characters only.
Submit form using POST method:
The POST method transfers information via HTTP headers.
It will transfer Information between pages through FORM body.
The POST method does not have any restriction on data size to be sent.
The POST method can be used to send ASCII as well as binary data.
The data sent by POST method goes through HTTP header so security depends on
HTTP protocol. By using Secure HTTP you can make sure that your information is
secure.
The PHP provides $_POST associative array to access all the sent information using
POST method
Information is transferred through FORM body, not visible to everyone. Hence
secure method.
It allows you to transfer larger amount of data.
Syntax:
$VariableName = $_POST[“fieldname”];
Syntax:
$VariableName = $_REQUEST[“fieldname”];
25 | P a g e
Web Development using PHP (4341604)
H. Resources/Equipment Required
Sr. Instrument/Equipment/
Specification Quantity
No. Components/Trainer kit
Computer (i3-i5 preferable), RAM
1 Hardware: Computer System
minimum 2 GB and onwards
2 Operating System Windows/ Linux/ MAC
XAMPP server (PHP, Web server, As Per
3 Software
Database) Batch Size
Notepad, Notepad++, Sublime Text or
4 Text Editor
similar
NA
J. Source code:
A) Create student registration form using text box, check box, radio button, select,
submit button. And display user inserted value in new PHP page using GET or POST
Method.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$enrollment = $_POST["enroll"];
$mobile = $_POST["mono"];
$gender = $_POST["gender"];
$language = $_POST["language"];
$courses = isset($_POST["Course"]) ? $_POST["Course"] : array();
echo "<p>Name: $name</p>";
echo "<p>Enrollment No: $enrollment</p>";
echo "<p>Mobile No: $mobile</p>";
echo "<p>Gender: $gender</p>";
echo "<p>Language: $language</p>";
if (!empty($courses)) {
echo "<p>Courses:</p>";
echo "<ul>";
foreach ($courses as $course)
{ echo "<li>$course</li>";
}echo "</ul>";
} else {
echo "<p>No courses selected</p>";
}else {
echo "<p>No data submitted.</p>";
}
?>
26 | P a g e
Web Development using PHP (4341604)
<html>
<head>
<title>Student Registration Form</title>
</head>
<body>
<h2>Student Registration Form</h2>
<form action="pr8.php" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="Enroll">Enrollment No:</label>
<input type="text" id="enroll" name="enroll" required><br><br>
<label for="mono">Mobile No:</label>
<input type="text" id="mono" name="mono" required><br><br>
<label for="gender">Gender:</label>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br><br>
<label for="language">Select language:</label>
<select id="language" name="language">
<option value="Gujarati">Gujarati</option>
<option value="Hindi">Hindi</option>
<option value="Marathi">Marathi</option>
<option value="English">English</option>
</select><br><br>
<label for="course">Course:</label><br>
<input type="checkbox" id="IT" name="Course[]" value="IT">
<label for="IT"> IT</label><br>
<input type="checkbox" id="Computer" name="Course[]"
value="Computer">
<label for="Computer">Computer</label><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
27 | P a g e
Web Development using PHP (4341604)
<!DOCTYPE html>
<html lang="en">
<head>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_REQUEST["languages"])) {
echo "<h3>Your favorite Coding Languages:</h3>";
echo "<ul>";
foreach ($_REQUEST["languages"] as $language)
{ echo "<li>$language</li>";
}
echo "</ul>";
} else {
echo "<p>No programming languages selected.</p>";
}
}
?>
</body>
</html>
28 | P a g e
Web Development using PHP (4341604)
OUTPUT:
1. variable is used to collect form data sent with both the GET and POSTmethods.
5. For password input in form, you should use attribute and value
of <input> tag.
29 | P a g e
Web Development using PHP (4341604)
A. References / Suggestions
1) https://www.w3schools.com/php/default.asp
2) https://www.guru99.com/php-tutorials.html
3) https://www.tutorialspoint.com/php/
4) https://tutorialehtml.com/en/php-tutorial-introduction/
5) www.tizag.com/phpT/
6) https://books.goalkicker.com/PHPBook/
7) https://spoken-tutorial.org/tutorial- search/?
search_foss=PHP+and+MySQL&search_language=English
8) https://codecourse.com/watch/php-basics
9) https://onlinecourses.swayam2.ac.in/aic20_sp32/preview
B. Assessment-Rubrics
Faculty
Marks Obtained Date
Signature
Program Implementation Student’s engagement
Correctness and Presentation in practical activities Total
(4) Methodology (3) (3) (10)
R1 R2 R3
96 | P a g e