What is the difference between GET and POST methods? GET और POST मेथड्स में क्या अंतर है?
Short Answer
Default parameters allow you to assign a default value to a function parameter. If the caller does not pass an argument for that parameter, the default value is used instead of throwing an error.
Detailed Explanation & Code Example
Default parameters are incredibly useful for making functions flexible and keeping function signatures clean when certain arguments are almost always the same.
// $role has a default value of 'Guest'
function createUser($name, $role = "Guest") {
echo "Created user: $name, Role: $role\n";
}
// We don't provide a second argument, so 'Guest' is used.
createUser("Alice");
// Output: Created user: Alice, Role: Guest
// We provide a second argument, which overrides the default.
createUser("AdminBob", "Administrator");
// Output: Created user: AdminBob, Role: Administrator
Common Mistakes
Ordering matters! Optional parameters (those with default values) must always be placed after required parameters in the function definition. Otherwise, PHP won't know which arguments align with which parameters.
// BAD: Will cause a Fatal Error if called with only one argument
function wrongExample($role = "Guest", $name) { }
// GOOD
function correctExample($name, $role = "Guest") { }
| पहलू | GET | POST |
|---|---|---|
| डेटा विज़िबिलिटी | URL में जुड़ा, दिखता है | रिक्वेस्ट बॉडी में, छिपा |
| डेटा लिमिट | ~2048 कैरेक्टर्स (ब्राउज़र पर निर्भर) | व्यावहारिक रूप से कोई लिमिट नहीं |
| कैशिंग | कैश/बुकमार्क हो सकता है | कैश नहीं होता |
| उपयोग | डेटा फेच/सर्च करना | फॉर्म सबमिट, संवेदनशील डेटा |
| सुरक्षा | कम सुरक्षित, URL/history में दिखता है | ज़्यादा सुरक्षित (फिर भी HTTPS + validation चाहिए) |
इंटरव्यू टिप: बताएं कि असली सुरक्षा HTTPS, इनपुट validation और CSRF protection से आती है, सिर्फ HTTP मेथड से नहीं।
Was this answer clear?