PHP Practical Manual
PHP Practical Manual
WEEK ONE
Example one
Write a PHP script to get the PHP version and configuration information.
<?php
phpinfo();
?>
Example Two
<?php
echo "Tomorrow I \'ll learn PHP global variables."."\n";
echo "This is a bad command : del c:\\*.*"."\n";
?>
EXERCISE
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
<?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;
?>
<?php
echo "Your User Agent is :" . $_SERVER ['HTTP_USER_AGENT'];
?>
<?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";
?>
<?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";
?>
<?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";
?>