webounstraininghub.in

Edit Content
Click on the Edit Content button to edit/add the content.

Building Dynamic Experiences with PHP

Sharpen Skills, Ace Interviews

PHP : Backend Development Interview Questions

PHP is a server-side scripting language designed primarily for web development but also used as a general-purpose programming language.

PHP is commonly used for dynamic web content, database interaction, session tracking, and building web applications.

Both include and require are used to include files, but require produces a fatal error and stops the script if the file is missing, while include only gives a warning and continues execution.

Sessions in PHP are used to store data across multiple pages during a user's visit, providing a way to persist user data.

$_SERVER['PHP_SELF'] returns the filename of the currently executing script relative to the document root.

isset() checks if a variable is set and not null, while empty() checks if a variable is empty (e.g., 0, false, null, "").

A namespace in PHP is used to avoid name conflicts by encapsulating classes, interfaces, functions, and constants within a specified space.

Traits are a mechanism for code reuse in single inheritance languages like PHP, allowing the inclusion of methods from multiple classes.

PHP connects to a MySQL database using functions like mysqli_connect() or PDO (PHP Data Objects).

== checks for equality of value, while === checks for both value and type equality.

header() is used to send raw HTTP headers to the browser, commonly used for redirection or content-type specification.

Superglobals are built-in arrays in PHP, such as $_GET, $_POST, $_SESSION, $_COOKIE, etc., accessible globally throughout a script.

Use prepared statements with bound parameters via PDO or MySQLi to prevent SQL injection.

explode() splits a string into an array based on a delimiter, while implode() joins array elements into a string using a delimiter.

file_get_contents() reads the contents of a file into a string.

A closure is an anonymous function that can capture variables from its surrounding scope.

An abstract class cannot be instantiated and may contain abstract methods, which must be implemented by derived classes

PHP handles error reporting via the error_reporting() function, which can control which errors are displayed or logged.

The final keyword prevents a class from being inherited or a method from being overridden.

echo can take multiple parameters and has no return value, while print takes one parameter and returns 1.

__construct() is a constructor method called when an object is instantiated, and __destruct() is a destructor called when an object is destroyed.

PDO (PHP Data Objects) is a database access layer providing a uniform method of access to multiple databases.

Use the is_array() function to check if a variable is an array.

unlink() deletes a file from the filesystem, while unset() destroys a variable in the script.

Magic methods are special methods like __construct(), __destruct(), __call(), __get(), __set(), which are invoked automatically under certain conditions.

$num = 4;
if ($num % 2 == 0) {
echo "$num is even.";
} else {
echo "$num is odd.";
}
function factorial($n) {
if ($n <= 1) return 1;
return $n * factorial($n - 1);
}
echo factorial(5); // Output: 120
function reverseString($str) {
return strrev($str);
}
echo reverseString("Hello"); // Output: olleH
function isPalindrome($str) {
return $str === strrev($str);
}
echo isPalindrome("madam") ? "Palindrome" : "Not a palindrome";
$a = 5;
$b = 10;
$a = $a + $b;
$b = $a - $b;
$a = $a - $b;
echo "a = $a, b = $b";
function findLargest($arr) {
return max($arr);
}
echo findLargest([2, 5, 1, 9, 7]); // Output: 9
$array = [1, 2, 2, 3, 4, 4, 5];
$uniqueArray = array_unique($array);
print_r($uniqueArray);
function countCharacters($str) {
return count_chars($str, 1);
}
print_r(countCharacters("hello"));
$array = [4, 2, 8, 5, 1];
sort($array);
print_r($array);
$array = [1, 2, 3, 4, 5];
echo array_sum($array); // Output: 15
function containsWord($str, $word) {
return strpos($str, $word) !== false;
}
echo containsWord("Hello world", "world") ? "Found" : "Not Found";
function generateRandomString($length = 10) {
return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}
echo generateRandomString(); // Example Output: hG5jK2a7Lz
function stringLength($str) {
return strlen($str);
}
echo stringLength("Hello World"); // Output: 11
function isLeapYear($year) {
return ($year % 4 == 0 && $year % 100 != 0) || ($year % 400 == 0);
}
echo isLeapYear(2024) ? "Leap Year" : "Not a Leap Year";
function countVowels($str) {
return preg_match_all('/[aeiouAEIOU]/', $str);
}
echo countVowels("Hello World"); // Output: 3
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
$mergedArray = array_merge($array1, $array2);
print_r($mergedArray);
function isPrime($num) {
if ($num <= 1) return false;
for ($i = 2; $i < sqrt($num); $i++) {
if ($num % $i == 0) return false;
}
return true;
}
echo isPrime(7) ? "Prime" : "Not Prime";
function toUpperCase($str) {
return strtoupper($str);
}
echo toUpperCase("hello world"); // Output: HELLO WORLD
$array1 = [1, 2, 3, 4];
$array2 = [3, 4, 5, 6];
$intersection = array_intersect($array1, $array2);
print_r($intersection);
function gcd($a, $b) {
return $b ? gcd($b, $a % $b) : $a;
}
echo gcd(12, 15); // Output: 3
function toLowerCase($str) {
return strtolower($str);
}
echo toLowerCase("HELLO WORLD"); // Output: hello world
function isAssociativeArray($array) {
return array_keys($array) !== range(0, count($array) - 1);
}
echo isAssociativeArray(["a" => 1, "b" => 2]) ? "Associative" : "Not Associative";
$array = [1, 2, 3, 4, 5];
$reversedArray = array_reverse($array);
print_r($reversedArray);
$array = [1, 2, 3, 4, 5];
echo count($array); // Output: 5
$array = [1, 2, 3, 4, 5];
array_pop($array);
print_r($array); // Output: [1, 2, 3, 4]
function checkNumber($num) {
if ($num > 0) return "Positive";
if ($num < 0) return "Negative";
return "Zero";
}
echo checkNumber(10); // Output: Positive
function checkNumber($num) {
if ($num > 0) return "Positive";
if ($num < 0) return "Negative";
return "Zero";
}
echo checkNumber(10); // Output: Positive
function square($num) {
return $num * $num;
}
echo square(5); // Output: 25
function squareRoot($num) {
return sqrt($num);
}
echo squareRoot(25); // Output: 5
$str = " Hello World ";
$trimmedStr = trim($str);
echo $trimmedStr; // Output: Hello World
$date1 = new DateTime("2024-01-01");
$date2 = new DateTime("2024-12-31");
$interval = $date1->diff($date2);
echo $interval->days; // Output: 365
Get in touch

We are here to help you & your business

We provide expert guidance, personalized support, and resources to help you excel in your digital marketing career.

Timings
9:00 am - 5:00 pm

    Book Your FREE  Digital Marketing Consultation

    +91 8005836769

    info@webounstraininghub.in