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

PHP 1 - 6 Programs

This document provides examples of PHP programs that take input in different ways and perform basic string and number operations. The programs demonstrate: 1) Taking fixed input values within the program and printing the table of a given number. 2) Taking variable input through an HTML form using the POST method and calculating the length and reverse of a user-entered string. 3) Taking input through the GET method in the URL and checking if a word is a palindrome. 4) Additional examples include finding the greatest of three numbers, and counting the words in a sentence.

Uploaded by

ASH GAMING Games
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

PHP 1 - 6 Programs

This document provides examples of PHP programs that take input in different ways and perform basic string and number operations. The programs demonstrate: 1) Taking fixed input values within the program and printing the table of a given number. 2) Taking variable input through an HTML form using the POST method and calculating the length and reverse of a user-entered string. 3) Taking input through the GET method in the URL and checking if a word is a palindrome. 4) Additional examples include finding the greatest of three numbers, and counting the words in a sentence.

Uploaded by

ASH GAMING Games
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PHP - PRACTICALS

Input to any program can be given in 3 ways :


a)Fixed value through program.(Fixed value stored in a variable)
b)Variable input through Form.(Post method, 2 programs)
c)Online input.(Get method)

Note : All programs should be saved in c:wamp/www/filename.html

1 Print table of any given number :

(FIXED VALUE METHOD)

// Roll No : Name : Practical No :

<HTML>
<HEAD><TITLE> PHP program on Table </TITLE></HEAD>
<BODY>
<?PHP

$t = 7;
$i = 1;
print "Print the table of given No. $t <BR><BR>";
for ($i; $i <= 10; $i++)

{
print "$t * $i = ".($t * $i). "<BR>";
}
?>
</BODY>
</HTML>

2 To find factorial of a given no. using while loop

// Roll No : Name : Practical No :

<HTML>
<HEAD><TITLE>To find factorial of a given no. using while loop
</TITLE></HEAD>
<BODY>
<?php

$num1 = 5;
$fact = 1;
$counter = 1;

While ( $counter <= 5 )


{
$fact = ( $fact * $counter );
$counter++;
}

echo "To find factorial of a given no. $num1 <BR><BR>";


print "Factorial of $num1 is $fact";
?>
</BODY>
</HTML>

3 Variable input through Form.(Post method, 2 programs)

// Roll No : Name : Practical No :

// STRING LENGTH & REVERSE STRING


a) filename.html rsinpstring.html
<HTML><HEAD><TITLE>STRING LENGTH & REVERSE
STRING</TITLE></HEAD>
<BODY>
<FORM ACTION ="rstring.php" METHOD ="POST">
Enter a string : <input type = "text" name = string><BR><BR>
<input type = "submit" value = "Manipulate">
</FORM>
</BODY>
</HTML>

Filename.php – rspstring.php
b)
<?php
$s1 = $_POST["string"];
$s2 = strlen($s1);
$s3 = strrev($s1);
echo "The given string is : $s1 <br><br>";
echo "The length of the string is : $s2<br><br>";
echo "The reverse string is : $s3<br><br>";
?>

4 Check word is Palindrome or not a Palindrome

// Roll No : Name : Practical No :

Palindrome using online (get method)


// online input by given name of the program followed by ?string=nitin
// NITIN, Malayalam , madam, Rotator, mom, noon

<?php
$word=$_GET[‘string’];
$x=strrev($word);

if ($x==$word)
{
echo “The given word $word is palindrome”;
}
else
{
echo “The given word $word is NOT palindrome”;
}
5. To find greatest of three given nos. rsgreat.php

// Roll No : Name : Practical No :

<HTML>
<HEAD><TITLE>To find greatest of three given nos. </TITLE></HEAD>
<BODY>
<?PHP

$a = 50; $b = 100; $c = 80;


echo "The given three numbers are $a, $b, $c <BR><BR>";
if ($a > $b and $a > $c)
{
echo "Greatest of three no. is $a";
}
if ($b > $c and $b > $a)
{
echo "Greatest of three no. is $b";
}
if ($c > $a and $c > $b)
{
echo "Greatest of three no. is $c";
}
?>
</BODY>
</HTML>

6 Print number of words in a sentence

<?php

// enter a sentence in the variable name $test

$test = "counting of words in a sentence.";


echo "<BR><BR> Input string is : ".$test;
echo "<BR> <BR> Number of Words in a string are : ".str_word_count($test);
?>

You might also like