Basic PHP Syntax - Lesson 1
This lesson marks the real beginning of our PHP journey. Here, we’ll start executing PHP scripts and generating HTML pages using PHP code. Right from the first lesson—we’re diving in! I’ve intentionally named this one "Lesson 1" because I prefer action over lengthy introductions. If you’re curious about the history of programming languages or PHP itself, you can explore that later on your own.
Important: In the video, you might see Denwer being used, but it is outdated and no longer supported. Instead, we recommend using Open Server:
https://drupalbook.org/ru/drupal/1-ustanovka-i-rusifikaciya-drupal-8
Syntax (from Ancient Greek "composition, structure, order") — is the study of sentence and phrase structure in linguistics.
Tools You'll Need
To begin programming with PHP on Windows, we recommend the following tools:
- Open Server – A platform that will serve as your local web server. It allows you to run websites locally and view results in your browser.
Download from: https://ospanel.io/ - Notepad++ – A lightweight text editor with syntax highlighting. This helps you easily read and edit code.
Download from: http://notepad-plus-plus.org/
Need help installing and setting up Open Server?
Check the guide here:
https://drupalbook.org/ru/drupal/1-ustanovka-i-rusifikaciya-drupal-8
Writing PHP Code
All PHP code begins with <?php
and ends with ?>
. PHP script blocks can be placed anywhere within an HTML document.
<?php // PHP code goes here ?>
Some servers allow a shortened tag <?
, but we recommend using full opening tags <?php
to avoid compatibility issues across different servers.
<?php ?>
Creating index.php
Let’s start coding. Create a file named index.php. This will be the default file your server loads first.
Inside that file, place the following code:
<html> <body> <?php echo "Hello World"; ?> </body> </html>
When you navigate to http://test
in your browser, you should see the output:
Hello World
Using print() Instead of echo()
You can use print()
instead of echo()
. Both produce the same result:
<html> <body> <?php print "Hello World"; ?> </body> </html>
File Extension
Make sure your file ends with .php
. If you save it as .html
, the PHP code will not execute.
PHP is an interpreted language. When a server processes a .php file, it looks for PHP code and executes it. Unlike compiled languages where the program must be converted into an application before execution, PHP executes directly and will attempt to generate as much output as possible, even if errors occur in parts of the code. This means you can still see some output even with small code mistakes.
PHP Comments
In PHP, use //
for single-line comments and /* ... */
for multi-line comments. Comments are helpful for explaining your code and are ignored during execution.
<html> <body> <?php // This is a single-line comment /* This is a multi-line comment */ ?> </body> </html>