Spex3
    

PHP Syntax and Structure


    

    

PHP has a simple and flexible syntax that is easy to learn. It allows embedding PHP code within HTML, making it a powerful language for web development.

1. PHP Tags and Code Blocks
PHP code must be enclosed within PHP tags so that the interpreter recognizes it.

Opening and Closing PHP Tags

< ? php
// PHP code goes here
?>
✅ Example:


< ? php
echo "Hello, PHP!";
?>
Short PHP Tags (Alternative)
Some servers support short tags, but they must be enabled in php.ini.


echo "Short tags example";
?>
⚠️ Note: Not recommended because short tags are disabled by default in modern PHP versions.

2. PHP in HTML
PHP can be embedded directly into an HTML page.

✅ Example:



<!DOCTYPE html>
<html>
<head>
<title>PHP in HTML</title>
</head>
<body>
<h1>Welcome</h1>
<p>The date today is: < ? php echo date("Y-m-d"); ?></p>
</body>
</html>
💡 Here, the line dynamically prints the current date.

3. PHP Comments
Comments are used to make code more readable and to explain what certain parts do. Comments are ignored by the PHP interpreter.

Single-Line Comments
Using // or #

// This is a single-line comment
echo "Hello, World!"; # Another single-line comment
Multi-Line Comments
Using /* ... */


/*
This is a multi-line comment.
It can span multiple lines.
*/
echo "This is PHP!";

💡 Best Practice: Use comments to describe complex logic but avoid over-commenting obvious code.

4. Basic PHP Code Structure

< ? php
// Variables
$name = "John Doe";

// Output
echo "Hello, " . $name;

// Conditional Statement
if ($name == "John Doe") {
echo " - Welcome!";
}

// Loop
for ($i = 1; $i <= 3; $i++) {
echo " Iteration " . $i;
}
?>
🔹 This example demonstrates variables, output, conditions, and loops.


    Date: 2025-03-28 00:00:00.000000