PHP - Syntax Overview
PHP Syntax
1. Embedding in HTML:
PHP code is typically embedded within HTML files using the following tags:
Opening: ?php
Closing: ?
Everything within these tags is treated as PHP code and executed on the server before the HTML is sent to the browser.
2. Comments:
Single-line comments start with // and extend to the end of the line.
Multi-line comments use /* and */ to enclose the commented section.
3. Variables:
Variables are declared using a $ symbol followed by the variable name. They can store different data types like integers, strings, arrays, and objects. Example: $name = "John"; $age = 30;.
4. Operators:
PHP supports various operators for performing arithmetic, comparison, logical, and other operations. Examples: +, -, *, /, ==, !=, &&, ||.
5. Control Flow:
Statements like if, else, for, while, and switch control the flow of program execution based on conditions and loops.
6. Functions:
Functions allow you to define reusable blocks of code that can be called multiple times. They are declared using the function keyword followed by the function name, parameters, and the code body.
7. Arrays:
Arrays store collections of data under a single variable name, accessed using an index. Example: $fruits = array("apple", "banana", "orange");.
8. Object-Oriented Programming (OOP):
PHP supports OOP concepts like classes, objects, inheritance, and polymorphism.
The most universally effective PHP tag style is −
• If you use this style, you can be positive that your tags will always be correctly interpreted.
• The default file extension for PHP files is ".php".
• A PHP file normally contains HTML tags, and some PHP scripting code.
• Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function "echo" to output the text "Welcome To CodeLines!" on a web page
Example
Output
Welcome To CodeLines!
Note:-
In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive.