PHP Program Tutorial
Please leave a remark at the bottom of each page with your useful suggestion.
Table of Contents
PHP Program Collection 1
- The php code can be embedded into html code (index.php)
- Multiline comments program
- To specify a boolean literal, use the keywords TRUE or FALSE. Both are case-insensitive.
- String
- Add to numbers program in php
- Swap two number using third variable program in PHP
- Even Odd number program in PHP
- Here is the Program to list the first 15 prime numbers.
- Factorial of a number in Php
- Armstrong number Program in PHP
- Palindrome number Program in PHP
- Write a PHP program to get the size of a file.
- Write a PHP program to remove duplicates from a sorted list.
- Swap two number using third variable program in PHP
- Program to simple calculator
1. The php code can be embedded into html code (index.php)
<html>
<head>
<title> Basic Example </title>
</head>
<body>
<?php
echo "This is php code embedded into html";
?>
</body>
</html>
similarly html code can be embedded in php code as follows -
example.php
<?php
echo "<b>"."using html in php"."</b>";
?>
2. Multiline comments program
<?php
/* This is a comment with multiline
Purpose: Multiline Comments Demo
Subject: PHP
*/
echo “An example with multi line comments”;
?>
3. To specify a boolean literal, use the keywords TRUE or FALSE. Both are case-insensitive.
<?php
$foo=True; //assign the value TRUE to $foo variable
?>
4. String
<?php
$variable = “name”;
$literally = ‘My $variable will not print!\\n’;
print($literally);
$literally = “My $variable will print!\\n”;
print($literally);
?>
This will produce following result:
My name will not print!
My name will print
5. Add to numbers program in php
<?php
$a=10;
$b=20;
$c=$a+$b;
echo "Sum: ",$c;
?>
Output : sum: 30
6. Swap two number using third variable program in PHP
<?php
$a=10;
$b=20;
echo "Value of a: $a</br>";
echo "Value of b: $b</br>";
$temp=$a;
$a=$b;
$b=$temp;
echo "Value of a: $a</br>";
echo "Value of b: $b</br>";
?>
Output:
Value of a: 10
Value of b: 20
Value of a: 20
Value of b: 10
7. Even Odd number program in PHP
<?php
$num=10;
if($num%2==0)
{
echo "Even number";
}
else
{
echo "Odd number";
}
?>
Output:
Even number
8. Here is the Program to list the first 15 prime numbers.
<?php
$count = 0;
$num = 2;
while ($count < 15 )
{
$div_count=0;
for ( $i=1; $i<=$num; $i++)
{
if (($num%$i)==0)
{
$div_count++;
}
}
if ($div_count<3)
{
echo $num." , ";
$count=$count+1;
}
$num=$num+1;
}
?>
9. Factorial of a number in Php
<?php
$num = 4;
$factorial = 1;
for($x=$num; $x>=1; $x--)
{
$factorial = $factorial * $x;
}
echo "Factorial of $num is $factorial";
?>
Output: Factorial of 4 is 24
10. Armstrong number Program in PHP
<?php
$num=153;
$sum=0;
$temp=$num;
while($temp!=0)
{
$rem=$temp%10;
$sum=$sum +$rem*$rem*$rem; $temp=$temp/10;
}
if($num==$sum)
{
echo "Armstrong number";
}
else
{
echo "not an armstrong number";
}
?>
11. Palindrome number Program in PHP
<?php
$num = 121;
$p=$num;
$revnum = 0;
while ($num != 0)
{
$revnum = $revnum * 10 + $num % 10;
$num = (int)($num / 10);
}
if($revnum==$p)
{
echo $p," is Palindrome number";
}
else
{
echo $p." is not Palindrome number";
}
?>
12. Write a PHP program to get the size of a file.
<?php
$myfile = fopen("/home/students/ppp.txt", "w") or die("Unable to open file!");
$txt = "PHP Exercises\n"; fwrite($myfile, $txt);
$txt = "from\n"; fwrite($myfile,$txt);
$txt = "hello World\n"; fwrite($myfile, $txt); fclose($myfile);
echo "Size of the file".filesize("/home/students/ppp.txt")."\n";
?>
13. Write a PHP program to remove duplicates from a sorted list.
<?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));
?>
14. Swap two number using third variable program in PHP
<?php
$a=10;
$b=20;
echo "Value of a: $a</br>";
echo "Value of b: $b</br>";
$temp=$a;
$a=$b; $b=$temp;
echo "Value of a: $a</br>";
echo "Value of b: $b</br>";
?>
15. Program to simple calculator
<?php
if(isset($_POST['sub']))
{
$txt1=$_POST['n1'];
$txt2=$_POST['n2'];
$oprnd=$_POST['sub'];
if($oprnd=="+")
$res=$txt1+$txt2;
else if($oprnd=="-")
$res=$txt1-$txt2;
else if($oprnd=="x")
$res=$txt1*$txt2;
else if($oprnd=="/")
$res=$txt1/$txt2;
}
?>
<form method="post" action="">
Calculator
<br>
No1:<input name="n1" value="<?php echo $txt1; ?>"> <br>
No2:<input name="n2" value="<?php echo $txt2; ?>"> <br>
Res:<input name="res" value="<?php echo $res; ?>">
<br>
<input type="submit" name="sub" value="+">
<input type="submit" name="sub" value="-">
<input type="submit" name="sub" value="x">
<input type="submit" name="sub" value="/">
</form>