Interview question
What is the difference between echo and print in PHP? PHP में echo और print में क्या अंतर है?
Answer
Short Answer
Both echo and print are language constructs used to output data to the screen. The main differences are that echo can take multiple parameters and has no return value, making it marginally faster. print can only take a single argument and always returns 1, meaning it can be used in expressions.
Comparison Table
| Feature | echo |
print |
|---|---|---|
| Return Value | None (void) | Returns 1 |
| Multiple Arguments | Supported (e.g., echo "a", "b";) |
Not Supported |
| Speed | Slightly faster | Slightly slower |
| Usage in Expressions | No | Yes (e.g., $val = print("Hi");) |
Code Example
// echo multiple strings
echo "Hello ", "World", "!";
// print returning a value (useful in ternary operators)
$isPrinted = print("Hello World!"); // Outputs: Hello World!
echo $isPrinted; // Outputs: 1
// echo inside an expression will cause a Syntax Error
// $val = (echo "test"); // ERROR!
Interview Tip
In modern PHP development, always use echo. The speed difference is microscopic, but echo is the universally accepted standard in the PHP community.
दोनों डेटा आउटपुट करते हैं, लेकिन छोटे अंतर हैं जो इंटरव्यूअर पूछते हैं।
| फीचर | echo | |
|---|---|---|
| रिटर्न वैल्यू | कोई नहीं (void) | हमेशा 1 |
| आर्गुमेंट्स | कई, कॉमा-सेपरेटेड | सिर्फ एक |
| स्पीड | थोड़ी तेज़ | थोड़ी धीमी |
| एक्सप्रेशन में | इस्तेमाल नहीं हो सकता | हो सकता है |
echo "Hello", " ", "World";\nprint "Hello World"; इंटरव्यू टिप: बताएं echo स्पीड और मल्टी-आर्ग सपोर्ट के लिए पसंदीदा है।
Was this answer clear?