PHP Common Coding Questions & Answers
1. What is the use of .htaccess file in Laravel?
- It controls URL rewriting to enable pretty URLs, manages redirects, security rules, and access control on
Apache servers.
2. What is the use of config folder in Laravel?
- It contains configuration files for settings like database, mail, cache, app settings, etc., which Laravel loads
automatically.
3. Check if a number is a palindrome in PHP without predefined functions
Code:
$num = 121;
$original = $num;
$reverse = 0;
while ($num > 0) {
$digit = $num % 10;
$reverse = $reverse * 10 + $digit;
$num = intdiv($num, 10);
}
if ($original == $reverse) {
echo "Palindrome";
} else {
echo "Not palindrome";
}
Logic: Extract digits from end and build reversed number, then compare.
4. Common function to check palindrome for both string and number (no predefined functions)
Code:
function isPalindrome($input) {
$length = 0;
while (isset($input[$length])) { $length++; }
for ($i = 0; $i < $length / 2; $i++) {
if ($input[$i] != $input[$length - 1 - $i]) {
return false;
}
PHP Common Coding Questions & Answers
}
return true;
}
Explanation: Compare characters from start and end moving towards the center.
5. Check if a number is prime
Code:
$num = 17;
$isPrime = true;
if ($num < 2) $isPrime = false;
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) {
$isPrime = false;
break;
}
}
echo $isPrime ? "Prime" : "Not prime";
Logic: Check divisibility from 2 up to sqrt(num).
6. Generate all prime numbers up to N
Code:
$n = 20;
for ($num = 2; $num <= $n; $num++) {
$isPrime = true;
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) {
$isPrime = false;
break;
}
}
if ($isPrime) echo $num . " ";
}
PHP Common Coding Questions & Answers
Logic: Check each number from 2 to N for primality.
7. Swap two numbers without third variable
Code:
$a = 5; $b = 10;
$a = $a + $b;
$b = $a - $b;
$a = $a - $b;
echo "a = $a, b = $b";
Logic: Use arithmetic to swap values without extra space.
8. Reverse a number
Code:
$num = 1234;
$reverse = 0;
while ($num > 0) {
$digit = $num % 10;
$reverse = $reverse * 10 + $digit;
$num = intdiv($num, 10);
}
echo $reverse;
Logic: Extract digits and build reversed number.
9. Reverse a string without predefined functions
Code:
$str = "hello";
$reversed = "";
$len = 0;
while (isset($str[$len])) { $len++; }
for ($i = $len - 1; $i >= 0; $i--) {
$reversed .= $str[$i];
}
echo $reversed;
PHP Common Coding Questions & Answers
Logic: Append characters from end to start.
10. Reverse an array without predefined functions
Code:
$arr = [1, 2, 3, 4, 5];
$reversed = [];
for ($i = count($arr) - 1; $i >= 0; $i--) {
$reversed[] = $arr[$i];
}
print_r($reversed);
Logic: Copy elements from last to first.
11. Find factorial of a number (recursive)
Code:
function factorial($n) {
if ($n < 0) return "Not defined";
elseif ($n <= 1) return 1;
else return $n * factorial($n - 1);
}
echo factorial(5);
Logic: factorial(n) = n * factorial(n-1), base case factorial(0)=1.
12. Find factorial of a number (iterative)
Code:
$n = 5;
$fact = 1;
for ($i = 2; $i <= $n; $i++) {
$fact *= $i;
}
echo $fact;
Logic: Multiply numbers from 1 to n.
PHP Common Coding Questions & Answers
13. Fibonacci series up to N terms
Code:
$n = 10;
$a = 0; $b = 1;
for ($i = 1; $i <= $n; $i++) {
echo $a . " ";
$next = $a + $b;
$a = $b;
$b = $next;
}
Logic: Start with 0 and 1; each next is sum of previous two.