Hooray! You got PHP installed!

Lesson 1

So let’s see what’s happening with that phpinfo.php file, that makes it go from just

<?php phpinfo(); ?>

in the phpinfo.php file you made, and turns it into this colorful table in the web browser.

So, what you have installed are (1) a web server, and (2) a PHP interpreter. Because PHP was created originally as a scripting language for making more interesting web pages, it almost always runs in coordination with a web server – an app that exists to receive internet requests and send back the requested data. That’s your IIS.

Behind the scenes, when you go to http://localhost/phpinfo.php in your web browser, the browser sends an HTTP “request” to the webserver on your computer (that’s localhost) saying, please send me whatever data you have on hand for phpinfo.php.

The web server looks at that file and says, "Oh its a .php file there." And because it has the PHP 7.4 interpreter registered to that file extension, it first sends the file to the PHP interpreter.

The PHP interpreter goes through that file and leaves alone anything that’s not between the <?php and ?> symbols, or tags. (In this case there isn’t anything that’s not between those tags, but there could be other HTML (web page content) there. The interpreter replaces those tags and anything between them with whatever directives it finds there. When all those directives have been processed and replaced into the document, it sends the result back to the web server, which then sends the whole thing back to your browser.

Ideally, everything that gets sent back is something the browser can read and present.

Looking at the phpinfo.php file, what’s between the PHP tags is phpinfo();. phpinfo() is an internal PHP function. Breaking that down, it’s internal and it’s a function:

  • Internal means its built in to PHP. PHP has a lot of these, but you will also be able to write your own functions, which will be user functions (because they were created by you, a PHP user).
  • A function is a “paragraph” of code that sticks together and (most of the time) has a label that can be used as a way of saying, “call in and process this paragraph now”.

So, phpinfo() tells the interpreter to call the built-in “paragraph” of code that generates that table and insert the results into the document.

Finally, the ; is like a period (because . is used for something else). Every line, or “sentence” of PHP ends with the semicolon, to say, “done with that thing; start a new thing now”.

One last thing about the phpinfo.php file before we move on:

On pages where a PHP section is the only thing or the last thing in the file, you don’t need the ?>, and most of the time it’s better not to use it. So, in this case, where we have just the one line of code, we could write it without that last ?>. So,

<?php phpinfo();

should work exactly the same. As a little exercise, you can remove the ?> from the phpinfo.php file and reload the page in your browser to give it a whirl.

To Review

We’ve covered:

  • The web server and the interpreter, and how they work together.
  • PHP (and other programming languages) make use of functions to process code in chunks or “paragraphs”. Some of these are built into the language themselves, and you can create others.
  • A basic rule is that every PHP line must end with a ;.

(This last point seems trivial, but it’s one of the things forgetting tends to cause a great deal of frustration with new programmers. If a line is missing a ;, it will always give you an error, and those errors are often hard to track down.)

Next Up: Variables

Variables, same as algebra, are using a symbol or name to represent something else. The function name phpinfo() was being used as a variable, since it was a name for the whole chunk of code. In fact, functions are a specific type of variable in PHP: a callable (because you use it to call the block of code it represents).

Homework

Variables come in several varieties in PHP, depending on what kind of information they represent. Each language has it’s own set of variable types to work with. To get an overview in preparation for the next lesson, take a read of the PHP documentation to see the available variable types we have to work with in PHP.