KY Php
PART 1 — PHP Fundamentals
Q1. What is PHP?
English Answer:
PHP stands for PHP: Hypertext Preprocessor. It is an open-source, server-side scripting language mainly used for developing dynamic websites and web applications.
PHP code executes on the server, and the generated result is sent to the browser.
Hinglish:
PHP ek server-side scripting language hai. PHP code browser mein directly execute nahi hota; server par execute hota hai aur browser ko output milta hai.
Example:
<?phpecho "Hello World";?>Interview Follow-up:
Q. Is PHP frontend or backend?
Answer: PHP is primarily a backend/server-side language.
Q2. Why is PHP called a server-side language?
English Answer:
PHP is called a server-side language because PHP code is executed on the web server before the response is sent to the user’s browser.
Hinglish:
Browser PHP code ko directly execute nahi karta. Request server par jaati hai, PHP server par execute hoti hai aur server HTML/JSON etc. response browser ko bhejta hai.
Flow:
Browser↓Request↓PHP Server↓PHP executes↓Response↓BrowserQ3. What are the main features of PHP?
English Answer:
The main features of PHP include:
- Open-source
- Free to use
- Server-side execution
- Cross-platform support
- Database support
- Object-oriented programming support
- Easy integration with HTML
- Large community and ecosystem
Hinglish:
PHP free aur open-source hai, Windows/Linux/macOS par run ho sakti hai, databases ke saath easily kaam karti hai aur OOP support karti hai.
Q4. What is the difference between PHP and JavaScript?
English Answer:
PHP is primarily a server-side language, while JavaScript traditionally runs in the browser and can also run on the server using environments such as Node.js.
Hinglish:
PHP → Mainly BackendJavaScript → Frontend + BackendPHP server-side processing ke liye commonly use hoti hai, while JavaScript browser-side interaction ke liye commonly use hoti hai.
Follow-up:
Q. Can PHP work with React?
Yes. React can communicate with PHP through APIs, usually exchanging JSON data.
Q5. How do you write PHP code?
English Answer:
PHP code is generally written inside PHP tags.
Code:
<?phpecho "Hello PHP";?>Hinglish:
PHP code normally <?php se start hota hai.
Q6. What is a PHP variable?
English Answer:
A variable is used to store data in PHP. PHP variables start with the $ symbol.
Code:
$name = "Rahul";$age = 25;$price = 500.50;Hinglish:
Variable data ko temporarily store karne ke liye use hota hai aur PHP mein $ se start hota hai.
Q7. What are the rules for naming variables in PHP?
English Answer:
A PHP variable:
- Must start with
$ - Must start with a letter or underscore after
$ - Cannot start with a number
- Can contain letters, numbers and underscores
- Variable names are case-sensitive
Example:
$name = "Rahul";$_name = "Rahul";$name1 = "Rahul";Invalid:
$1name = "Rahul";Hinglish:
Variable $ se start hona chahiye aur $ ke baad number se start nahi kar sakte.
Q8. Are PHP variables case-sensitive?
English Answer:
Yes, PHP variable names are case-sensitive.
$name = "Rahul";echo $name;// $Name is a different variableHinglish:
$name aur $Name PHP ke liye different variables hain.
Q9. What are PHP data types?
English Answer:
PHP supports several data types, including:
- String
- Integer
- Float
- Boolean
- Array
- Object
- NULL
- Resource
Hinglish:
PHP mein different types ka data store karne ke liye different data types hote hain.
Q10. What is a String?
English Answer:
A string is a sequence of characters.
Code:
$name = "Rahul";$message = 'Hello';Hinglish:
Text ko string kehte hain.
Q11. What is an Integer?
English Answer:
An integer is a whole number without a decimal point.
Example:
$age = 25;$count = 100;Hinglish:
Without decimal wale whole numbers integer hote hain.
Q12. What is a Float?
English Answer:
A float is a number containing a decimal point.
Example:
$price = 99.99;$rating = 4.5;Hinglish:
Decimal numbers ko float kehte hain.
Q13. What is a Boolean?
English Answer:
A Boolean represents one of two values: true or false.
Example:
$isLoggedIn = true;$isAdmin = falseHinglish:
Boolean mein mainly do values hoti hain:
truefalseQ14. What is NULL in PHP?
English Answer:
NULL represents a variable with no value.
Example:
$name = NULL;Hinglish:
Jab variable ke paas koi value nahi hoti, to uski value NULL ho sakti hai.
Q15. What is an array?
English Answer:
An array is a data structure that can store multiple values in a single variable.
Example:
$names = ["Rahul", "Amit", "Rohit"];Hinglish:
Ek variable ke andar multiple values store karni ho to array use karte hain.
Q16. What is an object in PHP?
English Answer:
An object is an instance of a class. It contains properties and methods defined by the class.
Example:
class User {public $name;}$user = new User();$user->name = "Rahul";Hinglish:
Class ek blueprint hoti hai aur us class se banaya hua actual instance object hota hai.
Q17. What is type casting?
English Answer:
Type casting is the process of converting a value from one data type to another.
Example:
$value = "100";$number = (int)$value;echo $number;Hinglish:
String ko integer, float ko integer etc. mein convert karna type casting hai.
Q18. What is type juggling?
English Answer:
Type juggling is PHP’s automatic conversion of a value from one type to another when required by an operation.
Example:
$num = "10";$result = $num + 5;PHP can convert the numeric string for the arithmetic operation.
Hinglish:
PHP zarurat ke according datatype automatically convert kar sakti hai. Isko type juggling kehte hain.
Q19. What is the difference between == and ===?
English Answer:
== compares values after type conversion when necessary, while === compares both value and data type.
Example:
5 == "5"; // true5 === "5"; // falseHinglish:
== → Value compare=== → Value + Data Type compareInterview Tip:
Interview mein === ko generally safer comparison operator ke roop mein explain karna.
Q20. What is echo in PHP?
English Answer:
echo is a language construct used to output data.
Example:
echo "Hello";echo $name;Hinglish:
Browser ko output bhejne ke liye echo commonly use karte hain.
Q21. What is print in PHP?
English Answer:
print is also used to output data. Unlike echo, it returns 1 and accepts one argument.
Example:
print "Hello";Hinglish:
print bhi output ke liye use hota hai, lekin echo zyada commonly use kiya jata hai.
Q22. What is the difference between echo and print?
English Answer:
echoprintLanguage constructLanguage constructCan accept multiple argumentsAccepts one argumentDoes not return a valueReturns 1Commonly used for outputAlso used for output
Hinglish:
Dono output ke liye use hote hain, lekin practical PHP code mein echo bahut common hai.
Q23. What are constants in PHP?
English Answer:
A constant is a value that cannot be changed during script execution.
Example:
define("SITE_NAME", "My Website");echo SITE_NAME;Or:
const SITE_NAME = "My Website";Hinglish:
Aisi value jo program ke execution ke during change nahi karni ho, uske liye constant use kar sakte hain.
Q24. What is the difference between const and define()?
English Answer:
Both can create constants, but they have different syntax and language behavior. const is part of PHP's language syntax and can be used for class constants, while define() is a function that defines a constant at runtime.
Hinglish:
Dono constants banane ke liye use hote hain, lekin const language construct hai aur class constants ke liye bhi use hota hai.
Q25. What are magic constants?
English Answer:
Magic constants are predefined constants whose values can change depending on where they are used.
Examples include:
__FILE____DIR____LINE____FUNCTION____CLASS____METHOD____NAMESPACE__Hinglish:
PHP ke special predefined constants ko magic constants kehte hain.
Example:
echo __FILE__;Current PHP file ka path provide kar sakta hai.
Q26. What are operators in PHP?
English Answer:
Operators are symbols used to perform operations on values.
Common types include:
- Arithmetic
- Assignment
- Comparison
- Logical
- Increment/Decrement
- String
- Conditional
Hinglish:
Values ke saath operation perform karne ke liye operators use hote hain.
Q27. What are arithmetic operators?
English Answer:
+ Addition- Subtraction* Multiplication/ Division% Modulus** ExponentiationExample:
$a = 10;$b = 3;echo $a + $b;echo $a % $b;Hinglish:
Mathematical calculations ke liye arithmetic operators use hote hain.
Q28. What are comparison operators?
English Answer:
Comparison operators compare two values.
Examples:
=====!=!==><>=<=Hinglish:
Do values ko compare karne ke liye comparison operators use karte hain.
Q29. What are logical operators?
English Answer:
Logical operators are used to combine or negate conditions.
&& AND|| OR! NOTExample:
if ($age >= 18 && $isVerified) {echo "Allowed";}Hinglish:
Multiple conditions ko combine karne ke liye logical operators use hote hain.
Q30. What is the ternary operator?
English Answer:
The ternary operator is a short form of a simple if-else expression.
Example:
$status = ($age >= 18) ? "Adult" : "Minor";Hinglish:
Normal:
if ($age >= 18) {$status = "Adult";} else {$status = "Minor";}Ternary:
$status = ($age >= 18) ? "Adult" : "Minor";Q31. What is the null coalescing operator?
English Answer:
The null coalescing operator ?? returns the first value if it exists and is not NULL; otherwise it returns the second value.
Example:
$name = $_GET['name'] ?? "Guest";Hinglish:
Agar $_GET['name'] available hai to wo milega, warna "Guest" milega.
Q32. What is isset()?
English Answer:
isset() checks whether a variable is set and is not NULL.
Example:
if (isset($name)) {echo $name;}Hinglish:
Check karna ho ki variable exist/set hai aur NULL nahi hai, to isset() use karte hain.
Q33. What is empty()?
English Answer:
empty() checks whether a variable contains an empty or false-like value.
Example:
if (empty($name)) {echo "Name is empty";}Hinglish:
Variable empty hai ya false-like value rakhta hai ya nahi, ye check karne ke liye empty() use hota hai.
Q34. Difference between isset() and empty()?
English Answer:
isset() checks whether a variable exists and is not NULL.
Get Rahul Jingar’s stories in your inbox
Join Medium for free to get updates from this writer.
empty() checks whether a value is considered empty.
Hinglish:
isset() → Set hai + NULL nahi?empty() → Empty hai?Interview Follow-up:
Q. What happens with isset($x) if $x = null?
Answer: It returns false.
Q35. What is unset()?
English Answer:
unset() destroys a variable or removes an array element.
Example:
$name = "Rahul";unset($name);Hinglish:
Variable ko remove/destroy karne ke liye unset() use hota hai.
Q36. What is variable scope?
English Answer:
Variable scope defines where a variable can be accessed.
PHP commonly has:
- Local scope
- Global scope
- Static scope
Hinglish:
Variable program ke kis area mein accessible hai, usko variable scope kehte hain.
Q37. What is a local variable?
English Answer:
A variable declared inside a function generally has local scope and can be accessed within that function.
Example:
function test() {$name = "Rahul";echo $name;}Hinglish:
Function ke andar banaya variable normally us function ke andar hi accessible hota hai.
Q38. What is a global variable?
English Answer:
A variable declared outside a function has global scope. It is not automatically available inside functions unless explicitly accessed.
Example:
$name = "Rahul";function test() {global $name;echo $name;}Hinglish:
Function ke bahar declared variable global scope mein hota hai. Function ke andar use karne ke liye global ya $GLOBALS use kar sakte hain.
Q39. What is the global keyword?
English Answer:
The global keyword allows a function to access a variable defined in the global scope.
Example:
$name = "Rahul";function showName() {global $name;echo $name;}Hinglish:
Function ke bahar wale variable ko function ke andar access karna ho to global use kar sakte hain.
Q40. What is $GLOBALS?
English Answer:
$GLOBALS is a superglobal array containing references to variables in the global scope.
Example:
$name = "Rahul";function test() {echo $GLOBALS['name'];}Hinglish:
Global variables ko $GLOBALS ke through bhi access kar sakte hain.
Q41. What is a conditional statement?
English Answer:
Conditional statements execute code based on whether a condition is true or false.
Common conditional statements include:
ifif-elseelseifswitchmatchHinglish:
Condition ke according different code execute karna ho to conditional statements use karte hain.
Q42. What is an if statement?
English Answer:
The if statement executes a block of code when a condition is true.
Example:
$age = 20;if ($age >= 18) {echo "Adult";}Hinglish:
Condition true hone par code execute hota hai.
Q43. What is the difference between if-else and elseif?
English Answer:
if-else provides an alternative block when the if condition is false. elseif allows multiple conditions to be checked sequentially.
Example:
if ($marks >= 90) {echo "A";} elseif ($marks >= 60) {echo "B";} else {echo "C";}Hinglish:
Multiple conditions check karni ho to elseif useful hota hai.
Q44. What is a switch statement?
English Answer:
A switch statement is used to execute different blocks based on the value of an expression.
Example:
$day = 1;switch ($day) {case 1:echo "Monday";break;case 2:echo "Tuesday";break;default:echo "Invalid";}Hinglish:
Ek value ke multiple possible cases ho to switch useful hota hai.
Q45. What is match expression in PHP?
English Answer:
match is an expression that compares a value strictly and returns a result.
Example:
$day = 1;$result = match ($day) {1 => "Monday",2 => "Tuesday",default => "Invalid"};echo $result;Hinglish:
match modern PHP mein value matching ke liye use hota hai. Ye strict comparison karta hai aur result return karta hai.
Q46. Difference between switch and match?
English Answer:
switchmatchStatementExpressionUses loose comparison behaviorUses strict comparisonUsually uses breakNo break requiredDoes not directly return a value in the same wayReturns a valueOlder/common syntaxModern PHP feature
Hinglish:
Simple words mein:
switch → Traditionalmatch → Modern + strict + value returnQ47. What is a loop?
English Answer:
A loop repeatedly executes a block of code while a condition is satisfied or while elements remain to be processed.
Types:
forwhiledo-whileforeachHinglish:
Same code ko repeatedly execute karne ke liye loop use hota hai.
Q48. What is a for loop?
English Answer:
A for loop is commonly used when the number of iterations is controlled by a counter.
Example:
for ($i = 1; $i <= 5; $i++) {echo $i;}Hinglish:
Jab pata ho ki loop ko kitni baar chalana hai, for loop useful hota hai.
Q49. What is a while loop?
English Answer:
A while loop repeatedly executes code as long as its condition remains true.
Example:
$i = 1;while ($i <= 5) {echo $i;$i++;}Hinglish:
Jab tak condition true hai tab tak loop chalta rahega.
Q50. What is a foreach loop?
English Answer:
foreach is commonly used to iterate over arrays and other iterable values.
Example:
$names = ["Rahul", "Amit", "Rohit"];foreach ($names as $name) {echo $name;}Hinglish:
Array ke har element ko one-by-one access karne ke liye foreach sabse commonly use hota hai.
🔥 Q51. Difference between for and foreach?
English Answer:
for is generally counter-based, while foreach is designed for iterating over arrays and iterable values.
Hinglish:
for↓Counter basedforeach↓Array/Iterable based🔥 Q52. What are break and continue?
English Answer:
break terminates the current loop.
continue skips the current iteration and moves to the next iteration.
Example:
for ($i = 1; $i <= 5; $i++) {if ($i == 3) {continue;}echo $i;}Output:
1245Hinglish:
break → Loop completely stopcontinue → Current iteration skip🔥 Q53. What is a function in PHP?
English Answer:
A function is a reusable block of code designed to perform a specific task.
Example:
function add($a, $b) {return $a + $b;}echo add(10, 20);Hinglish:
Function reusable code ka block hota hai. Ek baar bana ke multiple times use kar sakte hain.
Q54. What is the difference between parameter and argument?
English Answer:
A parameter is a variable defined in the function declaration. An argument is the actual value passed when calling the function.
Example:
function add($a, $b) {return $a + $b;}add(10, 20);Here:
$a, $b → Parameters10, 20 → ArgumentsHinglish:
Function banate waqt jo variables hain wo parameters hain. Function call karte waqt jo actual values dete hain wo arguments hain.
Q55. What is return?
English Answer:
return sends a value from a function back to the code that called the function.
Example:
function add($a, $b) {return $a + $b;}$result = add(10, 20);echo $result;Hinglish:
return function se value bahar bhejne ke liye use hota hai.
Q56. Difference between echo and return?
English Answer:
echo outputs data, while return sends a value back from a function.
Hinglish:
echo↓Output dikhata haireturn↓Function se value return karta haiQ57. What is a default parameter?
English Answer:
A default parameter has a predefined value that is used when the caller does not provide an argument.
Example:
function greet($name = "Guest") {return "Hello " . $name;}echo greet();Output:
Hello GuestHinglish:
Agar function call karte time value nahi doge to default value use hogi.
Q58. What is pass by value?
English Answer:
Pass by value means a copy of the variable’s value is passed to the function. Changes to the parameter do not normally modify the original variable.
Example:
function change($x) {$x = 100;}$a = 10;change($a);echo $a;Hinglish:
Normally function ko variable ki value ki copy milti hai, original variable directly change nahi hota.
Q59. What is pass by reference?
English Answer:
Pass by reference allows a function to work with the original variable using the & symbol.
Example:
function change(&$x) {$x = 100;}$a = 10;change($a);echo $a;Output:
100Hinglish:
& use karne par function original variable ko modify kar sakta hai.
Q60. What is an anonymous function?
English Answer:
An anonymous function is a function without a name. It can be assigned to a variable or passed as a callback.
Example:
$greet = function ($name) {return "Hello " . $name;};echo $greet("Rahul");Hinglish:
Jis function ka naam nahi hota usko anonymous function kehte hain.





