PHP Introduction | Server Preparation | Let's Run a PHP Script!
Now, let us use PHP right away. To use a PHP program, save the PHP file in the public directory of the web server. Files placed there are displayed in the browser as they are loaded by the web server.
Open the folder where XAMPP is installed, usually the xampp folder on the C drive, and then open the htdocs folder inside it. This folder is the public directory of the XAMPP web server. If you place HTML and PHP files here, you can access and view them from a browser.
Now create a folder named sample there, and inside it create a text file named index.php. The source code to write in this file is as follows.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8" />
<title>sample page</title>
</head>
<body>
<h1>Hello PHP!</h1>
<div>
<?php echo "PHP으로 표시한 텍스트입니다."; ?>
</div>
</body>
</html>
Here, the text encoding is set to UTF-8. All later articles in this series use UTF-8. Also, when using PHP, add the .php extension to the file name. There are cases where other extensions can be used, but using the .php extension is the basic approach.
When the file is ready, access http://localhost/sample/index.php in a web browser. The final index.php can also be omitted because it is called by default. You can confirm that the title Hello PHP! appears on the screen and that PHP으로 표시한 텍스트입니다. is displayed below it. The text below is executed by the PHP script.
As you can see from the source code, even if the file has the .php extension, its contents are basically no different from ordinary HTML. The biggest feature of PHP is that it can be mixed with HTML code. In other words, you do not need to write everything only with PHP statements. You can write the screen layout and other parts in HTML, and write PHP processing only where it is needed.
If you look closely at the code, you can see an unfamiliar tag written inside the HTML tags.
<?php echo "PHP으로 표시한 텍스트입니다."; ?>
This is the part where the PHP script is written. PHP scripts are written by using the <?php ?> tag. If you write the processing to execute inside this tag, that part is executed when it is loaded on the server, and the result is sent to the browser. In other words, PHP scripts are all executed on the server, so they are not displayed in the browser at all. This is a major difference from JavaScript and similar scripts. JavaScript is sent to the browser as script code and runs in the browser.
Here, the echo function is used. In PHP, most major features are prepared as functions. The echo function is executed as follows.
echo output_value;
Notice that a semicolon (;) is added at the end. In PHP, a semicolon must always be added at the end of a statement. PHP does not separate statements by line breaks. It separates them with semicolons. Therefore, for example, you can write a long statement while breaking lines at appropriate places. Errors will not occur if you add semicolons correctly.