Starting out - tags - echo - nothing works without HTML - but how does PHP work?

I'm Karsten and I want to help people learn German.
Buy me a coffee if you feel I did well.

Starting out - tags - echo - nothing works without HTML - but how does PHP work?

There's a button below (sneak peek) to peek at the code.

Starting from here, I'd like to remind you of the button "Sneak peek of the server side code" below. We are going to need this one. In my text editor the main content of this paragraph consists of a few lines, inside a p-element (paragraph). The text is in-between <p> and </p>. This is the one element you as a writer are going to be using most of the time. Press "Sneak peek of the server side code" now and look at the code and how that which is visualized is visualized by the browser.

So what you see in the window, is what the browser creates out of the text that is located inside the p-element.

This code is stored somewhere on a server somewhere. I don't know the exact location of the server and I don't have to, but I know the code is a physical instance, it is substantial. It is sent to the browser by the server, having been requested by you, the user.

Note the file ending in the URL at the top. It is php, and not html. Why is this so? What is actually happening is, we are telling our server: "Look here, buddy, this page is not pure html, there is some php-code mixed in. Before you send it to the user make sure you execute the php-code first.

And this is what is happening. The server executes php-code such as this: (press "sneak peak" button to view php-code)

Php says:


What is happening now? There is a program on my server, the php-interpreter, it is told that it needs to create a container, into which something can be put. At the same time you are telling the interpreter that the content of the container, named $variable, should be "sugar". The echo command tells the interpreter to send the p-tags and the content of the variable, which is "sugar", to the browser. This will output sugar Remember that each command is seperated by a semicolon (;).

You can then put something else into our container:

Php says:


That is why it is called a variable. Because you can change its content. The content is "variable" or interchangeable.

Note the <?php at the beginning and ?> at the end. These tags are the php-tags, telling the interpreter, hey dude, this is where in-between you will find php-code, do please execute it now.

From what we have talked about, I hope that you get that there are two types of code. One on the server (which is displayed by pressing the "sneak peek" button) and the other being that which the browser receives from the server:

source code

This is a screenshot of the code behind the site that you get if you right-click on the page (not on the image) and chose "show source" like this:

how to show the source code in browser

So now, do you see the difference between this:

and this?

Note the distance between the paragraphs. Looking at the page, 'echo "\n";' makes no difference, but <br/> does.

Now looking at the code, you will see that the browser receives 'echo "\n";' and that it does make a difference:


Thing is, you don't see it HERE, because the new lines are only generated in the code that the browser receives and all browsers do the same to whitespaces and new lines, they ignore them.

>> continue

submit to reddit

Sneak peek of the server side code