PHP Syntax
When PHP Interpreter parses a php code, It looks opening and closing tag of php code which are '<?php' and '?>'. These opening and closing tag tells the Interpreter to interpret the code between the '<?php' and '?>' tag.
Does PHP also support short open tags '<?'.
- This feature is available if enabled this short_open_tag in the php.ini configuration file.
- like C language php requires a semicolon at the end of every statement.
End Tag : ?>
Let us take a basic program to understand the php syntax
<?php
// some code goes here...
?>
How to make a first php program?
Step 1: Open the text editor.
Step 2: Write some php code between <?php and ?> these tag.
Step 3: Save this file named "index.php", .php extension is must for execute this program in PHP parser.
Step 4: Save this file anywhere inside the root directory of the webserver or in your local web server's root directory.
Step 5: Open the web browser and type "http://127.0.0.1/index.php" or "http://localhost/index.php" in the URL address bar and hit enter.
Write a program named "index.php" & save it
<!DOCTYPE html>
<html>
<head>
<title>A Simple Program</title>
</head>
<body>
<?php
echo "<h1>This is my first PHP program</h1>";
?>
</body>
</html>
Output:
This is my first PHP program
How to use comments in PHP?
PHP supports both single-line and multiline comments.
<?php
// This is the single-line comment
# This is the single-line comment
/*
This is the multiline comment
line 1
line 2
:
:
line n
*/
?>