PHP is a scripting language that is used to make web pages interactive. It has become very popular due to its ease of use, integration with HTML code and the ability to make web pages interactive. Just think about how the wikiHow site works when you try to modify the content of this article: behind this very simple process are dozens, possibly hundreds, of PHP scripts that control how web pages change under certain conditions. This article explains how to create a simple script in PHP so that the user can understand how it works.
Steps
Part 1 of 3: The Echo Instructions
Step 1. Launch a text editor
This is the program you will need to use to create and modify the script code.
- The "Notepad" text editor is integrated into all versions of Windows; you can start it by pressing the key combination ⊞ Win + R and typing the command "notepad".
- TextEdit is the Mac text editor; it can be started by accessing the "Applications" folder and clicking on the "TextEdit" icon.
Step 2. Enter a simple PHP instruction inside the "Notepad" app window
Each section of PHP code begins and ends with a pair of appropriate "" tags. The PHP language "Echo" instruction is used to print a message on the screen. The message text to be displayed on the screen must be enclosed in quotation marks and the "echo" instruction must end with the semicolon symbol.
The syntax of the "echo" statement is as follows
Step 3. Save the file using the name you prefer, for example the classic "hello world" and the extension ".php"
Go to the "File" menu and choose the "Save As" option.
- If you are using the "Notepad" editor, add the ".php" extension to the end of the file name, including it in quotation marks. This way, you can be sure that the file will be saved as indicated and will not automatically be converted into a text document. If you don't use quotes, the file will be saved as text and named "hello world.php.txt". Alternatively, you can click on the "Save as" drop-down menu and select the "All files (*. *)" Option. In this case, the quotes will not be needed.
- If you are using TextEdit, you will not need to enclose the filename in quotes. However, a pop-up will appear asking you to confirm your willingness to save the file in "PHP" format.
- Make sure you save the PHP file in the document server root folder. Normally, this folder is called "htdocs" and is located in the Apache server installation folder on Windows or the "/ Library / Webserver / Documents" directory on Mac, but it can be manually changed by the user.
Step 4. Access the PHP file you just created using your internet browser. Start the browser you normally use, click on the address bar and type the URL of your PHP file: https:// localhost / hello world.php. The browser should execute the "echo" statement in the file and show the corresponding output.
- If you get an error message, make sure you have entered the source code correctly as shown in the example and that you have included the colon.
- Also make sure you have stored the file in the correct folder on the web server.
Part 2 of 3: Using PHP and HTML
Step 1. Learn to use "php" tags
The tags reserved for the PHP language, "" tell the PHP interpreter that all the text contained between the two indicated tags represents the PHP source code. All the text present outside the two tags indicated must instead be handled as normal HTML code, so it must be ignored by the PHP interpreter and sent directly to the internet browser as it normally happens. The important concept that needs to be understood from this description is that PHP scripts are embedded within the HTML code of web pages.
Step 2. Understand the function of individual instructions placed inside PHP tags
These instructions are used to give orders to the PHP interpreter. In this case, the "echo" instruction is used to print a specific message on the screen.
In reality, the PHP interpreter does not print any content on the screen: all the output it generates based on the commands entered in the scripts is then sent to the browser in the form of HTML code. The internet browser, for its part, does not know that the HTML code it is processing was generated by the PHP server. The browser is simply doing the job it was designed for, which is interpreting the HTML code and displaying the result
Step 3. Use HTML tags inside PHP instructions to display bold text
HTML tags can be used to alter the output generated by PHP scripts. The tags " " And ""are used to display the text in bold. These tags appear before and after the text to be formatted in bold, but must be placed inside the quotation marks of the PHP" echo "instruction.
-
In this case, the source code of the PHP script should look like this:
<? php?
echo Hello World!
";
?>
Step 4. Save the document and open it using your internet browser. Go to the "File" menu and click on the "Save As" option. Save the new document using the name "helloworld2.php", then open it through your browser by typing the following URL into the address bar: https://localhost/helloworld2.php. The content of the output will be the same as in the previous example, but this time the message will be formatted in bold.
Make sure you save the PHP file in the document server root folder. Normally, this folder is called "htdocs" and is located in the Apache server installation folder on Windows or the "/ Library / Webserver / Documents" directory on Mac, but it can be manually changed by the user
Step 5. Edit the PHP file by adding a second "echo" statement
Remember that individual PHP statements must be separated by a semicolon.
-
At this point, the script sample code should look like this:
<? php
echo "Hello World!"
;
echo "How are you?";
?>
Step 6. Save the new file with the name "hello world double.php"
The internet browser will print the output of the two instructions on the screen using two separate lines. Look at the tag"
in the first PHP statement: this is an HTML tag that is used to insert a line break.
-
Without using the tag"
, the output of the script would be the following:
Hello World! How are you?
Part 3 of 3: Learning to Use Variables
Step 1. Imagine that variables are nothing more than data containers
In order to manipulate and manage data, whether they are numbers or words, they must be stored in special containers, that is, in variables. Variables must first be declared in order to be used. The syntax of the PHP language used for declaring a variable is the following: "$ Variable =" Hello World! ";".
- The dollar sign ($) placed at the beginning of the variable name tells the PHP server that the text "$ Variable" is actually a variable. All variables in PHP are marked with a dollar sign, but you can use whatever name you like as a name.
- In the above example, the string "Hello World!" was assigned to the variable "$ Variable". By doing this, you are telling the web server's PHP interpreter to store the value that is to the right of the equal sign within the variable that is to the left of the equal sign.
- Variables that contain a textual value are known as "strings".
Step 2. Use variables
When referencing a variable within code, that action is referred to as "getting" a variable. Start by declaring a variable, then use an "echo" statement to print its contents instead of a text message.
-
The code to use should look like this:
$ Variable = "Hello World!";
echo $ Variable;
?>
Step 3. Save the file and run it. Go to the "File" menu and click on the "Save As" option, then assign the name "first_use_variable.php" to the document. Launch your preferred browser and use it to load the URL https://localhost/myfirstvariable.php. As a result, you will see the contents of your variable appear on the screen. The output generated by the script is identical to that of the previous example, where you used a text message inserted directly into the "echo" statement, but it was obtained differently.
Make sure you save the PHP file in the document server root folder. Normally, this folder is called "htdocs" and is located in the Apache server installation folder on Windows or the "/ Library / Webserver / Documents" directory on Mac, but it can be manually changed by the user
Step 4. Use variables to manage numerical data
Variables can also contain numbers (known as "integers"), which can then be manipulated with simple mathematical functions. Start by declaring three variables named "$ SmallNumber", "$ LargeNumber" and "$ Total" respectively.
-
At this point, the source code should look like this:
<? php
$ SmallNumber;
$ BigNumber;
$ Total;
?>
Step 5. Assign two integers to the first two variables
Assigns an integer value to the variables "$ SmallNumber" and "$ LargeNumber".
- Note that integers do not have to be enclosed in quotes like strings. Otherwise, they would be handled as plain text and no longer as numbers, as in the case of the variable to which the string "Hello World!" Has been assigned.
-
At this point, the source code should look like this:
<? php
$ SmallNumber = 12;
$ BigNumber = 356;
$ Total;
?>
Step 6. Use the third variable to calculate the sum of the two numbers and print the result on the screen
Instead of performing the calculations manually, you can recall the two variables and store the result in the "$ Total" variable. Using a mathematical operator, the computer will automatically calculate the sum of the two numbers. To print the result on the screen, it is necessary to use an "echo" instruction which will recall the variable containing the sum of the indicated values after it has been calculated.
- All changes to the contents of the variables that have been made by the program will be displayed on the screen through the "echo" instruction and the "$ Total" variable.
-
At this point, the source code should look like this:
<? php
$ SmallNumber = 12;
$ BigNumber = 356;
$ Total = $ SmallNumber + $ LargeNumber;
echo $ Total;
?>
Step 7. Save the script and run it
The internet browser will show a single number, characterized by the sum of the two variables "$ NumeroPiccolo" and "$ NumeroGrande" which has been stored in turn in the "$ Total" variable.
Step 8. Review the use of "string" variables
Using a variable to store the text inside it allows you to call this variable at any point in the code where you need to use the text inside, rather than having to rewrite it every time. They are also used to perform more complex operations on textual data.
- The first variable, "$ VariabileUno", contains the text string "Hello World!". Unless you change its contents, the "$ VariabileUno" variable will always contain the string "Hello World!".
- The "echo" instruction will print the contents of the "$ VariabileUno" variable on the screen.
Step 9. Review how "integer" variables are used
You have already learned how to use integer variables using very simple mathematical functions. You also discovered how to store the result of these operations inside a third variable, but this is only a small part of what can be done using numeric variables.
- The two variables "$ SmallNumber" and "$ LargeNumber" both contain an integer.
- The third variable, "$ Total", contains the sum of the values stored in the "$ SmallNumber" and "$ LargeNumber" variables. In the previous example, the variable "$ NumeroPiccolo" was assigned a numeric value as well as the variable "$ NumeroGrande", after which the sum of these values was assigned to the variable "$ Total". This means that any modification to the values of the first two variables will consequently alter the value assigned to the latter.
Advice
- This article assumes that the Apache web server and its PHP interpreter / server have already been installed on your computer. Whenever you are instructed to save a PHP file, it must be stored in the "\ ht docs" (on Windows) or "\ Library / WebServer / Documents" (on Mac) folder in the Apache installation directory.
- Commenting on the source code is a fundamental step for any programmer. It is used to ensure that anyone who will have to manage the code created by another person can quickly understand its operation and the purpose of each instruction. For this reason, always remember to comment your PHP code correctly.
- A great tool, very useful for testing the PHP files you create, is the XAMPP platform. It is a free software suite that includes an Apache web server and a PHP server that will allow you to simulate the operation of a server on your computer.