What is PHP?

PHP tutorial
PHP is a scripting language, primarily intended to generate dynamical websites on a webserver. PHP is designed in 1994, by a senior software engineer at IBM, Rasmus Lerdorf, and the language was clearly inspired by Perl. The letters PHP used to mean Personal Homepage (or more complete: Personal Home Page/Forms Interpreter, PHP/FI). However, during the later developments, PHP became a recursive acronym, meaning “PHP: Hypertext Preprocessor”, which better suits what the language actually does: outputting hypertext (HTML or XHTML).
Since PHP is a server side scripting language, it means that the code is not compiled, but interpreted at the time of usage (by the interpreter, most often integrated in the webserver), and that the script runs on the server side, or the webserver. This way, the output is generated on the webserver, and sent to the client (your browser) as HTML.
PHP syntax is derived from many other languages, but mainly by C and Perl. However, since PHP5 also allows for object oriented programming, there is also an influence of Java noticeable. Despite all these different influences, PHP manages to remain simple and understandable.

The Source and the Tags

Although PHP is primarily intended as a text processor, it is often used as a pure programming language. However, to facilitate its text processor role, it can be inserted in a text file as well, by using the special PHP tags. Four different tags can be used to insert and execute PHP code in a text file:

img_0873

  • Standard tags, these are the most commonly used tags, and are the ones you should use:
    <?php

    ?>

 

  • Short tags, used to be the standard, but might interfere with XML headers, and therefor are used more rarely:
    <?

    ?>

 

One advantage is that they make really short syntax possible, such as <?=$variable ?> which prints the result of the variable straight to the output.

  • Script tags were introduced for HTML editors. These editors are programmed to ignore JavaScript code (which uses the same script tags), but were unable to ignore PHP code, which is fixed by these script tags:
    <script language=”PHP”>

    </script>

 

  • ASP tags, don’t ask, nobody seems to know why these were introduced…
    <%

    %>

 

Normally, the only tags you should use are the standard tags. Short tags, script tags and ASP tags are all considered deprecated.

The newline horror

Very important to remember is that the PHP interpreter outputs EVERY character outside the PHP tags as-is. This means every whitespace, every newline character. This is not an issue for whitespaces, but can be for newline characters, as they are used as a seperator between the header portion of the http response, and the actual data. This means that a newline character that is sent to the output before the headers are finished, will cause your script to fail. To help you prevent this problem, the PHP interpreter automatically strip the first newline character after the PHP closing tag (?>).

The body of a PHP script

The body of a PHP script consists of statements, which can be function calls, variable assignments, data output etcetera. Each statement, with only a few exceptions, needs to be terminated by a semicolon. One of the exceptions is for instance the last statement before a closing tag. However, for consistency’s sake, it is advised to always terminate your statements.

Comments are another almost vital part of any programming language. Yes, I know, they are not vital at all, but you should treat them like that. Make it a habit to write comments every function, class, method or property that you write in your code. If your code required some thinking to find how you will actually code it, it will require some thinking again later when you try to reread your code, and find out why you actually coded it that way. Comments can only make your life easier!
The different ways of inserting comments in a PHP script are these:

// a single line comment

# another single line comment

/* a multiline
comment
*/

/**
* another multiline comment
* Most often used to document API’s or functions, since they can hold large parts of structured text
* and still look… err… structured.
*/

Both single line comments (// and #) are closed by a newline character, or by the PHP closing tag.

Code blocks are a series of statements that must be executed all together, and under specific circumstances, such as a function call or a conditional statement. The statements inside code blocks are enclosed with two curly brackets ({}).

if ($whatimsaying == “yaddayadda”)
then
{
$takeanapforsometime = 5;
fall_asleep_for_how_long($takenapforsometime);
}

That’s all for today folks. More is coming up in the next lesson, which will start with Datatypes!

Share via
Copy link