Echo — PHP Technical Interview Questions & Answers
Echo
Echo
- echo sends text to the page. echo 'Hello'; echo $name; You can echo several values: echo $a, $b; No return value. print is similar but returns 1 — almost nobody cares. Use echo.
- <?= $name ?> is short echo inside HTML. Need short_open_tag / PHP 5.4+ echo short tag. Fine in modern PHP.
- Trap: echo array — Array to string conversion. Use json_encode or a loop. Trap: missing quotes around strings.
Technical Interview Tip: echo 'Hello'; Don’t echo arrays.
Code Examples for Echo
Echo — working example
<?php
// Echo
$scores = [80, 90, 70];
$avg = array_sum($scores) / count($scores);
echo "avg=" . number_format($avg, 1) . "\n";
foreach ($scores as $s) {
if ($s >= 80) echo "high: $s\n";
}Echo: scores array, print avg, then high marks. Say the output.
How to Explain Echo to an Interviewer
echo vs print?
Both print echo is the usual one print returns 1