Before we write our first PHP script, we must first theoretically explain how loading a page works with using PHP.
First, a user calls a specific URL in their web browser, such as https://phpweb.org
Subsequently, the web browser builds a so-called request , which is a special request for a web server that is sent to the Internet. It contains information about the requested page, basic identification of the browser and settings, information about cookies and so on.
This request travels through the Internet to a web server (most often Apache ), which reads the request and begins to compile a response.
Since the called URL is a PHP script and we request a file called index.php , Apache reads the index.php file from the root of the disk and passes it to the PHP interpreter , a program that can handle the PHP code itself and compile the HTML code that is sent back to the user.
Once the HTML code is built, the response is sent back to the user (called the response ) and the web browser renders the page in the normal way as if it were pure HTML.
Note that the web browser does not know anything about the content of the PHP script at all, but it processes up to the generated HTML so that your scripts and server content remain safe.
Writing the first script assumes you have a web server running on your computer. For Windows, XAMPP is best (download PHP 7.0 or later), on Mac, XAMPP works just like Windows. For Linux I recommend LAMP server (this website is running on Lamp server).
The PHP script file name must end with a .php extension to let the web server know that we want to process it according to PHP rules. So let's create an index.php file that contains the code for the main page of our site.
Open this file in a suitable text editor to write the source code.
In Windows, for example, Sublime Text is a good starting point, which nicely colors the syntax (language rules) and makes the code easier to read. Later I recommend to buy PhpStorm, which is widely used in companies and offers the possibility of programming in more people.
You need to use the basic look of an HTML page:
<!DOCTYPE HTML>
<html>
<head>
<title>My First script</title>
<meta charset="UTF-8">
</head>
<body>
</body>
</html>
All HTML code will be processed in the usual way and will be our great helper for designing pages. PHP uses the principles of HTML and CSS.
PHP is mainly a template language that generates its own content at appropriate places in the code. To clearly say what HTML is and what PHP is, we need to use a separator tag.
Currently, it's best to use a listing with Php and?> .
<?php
// Here will be PHP code
?>
If you want to use some additional HTML code, we include the ?> , If there is no more HTML code at the end of the PHP script, it is preferable not to include the ?> Tag so that there are no unnecessary whitespace (spaces and lines) at the end of the page that can be inserted by a text editor. .
In the past, the <? instead of <? php , but may not always be supported.
You can place wrapping tags anywhere in the HTML code, such as the body of the page:
<!DOCTYPE HTML>
<html>
<head>
<title>My First script</title>
<meta charset="UTF-8">
</head>
<body>
<?php
// Here will be PHP code
?>
</body>
</html>
All constructs (language expressions), statements, and functions are separated by semicolons to clearly identify where and from which the current construct is valid.
The semicolon is usually followed by a line. Symbolically written:
command;
another command;
variable x = its value;
write variable x;
save to file;
The echo construct is used for writing content. It is very easy to use:
echo 'Hello World!';
It then writes the text "Hello World!" Into the HTML code. Try the demo.
All other samples will only contain the inside of the PHP code. You can guess the surrounding HTML code (for example, at the beginning of the article).
Variables are virutal memory locations where data is stored and moved. The variable name always starts with a dollar, followed by the name itself, and then its value.
$favouriteNumber = 1024;
$authorName = 'Mark Weber';
echo $favouriteNumber;
echo '<br>';
echo $authorName;
The variable name should reflect what the variable actually contains to make the code clearer. Also note the insertion of the HTML tag <br> for line feeds. You should already know this tag from HTML.
Variables are virutal memory locations where data is stored and moved. The variable name always starts with a dollar, followed by the name itself, and then its value.
$favouriteNumber = 1024;
$authorName = 'Mark Weber';
echo $favouriteNumber . '<br>' . $authorName;
After connecting the strings with a dot, the whole thing will be viewed as one large string.
Among the variables, all basic mathematical operations work intuitively as expected. Let's define 2 variables and substitute numbers into them:
$x = 5; // defines variable x with value 5
$y = 3; // defines a variable y with a value of 3
echo $x + $y; // add variables and print 8
Note that an equal sign (=) is not used to perform a mathematical operation, so you cannot write equations, for example. PHP acts like a calculator in this respect.
If we don't want to use variables, we can do the operations directly. The place of operations is therefore irrelevant and evaluated anywhere.
echo 5 + 3; // prints 8
Alternatively, we can add the variables and save the result in another variable:
$x = 5;
$y = 3;
$z = $x + $y; // variable $z contains 8
echo $z; // prints 8
In the next section, we will learn the complete fundamentals of variable definition and its use.