PHP Syntax

PHP Tutorial

Here is a clear and beginner-friendly guide to PHP Syntax


PHP Syntax (Basic Rules You Must Know)

PHP Syntax is always executed on the server, and the result is sent to the browser as HTML.


1. PHP Script Starts and Ends With Tags

Every PHP code must be written inside:

<?php
// PHP code here
?>

Example:



2. PHP Statements End with semicolon ;



3. PHP is Case-Sensitive

  • Variables are case-sensitive

  • Keywords, functions, classes are NOT case-sensitive

Example:



4. PHP Can Be Embedded in HTML


 


5. echo & print – Used for Output


Both work the same, echo is slightly faster.


6. PHP Comments

Single-line:


Multi-line:



7. PHP Variables

Rules for variables:

  • Start with $

  • Must start with a letter or underscore

  • Cannot start with a number

  • No spaces



8. PHP Keywords (reserved words)

Cannot be used as variable names:

if, else, function, class, switch, for, while, echo, etc.

Example (❌ wrong):

$if = 10; // incorrect

9. PHP Whitespace is ignored

Spaces, tabs, new lines do not affect the program:


Both will work.


10. PHP execute on server, HTML on client

This:


Outputs this in browser:

Hello User (as HTML)

⭐ Complete Example


 

If you want, I can continue with the next chapter:
PHP Variables
PHP Data Types
PHP echo/print
PHP Operators

You may also like...