PHP tags
PHP code goes between <?php and ?>:
<?php
echo 'Hello';
?>
In files that contain only PHP, leave out the closing ?> — it avoids accidental spaces in the output.
Statements
- Every statement ends with a semicolon
; - Code blocks use curly braces
{ }
Case sensitivity
- Variables are case-sensitive:
$nameand$Nameare different - Keywords and function names are not:
echo,ECHOboth work (but use lowercase)
Mixing PHP and HTML
You can switch between HTML and PHP anywhere. The short echo tag prints a value: <?= $name ?>
Comments
// single-line comment
# also single-line
/* multi-line
comment */
Example
Example — PHP inside HTML
PHP
<?php
$student = 'Asha';
$marks = 86; // out of 100
/*
Decide the result
*/
$result = $marks >= 33 ? 'Pass' : 'Fail';
?>
<!DOCTYPE html>
<html>
<body>
<h2>Result card</h2>
<p>Name: <?= $student ?></p>
<p>Marks: <?= $marks ?></p>
<p>Result: <strong><?= $result ?></strong></p>
</body>
</html>
Output: Result card / Name: Asha / Marks: 86 / Result: Pass