PHP is a server-side scripting language for creating interactive web pages. It has become very popular for its ease of use, interactivity features within web pages, and HTML integration. Just imagine what happens when you edit a page, even on this site. There are many, perhaps even hundreds, of PHP scripts behind this process that control how web pages change depending on different circumstances. In this article, you will learn how you can write some very simple PHP scripts to give you an overview of the basics of how PHP works.
Steps
Part 1 of 3: Getting Started with the Echo Operator

Step 1. Open the "Blockonot" text editor
This is the program you will use to write and modify your code.
- To launch Notepad on any version of Windows, you can use the keyboard shortcut ⊞ Win + R> Notepad.
- You can launch TextEdit on Mac OS through Applications> TextEdit.

Step 2. Type a simple command in Notepad
The PHP code block at the beginning and at the end is surrounded by special brackets, so-called tags (“”). “Echo” is a very simple command (computer instruction) in PHP for displaying text on the screen. The text you want to print must be enclosed in quotation marks. There must be a semicolon at the end of the line.
The code you entered should look something like this:

Step 3. Save the file with the name “hello world” and the extension.php
This action can be performed through the menu "File"> "Save As …"
- In Notepad, add the.php extension to the file name and enclose it in quotation marks. Thus, Notepad will no longer consider the file to be text and will not convert it to text format. If you don't add quotes, the filename will automatically become hello world.php.txt. As an alternative to disable the automatic addition of the.txt extension on saving, you can select “All Files (*. *)” In the File Type menu. In this case, the file name will remain as you enter it, and you will no longer need to add quotes.
- In TextEdit, you do not need to add quotation marks to the file name when you save it. Instead, a pop-up window will appear asking you to confirm that you want to save the file with the.php extension.
- Make sure to save the file in your server's document root. Usually inside the Apache server folder on Windows, this directory is called “htdocs”. For Mac, the default directory is / Library / Webserver / Documents, however the path can be adjusted when configuring the server.

4 Open the PHP file in a web browser. Open your preferred browser and enter the name of the generated PHP file in the address bar: http:// localhost / hello world.php. The browser window should display the result of the echo command.
- If you receive an error message, check that the code in the file is exactly the same as in the example. Be sure to remember to add a semicolon at the end of the command.
- You also need to check that the file is saved in the correct directory.
Part 2 of 3: Using PHP and HTML Together

Step 1. Understand how PHP tags work
The “” tags tell the PHP engine that everything between these brackets is PHP code. Anything outside these brackets is considered normal HTML, which is ignored by the PHP engine and passed to the browser like any other HTML. It is important to understand, however, that PHP scripts are directly embedded in regular HTML web pages.

Step 2. Understand how tag commands work
Commands are instructions to the PHP engine to do something. In the case of the echo command, the PHP engine is instructed to print the quoted text.
That being said, the PHP engine doesn't actually display text on the screen. The result of the engine is sent to the browser as HTML. The browser itself has no idea that it is receiving the final output of the PHP script. When it comes to the browser, its operation is completely based on plain HTML

Step 3. Use HTML tags to make the font of the text bold
Adding individual HTML tags can change the output format of PHP commands. Tags “ ” “ ”Make all text between them bold. Note that the tags must surround the text to be displayed, but they must be inside the quotes of the echo command.
-
Your code should look something like this:
<? php?
echo " Hello World!
";
?>

4 Save the file and open it in your browser. Use the File> Save As … menu and save the file as "helloworld2.php", then open it in your browser using the URL: http://localhost/helloworld2.php. The page text will not change, but the font will become bold.
Make sure you saved the file in the document root of the server. Usually inside the Apache server folder on Windows, this directory is called “htdocs”, for OSX the default directory is / Library / Webserver / Documents, but the path can be changed when configuring the server

Step 5. Modify the file by adding a second echo command
Remember that there must be a semicolon at the end of each command.
-
Your code should now look something like this:
<? php
echo "Hello World!"
;
echo “How are you doing?”;
?>

Step 6. Save and open the "hello world double.php" file in your browser
There will now be two sentences on the page, arranged in order on two different lines. Notice the new tag “
”In the first line of the PHP script. This HTML tag signals the browser to perform a line break.
-
If you don't add “
”, Then the sentences will be displayed on one line:
Hello World! How are you doing?
Part 3 of 3: Introducing Variables

