SlideShare a Scribd company logo
How to run PHP code in XAMPP
Generally, a PHP file contains HTML tags and some PHP scripting code. It is very easy to
create a simple PHP example. To do so, create a file and write HTML tags + PHP code and
save this file with .php extension.
Note: PHP statements ends with semicolon (;).
All PHP code goes between the php tag. It starts with <?php and ends with ?>. The syntax of
PHP tag is given below:
<?php
//your code here
?>
Let's see a simple PHP example where we are writing some text using PHP echo command.
<!DOCTYPE>
<html>
<body>
<?php
echo "<h2>Hello First PHP</h2>";
?>
</body>
</html>
How to run PHP programs in XAMPP
How to run PHP programs in XAMPP PHP is a popular backend programming language.
PHP programs can be written on any editor, such as - Notepad, Notepad++, Dreamweaver,
etc. These programs save with .php extension, i.e., filename.php inside the htdocs folder.
For example - p1.php.
As I'm using window, and my XAMPP server is installed in D drive. So, the path for the
htdocs directory will be "D:xampphtdocs".
PHP program runs on a web browser such as - Chrome, Internet Explorer, Firefox, etc. Below
some steps are given to run the PHP programs.
Step 1: Create a simple PHP program like hello world.
<?php
echo "Hello World!";
?>
Step 2: Save the file with hello.php name in the htdocs folder, which resides inside the
xampp folder.
Step 3: Run the XAMPP server and start the Apache and MySQL.
Step 4: Now, open the web browser and type localhost http://localhost/hello.php on your
browser window.
Step 5: The output for the above hello.php program will be shown as the screenshot below:
PHP Case Sensitivity
In PHP, keyword (e.g., echo, if, else, while), functions, user-defined functions, classes are not
case-sensitive. However, all variable names are case-sensitive.
In the below example, you can see that all three echo statements are equal and valid:
<!DOCTYPE>
<html>
<body>
<?php
echo "Hello world using echo </br>";
ECHO "Hello world using ECHO </br>";
EcHo "Hello world using EcHo </br>";
?>
</body>
</html>
Look at the below example that the variable names are case sensitive. You can see the
example below that only the second statement will display the value of the $color
variable. Because it treats $color, $ColoR, and $COLOR as three different variables:
<html>
<body>
<?php
$color = "black";
echo "My car is ". $ColoR ."</br>";
echo "My dog is ". $color ."</br>";
echo "My Phone is ". $COLOR ."</br>";
?>
</body>
</html>
PHP Echo
PHP echo is a language construct, not a function. Therefore, you don't need to use parenthesis
with it. But if you want to use more than one parameter, it is required to use parenthesis.
The syntax of PHP echo is given below:
void echo (string $arg1 [, string $... ] )
PHP echo statement can be used to print the string, multi-line strings, escaping characters,
variable, array, etc. Some important points that you must know about the echo statement are:
o echo is a statement, which is used to display the output.
o echo can be used with or without parentheses: echo(), and echo.
o echo does not return any value.
o We can pass multiple strings separated by a comma (,) in echo.
o echo is faster than the print statement.
PHP echo: printing string
<?php
echo "Hello by PHP echo";
?>
PHP echo: printing multi line string
<?php
echo "Hello by PHP echo
this is multi line
text printed by
PHP echo statement
";
?>
PHP echo: printing escaping characters
<?php
echo "Hello escape "sequence" characters";
?>
PHP echo: printing variable value
<?php
$msg="Hello JavaTpoint PHP";
echo "Message is: $msg";
?>
PHP Variable: Declaring string, integer, and float
<?php
$str="hello string";
$x=200;
$y=44.6;
echo "string is: $str <br/>";
echo "integer is: $x <br/>";
echo "float is: $y <br/>";
?>
ODD or EVEN
<?php
$number=12;
if($number%2==0)
{
echo "$number is Even Number";
}
else
{
echo "$number is Odd Number";
}
?>
Adding Two Numbers
<?php
$x=15;
$y=30;
$z=$x+$y;
echo "Sum: ",$z;
?>
Sum of Digits
To find sum of digits of a number just add all the digits.
For example,
14597 = 1 + 4 + 5 + 9 + 7
14597 = 26
Logic:
o Take the number.
o Divide the number by 10.
o Add the remainder to a variable.
o Repeat the process until remainder is 0.
Example:
<?php
$num = 14597;
$sum=0; $rem=0;
for ($i =0; $i<=strlen($num);$i++)
{
$rem=$num%10;
$sum = $sum + $rem;
$num=$num/10;
}
echo "Sum of digits 14597 is $sum";
?>
Table of Number
A table of a number can be printed using a loop in program.
Logic:
o Define the number.
o Run for loop.
o Multiply the number with for loop output.
Example:
<?php
define('a', 7);
for($i=1; $i<=10; $i++)
{
echo $i*a;
echo '<br>';
}
?>
Factorial Program
The factorial of a number n is defined by the product of all the digits from 1 to n
(including 1 and n).
For example,
4! = 4*3*2*1 = 24
6! = 6*5*4*3*2*1 = 720
Note:
o It is denoted by n! and is calculated only for positive integers.
o Factorial of 0 is always 1.
The simplest way to find the factorial of a number is by using a loop.
There are two ways to find factorial in PHP:
o Using loop
o Using recursive method
Logic:
o Take a number.
o Take the descending positive integers.
o Multiply them.
Example:
<?php
$num = 4;
$factorial = 1;
for ($x=$num; $x>=1; $x--)
{
$factorial = $factorial * $x;
}
echo "Factorial of $num is $factorial";
?>
Factorial using Recursion in PHP
Factorial of 6 using recursion method is shown.
Example:
<?php
function fact ($n)
{
if($n <= 1)
{
return 1;
}
else
{
return $n * fact($n - 1);
}
}
echo "Factorial of 6 is " .fact(6);
?>
Armstrong Number
An Armstrong number is the one whose value is equal to the sum of the cubes of its
digits.
0, 1, 153, 371, 407, 471, etc are Armstrong numbers.
For example,
407 = (4*4*4) + (0*0*0) + (7*7*7)
= 64 + 0 + 343
407 = 407
Logic:
o Take the number.
o Store it in a variable.
o Take a variable for sum.
o Divide the number with 10 until quotient is 0.
o Cube the remainder.
o Compare sum variable and number variable.
Armstrong number in PHP
Below program checks whether 407 is Armstrong or not.
Example:
<?php
$num=407;
$total=0;
$x=$num;
while($x!=0)
{
$rem=$x%10;
$total=$total+$rem*$rem*$rem;
$x=$x/10;
}
if($num==$total)
{
echo "Yes it is an Armstrong number";
}
else
{
echo "No it is not an armstrong number";
}
?>
Fibonacci Series
Fibonacci series is the one in which you will get your next term by adding previous
two numbers.
For example,
0 1 1 2 3 5 8 13 21 34
Here, 0 + 1 = 1
1 + 1 = 2
3 + 2 = 5
and so on.
Logic:
o Initializing first and second number as 0 and 1.
o Print first and second number.
o From next number, start your loop. So third number will be the sum of the first two
numbers.
Example:
<?php
$num = 0;
$n1 = 0;
$n2 = 1;
echo "<h3>Fibonacci series for first 12 numbers: </h3>";
echo "n";
echo $n1.' '.$n2.' ';
while ($num < 10 )
{
$n3 = $n2 + $n1;
echo $n3.' ';
$n1 = $n2;
$n2 = $n3;
$num = $num + 1;
?>
Fibonacci series using Recursive function
Recursion is a phenomenon in which the recursion function calls itself until the base
condition is reached.
<?php
/* Print fiboancci series upto 12 elements. */
$num = 12;
echo "<h3>Fibonacci series using recursive function:</h3>";
echo "n";
/* Recursive function for fibonacci series. */
function series($num){
if($num == 0){
return 0;
}else if( $num == 1){
return 1;
} else {
return (series($num-1) + series($num-2));
}
}
/* Call Function. */
for ($i = 0; $i < $num; $i++){
echo series($i);
echo "n";
}
Reverse number
A number can be written in reverse order.
For example
12345 = 54321
Logic:
o Declare a variable to store reverse number and initialize it with 0.
o Multiply the reverse number by 10, add the remainder which comes after dividing the
number by 10.
Reversing Number in PHP
Example:
Below progrem shows digits reversal of 23456.
<?php
$num = 23456;
$revnum = 0;
while ($num > 1)
{
$rem = $num % 10;
$revnum = ($revnum * 10) + $rem;
$num = ($num / 10);
}
echo "Reverse number of 23456 is: $revnum";
?>
Reverse String
A string can be reversed either using strrev() function or simple PHP code.
For example, on reversing JAVATPOINT it will become TNIOPTAVAJ.
Logic:
o Assign the string to a variable.
o Calculate length of the string.
o Declare variable to hold reverse string.
o Run for loop.
o Concatenate string inside for loop.
o Display reversed string.
Reverse String using strrev() function
A reverse string program using strrev() function is shown.
Example:
<?php
$string = "JAVATPOINT";
echo "Reverse string of $string is " .strrev ( $string );
?>
Swapping two numbers
Two numbers can be swapped or interchanged. It means first number will become
second and second number will become first.
For example
a = 20, b = 30
After swapping,
a = 30, b = 20
There are two methods for swapping:
o By using third variable.
o Without using third variable.
Swapping Using Third Variable
Swap two numbers 45 and 78 using a third variable.
Example:
<?php
$a = 45;
$b = 78;
// Swapping Logic
$third = $a;
$a = $b;
$b = $third;
echo "After swapping:<br><br>";
echo "a =".$a." b=".$b;
?>
Swapping Without using Third Variable
Swap two numbers without using a third variable is done in two ways:
o Using arithmetic operation + and ?
o Using arithmetic operation * and /
Example for (+ and -):
<?php
$a=234;
$b=345;
//using arithmetic operation
$a=$a+$b;
$b=$a-$b;
$a=$a-$b;
echo "Value of a: $a</br>";
echo "Value of b: $b</br>";
?>
Area of Triangle
Area of a triangle is calculated by the following Mathematical formula,
(base * height) / 2 = Area
Area of Triangle in PHP
Program to calculate area of triangle with base as 10 and height as 15 is shown.
Example:
<?php
$base = 10;
$height = 15;
echo "area with base $base and height $height= " . ($base * $height) / 2;
?>
Area of a Rectangle
Area of a rectangle is calculated by the mathematical formula,
1. Length ∗ Breadth = Area
Logic:
o Take two variables.
o Multiply both of them.
Area of Rectangle in PHP
Program to calculate area of rectangle with length as 14 and width as 12 is shown.
Example:
Advertisement
<?php
$length = 14;
$width = 12;
echo "area of rectangle is $length * $width= " . ($length * $width) . "<br />";
?>
Leap Year Program
A leap year is the one which has 366 days in a year. A leap year comes after every
four years. Hence a leap year is always a multiple of four.
For example, 2016, 2020, 2024, etc are leap years.
Leap Year Program
This program states whether a year is leap year or not from the specified range of
years (1991 - 2016).
Example:
<?php
function isLeap($year)
{
return (date('L', mktime(0, 0, 0, 1, 1, $year))==1);
}
//For testing
for($year=1991; $year<2016; $year++)
{
If (isLeap($year))
{
echo "$year : LEAP YEAR<br />n";
}
else
{
echo "$year : Not leap year<br />n";
}
}
?>
Alphabet Triangle Pattern
Some different alphabet triangle patterns using range() function in PHP are shown
below.
<?php
$alpha = range('A', 'Z');
for($i=0; $i<5; $i++){
for($j=5; $j>$i; $j--){
echo $alpha[$i];
}
echo "<br>";
}
?>
Star Triangle
The star triangle in PHP is made using for and foreach loop. There are a lot of star
patterns. We'll show some of them here.
Pattern 1
<?php
for($i=0;$i<=5;$i++){
for($j=5-$i;$j>=1;$j--){
echo "* ";
}
echo "<br>";
}
?>
Output:
Pattern 2
<?php
for($i=0;$i<=5;$i++){
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
?>
Output:
Pattern 3
<?php
for($i=0;$i<=5;$i++){
for($k=5;$k>=$i;$k--){
echo " ";
}
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
for($i=4;$i>=1;$i--){
for($k=5;$k>=$i;$k--){
echo " ";
}
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
?>
Output:
Pattern 4
<?php
for($i=1; $i<=5; $i++){
for($j=1; $j<=$i; $j++){
echo ' * ';
}
echo '<br>';
}
for($i=5; $i>=1; $i--){
for($j=1; $j<=$i; $j++){
echo ' * ';
}
echo '<br>';
}
?>
Output:
Pattern 5
<?php
for ($i=1; $i<=5; $i++)
{
for ($j=1; $j<=5; $j++)
{
echo '* ';
}
echo "</br>";
}
?>
Output:
Pattern 6
<?php
for($i=5; $i>=1; $i--)
{
if($i%2 != 0)
{
for($j=5; $j>=$i; $j--)
{
echo "* ";
}
echo "<br>";
}
}
for($i=2; $i<=5; $i++)
{
if($i%2 != 0)
{
for($j=5; $j>=$i; $j--)
{
echo "* ";
}
echo "<br>";
}
}
?>

More Related Content

PPT
Web Technology_10.ppt
PPT
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PPTX
PHP FUNCTIONS AND ARRAY.pptx
PPTX
The basics of php for engeneering students
PPTX
unit 1.pptx
DOCX
Free PHP Book Online | PHP Development in India
PPTX
Php.ppt
PPTX
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
Web Technology_10.ppt
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP FUNCTIONS AND ARRAY.pptx
The basics of php for engeneering students
unit 1.pptx
Free PHP Book Online | PHP Development in India
Php.ppt
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College

Similar to How to run PHP code in XAMPP.docx (1).pdf (20)

PPTX
UNIT II (7).pptx
PPTX
UNIT II (7).pptx
PPTX
PHP BASICs Data Type ECHo and PRINT.pptx
PDF
Lecture14-Introduction to PHP-coding.pdf
ODP
Php Learning show
PPSX
Php using variables-operators
PPTX
String variable in php
PPT
PHP - Web Development
PDF
PHP Lec 2.pdfsssssssssssssssssssssssssss
PPTX
PHP Powerpoint -- Teach PHP with this
PDF
lab4_php
PDF
lab4_php
PPTX
Expressions and Operators.pptx
PPT
What Is Php
 
PPTX
php programming.pptx
PPTX
PPT
Php essentials
PDF
Introduction to PHP
PDF
Php a dynamic web scripting language
UNIT II (7).pptx
UNIT II (7).pptx
PHP BASICs Data Type ECHo and PRINT.pptx
Lecture14-Introduction to PHP-coding.pdf
Php Learning show
Php using variables-operators
String variable in php
PHP - Web Development
PHP Lec 2.pdfsssssssssssssssssssssssssss
PHP Powerpoint -- Teach PHP with this
lab4_php
lab4_php
Expressions and Operators.pptx
What Is Php
 
php programming.pptx
Php essentials
Introduction to PHP
Php a dynamic web scripting language
Ad

More from rajeswaria21 (8)

DOCX
internet-of-things-1 .................(1).docx
PDF
internet-of-things-4.........................pdf
PDF
internet-of-things-2.....................pdf
PDF
internet-of-things-5.........................pdf
PDF
internet-of-things-3.....................pdf
PDF
II BCA JAVA PROGRAMMING NOTES FOR FIVE UNITS.pdf
PDF
SQL.......................................pdf
PDF
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
internet-of-things-1 .................(1).docx
internet-of-things-4.........................pdf
internet-of-things-2.....................pdf
internet-of-things-5.........................pdf
internet-of-things-3.....................pdf
II BCA JAVA PROGRAMMING NOTES FOR FIVE UNITS.pdf
SQL.......................................pdf
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
Ad

Recently uploaded (20)

PPTX
Cell Membrane: Structure, Composition & Functions
PPTX
Classification Systems_TAXONOMY_SCIENCE8.pptx
DOCX
Q1_LE_Mathematics 8_Lesson 5_Week 5.docx
PDF
Unveiling a 36 billion solar mass black hole at the centre of the Cosmic Hors...
PDF
Biophysics 2.pdffffffffffffffffffffffffff
PPTX
Introduction to Cardiovascular system_structure and functions-1
PPTX
The KM-GBF monitoring framework – status & key messages.pptx
PDF
Placing the Near-Earth Object Impact Probability in Context
PPTX
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
PPTX
ECG_Course_Presentation د.محمد صقران ppt
PDF
The scientific heritage No 166 (166) (2025)
PPTX
neck nodes and dissection types and lymph nodes levels
PDF
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
PPTX
Introduction to Fisheries Biotechnology_Lesson 1.pptx
PPTX
Derivatives of integument scales, beaks, horns,.pptx
PPTX
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
PPT
The World of Physical Science, • Labs: Safety Simulation, Measurement Practice
PDF
HPLC-PPT.docx high performance liquid chromatography
PDF
lecture 2026 of Sjogren's syndrome l .pdf
PDF
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
Cell Membrane: Structure, Composition & Functions
Classification Systems_TAXONOMY_SCIENCE8.pptx
Q1_LE_Mathematics 8_Lesson 5_Week 5.docx
Unveiling a 36 billion solar mass black hole at the centre of the Cosmic Hors...
Biophysics 2.pdffffffffffffffffffffffffff
Introduction to Cardiovascular system_structure and functions-1
The KM-GBF monitoring framework – status & key messages.pptx
Placing the Near-Earth Object Impact Probability in Context
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
ECG_Course_Presentation د.محمد صقران ppt
The scientific heritage No 166 (166) (2025)
neck nodes and dissection types and lymph nodes levels
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
Introduction to Fisheries Biotechnology_Lesson 1.pptx
Derivatives of integument scales, beaks, horns,.pptx
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
The World of Physical Science, • Labs: Safety Simulation, Measurement Practice
HPLC-PPT.docx high performance liquid chromatography
lecture 2026 of Sjogren's syndrome l .pdf
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf

How to run PHP code in XAMPP.docx (1).pdf

  • 1. How to run PHP code in XAMPP Generally, a PHP file contains HTML tags and some PHP scripting code. It is very easy to create a simple PHP example. To do so, create a file and write HTML tags + PHP code and save this file with .php extension. Note: PHP statements ends with semicolon (;). All PHP code goes between the php tag. It starts with <?php and ends with ?>. The syntax of PHP tag is given below: <?php //your code here ?> Let's see a simple PHP example where we are writing some text using PHP echo command. <!DOCTYPE> <html> <body> <?php echo "<h2>Hello First PHP</h2>"; ?> </body> </html> How to run PHP programs in XAMPP How to run PHP programs in XAMPP PHP is a popular backend programming language. PHP programs can be written on any editor, such as - Notepad, Notepad++, Dreamweaver, etc. These programs save with .php extension, i.e., filename.php inside the htdocs folder. For example - p1.php. As I'm using window, and my XAMPP server is installed in D drive. So, the path for the htdocs directory will be "D:xampphtdocs". PHP program runs on a web browser such as - Chrome, Internet Explorer, Firefox, etc. Below some steps are given to run the PHP programs. Step 1: Create a simple PHP program like hello world. <?php echo "Hello World!"; ?> Step 2: Save the file with hello.php name in the htdocs folder, which resides inside the xampp folder. Step 3: Run the XAMPP server and start the Apache and MySQL.
  • 2. Step 4: Now, open the web browser and type localhost http://localhost/hello.php on your browser window. Step 5: The output for the above hello.php program will be shown as the screenshot below: PHP Case Sensitivity In PHP, keyword (e.g., echo, if, else, while), functions, user-defined functions, classes are not case-sensitive. However, all variable names are case-sensitive. In the below example, you can see that all three echo statements are equal and valid: <!DOCTYPE> <html> <body> <?php echo "Hello world using echo </br>"; ECHO "Hello world using ECHO </br>"; EcHo "Hello world using EcHo </br>"; ?> </body> </html> Look at the below example that the variable names are case sensitive. You can see the example below that only the second statement will display the value of the $color variable. Because it treats $color, $ColoR, and $COLOR as three different variables: <html> <body> <?php $color = "black"; echo "My car is ". $ColoR ."</br>"; echo "My dog is ". $color ."</br>"; echo "My Phone is ". $COLOR ."</br>"; ?> </body> </html>
  • 3. PHP Echo PHP echo is a language construct, not a function. Therefore, you don't need to use parenthesis with it. But if you want to use more than one parameter, it is required to use parenthesis. The syntax of PHP echo is given below: void echo (string $arg1 [, string $... ] ) PHP echo statement can be used to print the string, multi-line strings, escaping characters, variable, array, etc. Some important points that you must know about the echo statement are: o echo is a statement, which is used to display the output. o echo can be used with or without parentheses: echo(), and echo. o echo does not return any value. o We can pass multiple strings separated by a comma (,) in echo. o echo is faster than the print statement. PHP echo: printing string <?php echo "Hello by PHP echo"; ?> PHP echo: printing multi line string <?php echo "Hello by PHP echo this is multi line text printed by PHP echo statement "; ?> PHP echo: printing escaping characters <?php echo "Hello escape "sequence" characters"; ?> PHP echo: printing variable value <?php $msg="Hello JavaTpoint PHP"; echo "Message is: $msg"; ?>
  • 4. PHP Variable: Declaring string, integer, and float <?php $str="hello string"; $x=200; $y=44.6; echo "string is: $str <br/>"; echo "integer is: $x <br/>"; echo "float is: $y <br/>"; ?> ODD or EVEN <?php $number=12; if($number%2==0) { echo "$number is Even Number"; } else { echo "$number is Odd Number"; } ?> Adding Two Numbers <?php $x=15; $y=30; $z=$x+$y; echo "Sum: ",$z; ?> Sum of Digits To find sum of digits of a number just add all the digits. For example, 14597 = 1 + 4 + 5 + 9 + 7
  • 5. 14597 = 26 Logic: o Take the number. o Divide the number by 10. o Add the remainder to a variable. o Repeat the process until remainder is 0. Example: <?php $num = 14597; $sum=0; $rem=0; for ($i =0; $i<=strlen($num);$i++) { $rem=$num%10; $sum = $sum + $rem; $num=$num/10; } echo "Sum of digits 14597 is $sum"; ?> Table of Number A table of a number can be printed using a loop in program. Logic: o Define the number. o Run for loop. o Multiply the number with for loop output. Example: <?php define('a', 7); for($i=1; $i<=10; $i++) { echo $i*a; echo '<br>'; } ?> Factorial Program
  • 6. The factorial of a number n is defined by the product of all the digits from 1 to n (including 1 and n). For example, 4! = 4*3*2*1 = 24 6! = 6*5*4*3*2*1 = 720 Note: o It is denoted by n! and is calculated only for positive integers. o Factorial of 0 is always 1. The simplest way to find the factorial of a number is by using a loop. There are two ways to find factorial in PHP: o Using loop o Using recursive method Logic: o Take a number. o Take the descending positive integers. o Multiply them. Example: <?php $num = 4; $factorial = 1; for ($x=$num; $x>=1; $x--) { $factorial = $factorial * $x; } echo "Factorial of $num is $factorial"; ?> Factorial using Recursion in PHP Factorial of 6 using recursion method is shown. Example: <?php function fact ($n) { if($n <= 1) { return 1;
  • 7. } else { return $n * fact($n - 1); } } echo "Factorial of 6 is " .fact(6); ?> Armstrong Number An Armstrong number is the one whose value is equal to the sum of the cubes of its digits. 0, 1, 153, 371, 407, 471, etc are Armstrong numbers. For example, 407 = (4*4*4) + (0*0*0) + (7*7*7) = 64 + 0 + 343 407 = 407 Logic: o Take the number. o Store it in a variable. o Take a variable for sum. o Divide the number with 10 until quotient is 0. o Cube the remainder. o Compare sum variable and number variable. Armstrong number in PHP Below program checks whether 407 is Armstrong or not. Example: <?php $num=407; $total=0; $x=$num; while($x!=0) { $rem=$x%10; $total=$total+$rem*$rem*$rem;
  • 8. $x=$x/10; } if($num==$total) { echo "Yes it is an Armstrong number"; } else { echo "No it is not an armstrong number"; } ?> Fibonacci Series Fibonacci series is the one in which you will get your next term by adding previous two numbers. For example, 0 1 1 2 3 5 8 13 21 34 Here, 0 + 1 = 1 1 + 1 = 2 3 + 2 = 5 and so on. Logic: o Initializing first and second number as 0 and 1. o Print first and second number. o From next number, start your loop. So third number will be the sum of the first two numbers. Example: <?php $num = 0; $n1 = 0; $n2 = 1; echo "<h3>Fibonacci series for first 12 numbers: </h3>"; echo "n"; echo $n1.' '.$n2.' '; while ($num < 10 )
  • 9. { $n3 = $n2 + $n1; echo $n3.' '; $n1 = $n2; $n2 = $n3; $num = $num + 1; ?> Fibonacci series using Recursive function Recursion is a phenomenon in which the recursion function calls itself until the base condition is reached. <?php /* Print fiboancci series upto 12 elements. */ $num = 12; echo "<h3>Fibonacci series using recursive function:</h3>"; echo "n"; /* Recursive function for fibonacci series. */ function series($num){ if($num == 0){ return 0; }else if( $num == 1){ return 1; } else { return (series($num-1) + series($num-2)); } } /* Call Function. */ for ($i = 0; $i < $num; $i++){ echo series($i); echo "n"; } Reverse number A number can be written in reverse order. For example 12345 = 54321 Logic:
  • 10. o Declare a variable to store reverse number and initialize it with 0. o Multiply the reverse number by 10, add the remainder which comes after dividing the number by 10. Reversing Number in PHP Example: Below progrem shows digits reversal of 23456. <?php $num = 23456; $revnum = 0; while ($num > 1) { $rem = $num % 10; $revnum = ($revnum * 10) + $rem; $num = ($num / 10); } echo "Reverse number of 23456 is: $revnum"; ?> Reverse String A string can be reversed either using strrev() function or simple PHP code. For example, on reversing JAVATPOINT it will become TNIOPTAVAJ. Logic: o Assign the string to a variable. o Calculate length of the string. o Declare variable to hold reverse string. o Run for loop. o Concatenate string inside for loop. o Display reversed string. Reverse String using strrev() function A reverse string program using strrev() function is shown. Example: <?php $string = "JAVATPOINT"; echo "Reverse string of $string is " .strrev ( $string );
  • 11. ?> Swapping two numbers Two numbers can be swapped or interchanged. It means first number will become second and second number will become first. For example a = 20, b = 30 After swapping, a = 30, b = 20 There are two methods for swapping: o By using third variable. o Without using third variable. Swapping Using Third Variable Swap two numbers 45 and 78 using a third variable. Example: <?php $a = 45; $b = 78; // Swapping Logic $third = $a; $a = $b; $b = $third; echo "After swapping:<br><br>"; echo "a =".$a." b=".$b; ?> Swapping Without using Third Variable Swap two numbers without using a third variable is done in two ways: o Using arithmetic operation + and ? o Using arithmetic operation * and / Example for (+ and -): <?php $a=234; $b=345; //using arithmetic operation
  • 12. $a=$a+$b; $b=$a-$b; $a=$a-$b; echo "Value of a: $a</br>"; echo "Value of b: $b</br>"; ?> Area of Triangle Area of a triangle is calculated by the following Mathematical formula, (base * height) / 2 = Area Area of Triangle in PHP Program to calculate area of triangle with base as 10 and height as 15 is shown. Example: <?php $base = 10; $height = 15; echo "area with base $base and height $height= " . ($base * $height) / 2; ?> Area of a Rectangle Area of a rectangle is calculated by the mathematical formula, 1. Length ∗ Breadth = Area Logic: o Take two variables. o Multiply both of them. Area of Rectangle in PHP Program to calculate area of rectangle with length as 14 and width as 12 is shown. Example: Advertisement <?php $length = 14; $width = 12; echo "area of rectangle is $length * $width= " . ($length * $width) . "<br />"; ?> Leap Year Program A leap year is the one which has 366 days in a year. A leap year comes after every four years. Hence a leap year is always a multiple of four.
  • 13. For example, 2016, 2020, 2024, etc are leap years. Leap Year Program This program states whether a year is leap year or not from the specified range of years (1991 - 2016). Example: <?php function isLeap($year) { return (date('L', mktime(0, 0, 0, 1, 1, $year))==1); } //For testing for($year=1991; $year<2016; $year++) { If (isLeap($year)) { echo "$year : LEAP YEAR<br />n"; } else { echo "$year : Not leap year<br />n"; } } ?> Alphabet Triangle Pattern Some different alphabet triangle patterns using range() function in PHP are shown below. <?php $alpha = range('A', 'Z'); for($i=0; $i<5; $i++){ for($j=5; $j>$i; $j--){ echo $alpha[$i]; } echo "<br>"; } ?>
  • 14. Star Triangle The star triangle in PHP is made using for and foreach loop. There are a lot of star patterns. We'll show some of them here. Pattern 1 <?php for($i=0;$i<=5;$i++){ for($j=5-$i;$j>=1;$j--){ echo "* "; } echo "<br>"; } ?> Output: Pattern 2 <?php for($i=0;$i<=5;$i++){ for($j=1;$j<=$i;$j++){ echo "* "; } echo "<br>"; } ?> Output:
  • 15. Pattern 3 <?php for($i=0;$i<=5;$i++){ for($k=5;$k>=$i;$k--){ echo " "; } for($j=1;$j<=$i;$j++){ echo "* "; } echo "<br>"; } for($i=4;$i>=1;$i--){ for($k=5;$k>=$i;$k--){ echo " "; } for($j=1;$j<=$i;$j++){ echo "* "; } echo "<br>"; } ?> Output:
  • 16. Pattern 4 <?php for($i=1; $i<=5; $i++){ for($j=1; $j<=$i; $j++){ echo ' * '; } echo '<br>'; } for($i=5; $i>=1; $i--){ for($j=1; $j<=$i; $j++){ echo ' * '; } echo '<br>'; } ?> Output:
  • 17. Pattern 5 <?php for ($i=1; $i<=5; $i++) { for ($j=1; $j<=5; $j++) { echo '* '; } echo "</br>"; } ?> Output: Pattern 6 <?php for($i=5; $i>=1; $i--) { if($i%2 != 0) { for($j=5; $j>=$i; $j--) { echo "* "; } echo "<br>"; } } for($i=2; $i<=5; $i++) { if($i%2 != 0) {
  • 18. for($j=5; $j>=$i; $j--) { echo "* "; } echo "<br>"; } } ?>