Hello everyone! Today we'll talk about what expressions are in PHP.
Let's start with the fact that an expression in PHP is generally any string, number, object, array, or something else that is presented explicitly or in the form of some kind of computation. For example:
'string with spaces' 7 2 + 2 5 / 2
All of the above are expressions. In this case, the expression must have some meaning. For the examples above, this is, for example, a line with text, an integer 7, an integer 4 and, finally, a fractional number 2.5.
As you can imagine, expressions by themselves are useless until they are used. The beauty of PHP is that it can be embedded in HTML.
I hope everyone knows what HTML is and can work with it.
Well, PHP can be embedded directly into HTML.
Let's edit our index.php file with you. Let's enter the following code into it:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>An example of embedding PHP in HTML</title> </head> <body> <h1>An example of embedding PHP in HTML</h1> 2 + 2 = <?php echo 2 + 2; ?> </body> </html>
and see the result in a browser.
As we can see, the part of the code that was inside the <?php...?> tags was successfully executed. If we now look at the source code of the page in the browser, we will see only the resulting result.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>An example of embedding PHP in HTML</title> </head> <body> <h1>An example of embedding PHP in HTML</h1> 2 + 2 = 4 </body> </html>
Once again, PHP code is executed on the server side. Only the result is sent to the browser, to the client. Hope this is clear.
Let's now figure out how exactly what we saw happened. The web server sees that a .php file has been requested.
He understands that there will be code in PHP, and passes the contents of the file to the language interpreter. He, in turn, knows that the code is located between the tags <?php ...?> And as soon as he meets them,
then it executes this code. After that, it gives the result of the execution to the web server, and already it simply sends the resulting result to the browser.
If something is not clear now - ask a question in the comments. I will explain in more detail.
The expression, as we have already said, can be represented in the form of calculations. For calculations, in turn, several operators can be used.
We will talk about this in the next lesson.