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

PHP Practical Manual

This document provides examples and exercises for learning PHP programming. It begins with examples of PHP scripts to display PHP and server configuration information, strings, and accept user input. Subsequent exercises demonstrate PHP functions and syntax for tasks like retrieving the client IP address, detecting the browser, displaying values in tables, reading file contents, testing number ranges with ternary operators, and more advanced concepts like finding prime numbers and pairs of numbers that sum to a given total. The goal is to introduce students to PHP programming.

Uploaded by

Fasoro Ayodeji
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
234 views

PHP Practical Manual

This document provides examples and exercises for learning PHP programming. It begins with examples of PHP scripts to display PHP and server configuration information, strings, and accept user input. Subsequent exercises demonstrate PHP functions and syntax for tasks like retrieving the client IP address, detecting the browser, displaying values in tables, reading file contents, testing number ranges with ternary operators, and more advanced concepts like finding prime numbers and pairs of numbers that sum to a given total. The goal is to introduce students to PHP programming.

Uploaded by

Fasoro Ayodeji
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Course Title: COMPUTER PACKAGES II

Course Code: COM 215


Goal: This course is designed to enable the student to acquire a better understanding and
introduction to PHP programming

WEEK ONE

Example one

Write a PHP script to get the PHP version and configuration information.

<?php
phpinfo();
?>

Example Two

Write a PHP script to display the following strings.


Sample String :
'Tomorrow I \'ll learn PHP global variables.'
'This is a bad command : del c:\\*.*'

<?php
echo "Tomorrow I \'ll learn PHP global variables."."\n";
echo "This is a bad command : del c:\\*.*"."\n";
?>

EXERCISE

Write a PHP program for the following variable declarations

I. String
II. Float
III. Double
IV.

$var = 'PHP Tutorial'. Put this variable into the title section, h3 tag and as an
anchor text within an HTML document.

<?php
$var = 'PHP Tutorial';
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title><?php echo $var; ?> - W3resource!</title>
</head>
<body>
<h3><?php echo $var; ?></h3>
<p>PHP, an acronym for Hypertext Preprocessor, is a widely-used open
source general-purpose scripting language. It is a cross-platform,
HTML embedded server-side scripting language and is especially suited
for web development.</p>
<p><a href="https://www.w3resource.com/php/php-home.php">Go to the
<?php echo $var; ?></a>.</p>
</body>
</html>

Create a simple HTML form and accept the user name and display the name
through PHP echo statement

Write a PHP script to get the client IP address.

<?php
//whether ip is from share internet
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip_address = $_SERVER['HTTP_CLIENT_IP'];
}
//whether ip is from proxy
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
//whether ip is from remote address
else
{
$ip_address = $_SERVER['REMOTE_ADDR'];
}
echo $ip_address;
?>

Write a simple PHP browser detection script.

<?php
echo "Your User Agent is :" . $_SERVER ['HTTP_USER_AGENT'];

?>

Write a e PHP script to display string, values within a table.


Note : Use HTML table elements into echo.
Expected Output :

<?php
$a=1000;
$b=1200;
$c=1400;
echo "<table border=1 cellspacing=0 cellpading=0>
<tr> <td><font color=blue>Salary of Mr. A is</td>
<td>$a$</font></td></tr>
<tr> <td><font color=blue>Salary of Mr. B is</td>
<td>$b$</font></td></tr>
<tr> <td><font color=blue>Salary of Mr. C is</td>
<td>$c$</font></td></tr>
</table>";
?>
Write a PHP script to display source code of a webpage (e.g.
"http://www.example.com/").

<?php
$all_lines = file('https://www.w3resource.com/');
foreach ($all_lines as $line_num => $line)
{
echo "Line No.-{$line_num}: " . htmlspecialchars($line) . "\n";
}
?>
Write a PHP script to count number of lines in a file.
Note : Store a text file name into a variable and count the number of lines of text
it has.

<?php
$file = basename($_SERVER['PHP_SELF']);
$no_of_lines = count(file($file));
echo "There are $no_of_lines lines in $file"."\n";
?>

Write a PHP script to print current PHP version.


Note : Do not use phpinfo() function.

<?php
echo 'Current PHP version : ' . phpversion();
// prints e.g. '2.0' or nothing if the extension isn't enabled
echo phpversion('tidy')."\n";
?>

Write a PHP function to test whether a number is greater than 30, 20 or 10 using
ternary operator.

<?php
function trinary_Test($n){
$r = $n > 30
? "greater than 30"
: ($n > 20
? "greater than 20"
: ($n >10
? "greater than 10"
: "Input a number atleast greater than 10!"));
echo $n." : ".$r."\n";
}
trinary_Test(32);
trinary_Test(21);
trinary_Test(12);
trinary_Test(4);
?>

Write a PHP script to get the document root directory under which the current
script is executing, as defined in the server's configuration file.

<?php
// getenv() gets the value of an environment variable
$rd = getenv('DOCUMENT_ROOT');
echo $rd."\n";
?>

Write a PHP program to swap two variables


define swap(a, b)
temp := a
a := b
b := temp

Write a PHP program to remove duplicates from a sorted list.


Input: (1,1,2,2,3,4,5,5)
Output: (1,2,3,4,5)

<?php
function remove_duplicates_list($list1) {
$nums_unique = array_values(array_unique($list1));
return $nums_unique ;
}
$nums = array(1,1,2,2,3,4,5,5);
print_r(remove_duplicates_list($nums));
?>

Write a PHP program to compute the sum of the prime numbers less than 100.
Note: There are 25 prime numbers are there in less than 100.
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
89, 97 and sum of all these numbers is 1060.

<?php
$primes = array();
$is_prime_no = false;
for ($i = 2; $i<100; $i++) {
$is_prime_no = true;
for ($j = 2; $j<=($i/2); $j++) {
if ($i%$j==0) {
$is_prime_no = false;
break;
}
}
if ($is_prime_no) {
array_push($primes,$i);
}
if (count($primes)==100) {
break;
}
}
echo array_sum($primes)."\n";
?>

 Write a PHP program to print out the multiplication table upto 6*6.
Output:

<?php
for ($i = 1; $i < 7; $i++) {
for ($j = 1; $j < 7; $j++) {
if ($j == 1) {
echo str_pad($i*$j, 2, " ", STR_PAD_LEFT);
} else {
echo str_pad($i*$j, 4, " ", STR_PAD_LEFT);
}
}
echo "\n";
}
?>

Write a PHP program to print out the sum of pairs of numbers of a given sorted
array of positive integers which is equal to a given number.

<?php
function find_Pairs($nums, $pair_sum) {
$nums_pairs = "";
for ($i = 0; $i <= count($nums); $i++) {
for ($j = $i + 1; $j < count($nums); $j++) {
if ($nums[$i] + $nums[$j] == (int)$pair_sum) {
$nums_pairs .= $nums[$i] . "," . $nums[$j] . ";";
}
}
}
return $nums_pairs;
}
$nums = array(0,1,2,3,4,5,6);
echo find_Pairs($nums, 7)."\n";
echo find_Pairs($nums, 5)."\n";
?>

You might also like