Last Updated On By Khizer Ali
PHP Programming is all about logic. And controlling the flow of logic is an essential part of any program. Here, the flow of logic means the decision-making process where you pick which steps should be taken under which conditions.
In this article, we’ll go through examples of logical programs in php.
Table of Contents
Leap year is a year which occurs once every four years.
<?php
$year = 2019;
if($year % 4 == 0) {
echo "This is a leap year";
}
else {
echo "This is a not leap year";
}
?>
If the given year is a leap year, then it should be completely divisible by 4. So, by applying this logic, we check that if the remainder is equal to 0 after dividing it with 4, then it must be a leap year; otherwise, it is not a leap year.
Output:
Even numbers those which are divisible by 2 like 2, 4, 6, 8 and those which are not divisible by 2 are odd numbers like 1, 3, 5, 7.
<?php
$check1 = 14255;
$check2 = 35934;
if($check1%2 == 0) {
echo "$check1 is Even number <br/>";
} else {
echo "$check1 is Odd number <br/>";
}
if($check2%2 == 0) {
echo "$check2 is Even number";
} else {
echo "$check2 is Odd number";
}
?>
output
To find the sum of numbers, simply add all the digits.
<?php
$num = 12345;
$rem = 0;
$sum = 0;
for($i = 0; $i <= strlen($num); $i++) {
$rem = $num % 10;
$sum = $sum + $rem;
$num = $num/10;
}
echo "Sum of digits is $sum";
?>
We initialize the variable $num with the value whose digits we are going to count. Then initialize two other variables $rem and $sum to store the remainder and the sum respectively. To understand the flow of the for() loop and how it is working, let us take an example of number 234.
| $rem = $num %10 | $sum = $sum + $rem | $num = $num/10 |
1st iteration | 234 % 10 $rem = 4 | 0 + 4 $sum = 4 | 234/10 $num = 23.4 |
2nd iteration | 23.4 % 10 $rem = 3 | 4 + 3 $sum = 7 | 23/10 $num = 2.3 |
3rd iteration | 2.3 % 10 $rem = 2 | 7 + 2 $sum = 9 | 2/10 $num = 0.2 |
Output
We can easily print a table of any number with the help of loop.
<?php
$num = 5;
for($i = 1; $i <=10; $i++) {
echo "$num * $i = " . $num*$i;
echo "<br/>";
}
?>
output
Factorial is the product of the integer and all the integers below it till 1. For example, 5! = 5 * 4 * 3 *2 * 1 = 120
<?php
$num = 5;
$factorial = 1;
if($num != 0) {
for($i = $num; $i>=1; $i--) {
$factorial = $factorial * $i;
}
}
else {
$factorial = 1;
}
echo "Factorial of $num is $factorial";
?>
Output:
<?php
$n = 12;
$a = -1;
$b = 1;
for($i = 1; $i <= $n; $i++) {
$c = $a + $b;
echo $c." ";
$a = $b;
$b = $c;
}
?>
Initializes $n with the 12 which means we’re going to display 12 elements of a Fibonacci series. Start the for loop, in this we add the first two numbers to get the third one in $c, and we’ll get 0 as our first number. Display it and update our two variable $a, and $b. In our next iteration, we’ll get 1 and so on.
output:
A prime number is a number which is only divisible by 1 and that number itself. It means that the prime number can not be divided by any other number.
<?php
$no = 2;
$total_display = 0;
while ($total_display < 10 ) {
$divisible_count = 0;
for($i = 1; $i <=$no; $i++) {
if($no % $i == 0) {
$divisible_count += 1;
}
}
if($divisible_count < 3) {
echo $no . " ";
$total_display += 1;
}
$no += 1;
}
?>
We first store the current number to a variable $no. And then we’ll find the reminder for each value with itself and its below integers. And we’ll increment a counter called $divisible_count if a reminder equals to 0.
After all the divisions we check the value of $divisible_count if it is less than 3, it means that a number is a prime number because it is only wholly divisible by 1 and itself.
Output:
A palindrome is a number which remains the same when its integers are reversed. For example, 78987.
<?php
function reverse($number) {
$num = $number;
$rev = 0;
while ($num > 1) {
$rem = $num % 10;
$rev = ($rev * 10) + $rem;
$num /= 10;
}
return $rev;
}
$check = 12321;
$reverse = reverse($check);
if($check == $reverse) {
echo "$check is a palindrome";
}
else {
echo "$check is not a palindrome";
}
?>
First, we take a number in a variable $check then, pass this number to a function which returns it in reverse order, finally compare the two figures, if they are same, means the number is a palindrome.
output:
Anagrams are those words which share the same alphabets as each other with the difference of arrangement. For example, “listen” and “silent” are both anagrams.
<?php
$str1 = "earth";
$str2 = "heart";
$k = 0;
if(strlen($str1) == strlen($str2)) {
for($i = 0; $i <strlen($str1); $i++) {
for($j = 0; $j <strlen($str2); $j++) {
if($str1[$i] == $str2[$j]) {
$k++;
break;
}
}
}
if ($k == strlen($str2)) {
echo "$str1 and $str2 are anagram";
}
else {
echo "$str1 and $str2 are not anagram";
}
}
else {
echo "$str1 and $str2 are not anagram";
}
?>
output:
(A)
If you observe that pattern, there are 5 rows and 5 columns. Row 1 has 1 column; row 2 has 2 columns and so on. It means that rows and columns are the same.
<?php
for($i = 0; $i <5; $i++) {
for($j = 0; $j <= $i; $j++) {
echo "* ";
}
echo "<br/>";
}
?>
Here, the outer for loop indicates the row and the inner for loop is for the column.
Output:
(b)
This is the same pattern as the above one, but except the stars there are numbers. And if you observe it they are even numbers.
<?php
$no = 2;
for($i = 0; $i <5; $i++) {
for($j = 0; $j <= $i; $j++) {
echo "$no ";
$no += 2;
}
echo "<br/>";
}
?>
The outer and inner loop is working same as the above but except printing *we print 2 and then increment it by 2 so that the next number can be generated.
output:
(C)
<?php
for($i = 5; $i > 0; $i--) {
for($j = $i; $j > 0; $j--) {
echo "* ";
}
echo "<br/>";
}
?>
According to the pattern, there are 5 columns on 1st row, 4 columns on 2nd row and so on. Means starting from 5 columns and then decreasing it at each row.
output:
PHP language is a popular server-side scripting language. If you are willing to learn PHP, then it is essential to grasp the concepts of how you can make different logics to solve a problem.
In this article, we’ve seen various examples with source code to understand how to resolve the issue. There could be different PHP code for every problem because every person thinks differently. If you want to try any problem with your logic and you should try. This is how you learn. Good luck!