Step 1. Think of variables as data stores
To process information, be it numbers or names, you need to create a kind of container for storing information. This process is called variable declaration. In PHP, the syntax for declaring a variable looks like this: “$ myVariable =“Hello World!”;”
- The dollar sign ($) at the very beginning tells the PHP engine that $ myVariable comes next. All variables must begin with a dollar sign, but the name of the variable itself can be anything.
- In the example above, the string "Hello World!" is the value and the variable is $ myVariable. You told PHP to store the value to the right of the equal sign (=) in a variable to the left of the equal sign.
- A variable containing a text value is called a string.

Step 2. Refer to the variable
A reference to a variable in your code is called a call. Declare a variable in the script and then call the echo command with the variable instead of text.
-
Your code should look something like this:
$ myVariable = “Hello World!”;
echo $ myVariable;
?>

3 Save and open the file in a browser. Use the File> Save As … menu and save the file as “myfirstvariable.php”. Open your browser and enter the address into it: http://localhost/myfirstvariable.php; the executed script will output the value of the variable. The result will be the same as for plain text, but the way you get it will be different.
Make sure to save the file in the document root of the server. Usually inside the Apache server folder on Windows this directory is called “htdocs”, for OSX the default directory is / Library / Webserver / Documents, however this path can be changed during server configuration

Step 4. Use variables with numbers
Variables can also store numeric values (such as integers) that you can perform simple math operations on. For example, declare three variables in the script: $ mySmallNumber, $ myLargeNumber, and $ myTotal.
-
Your code should look something like this:
<? php
$ mySmallNumber;
$ myLargeNumber;
$ myTotal;
?>

Step 5. Assign integer values to the first two variables
Assign integer values for $ mySmallNumber and myLargeNumber.
- Note that integers do not need to be quoted. Otherwise, numbers will be treated as string values as if they were text like “Hello World!”.
-
Your code should look something like this:
<? php
$ mySmallNumber = 12;
$ myLargeNumber = 356;
$ myTotal;
?>

Step 6. Use the third variable to sum and display the result
To avoid doing the calculations yourself, you can sum the two variables in $ myTotal. Just enter the appropriate math function and the computer will do the calculation for you. To print the result to the screen, you just need to call the echo command with a variable as a parameter. The echo command must be called after the variable declaration.
- Any change in the values of the previously declared variables will be reflected in the output of the value of the variable “$ myTotal”, printed by the echo command.
-
Your code should now look something like this:
<? php
$ mySmallNumber = 12;
$ myLargeNumber = 356;
$ myTotal = $ mySmall Number + $ myLargeNumber;
echo $ myTotal;
?>

Step 7. Save the file and open the script in a browser
Your browser should display one number. This number will be the sum of the two values determined by the calculation of the $ myTotal variable.

Step 8. Change the values of string variables if necessary
Using a variable to store text allows your code to refer to the variable instead of constantly retyping the same text. In the future, more complex manipulations can be performed with this text.
- The first variable named $ myVariable contains the string "Hello World!" Until you change the value of this variable yourself, $ myVariable will contain the given line of text unchanged.
- The echo command will print the value you specified for the $ myVariable variable.

Step 9. If required, change the values of integer variables
You've already learned the basics of integer operations using math functions. The final results of these calculations can be stored in a separate variable. At the same time, the simplest steps were considered, demonstrating the possibilities of working with variables.
- An integer has been assigned to each of the two variables $ mySmallNumber and $ myLargeNumber.
- The third variable $ myTotal stored the sum of the values of the above two variables $ mySmallNumber and $ myLargeNumber. As long as $ mySmallNumber stores one numeric value and $ myLargeNumber stores another number, $ myTotal is calculated as the sum of these two numbers. The result of the calculation can change with the change in the value of any of the original variables.
Advice
- This article assumes you have Apache and PHP installed on your PC. When the article mentions saving a file, you need to save it in the "\ ht docs" (for Windows)) or “\ Library \ WebServer \ Documents” (for Mac) directory located in the root folder of your Apache installation.
- It can be helpful to test the generated PHP files in XAMPP, a free program that will install Apache and PHP for you and help you run a local server on your computer.
- Comments are very helpful in programming in any language, so it will also be useful to read the article "How to add a comment in PHP".