Beginners Guide to PHP Part 1

By: Ryan Goose

Topics: php

Beginners Guide to PHP Part 1

PHP stands for “PHP Hypertext Preprocessor”, which is an acronym that refers to itself (oh, those clever geeks). PHP is, is an interpreted language most commonly used for web development. This means that you write lines of code in a text file, run the text file through a PHP parser (AKA an Interpreter), then the parser executes the actions specified. So how do you accomplish this?

Syntax

As with any programming language you must specify what actions you want to take place, and in what order to do so, by use of a structured writing mechanism known as “syntax”. Just as the English language has it’s own ruleset to define spelling, grammar, and structure, so does a programming language.

  • PHP Tags

All PHP files you wish the parser to execute need to have a .php extension at the end of the file name. Often this extension is hidden via URL rewriting, however, that is a topic from another discussion. Inside of a .php file, you can use regular HTML, basically develop like you’re developing a basic HTML site. However, when you want to use PHP, you must specify open and close PHP tags. These tags tell the parser that anything within these tags is PHP code that should be interpreted as such. If you do not specify these tags, then your PHP code will be interpreted as plain text/HTML. If you put non-PHP code within these tags you will get an error message giving you some parsing error.

PHP Open and Close Tags

  • <?PHP
  • // PHP Code
  • ?>
You May Also Like  Creating Secure PHP Applications

As you can see above, this is the end tag that marks the end of the PHP Code. You can feel free to intermingle these as much as you want with regular HTML.

However, keep in mind, that it is often best to separate PHP code, page structure (HTML), and presentation (CSS). Though, it is certainly possible to do so, mixing these too much will make your site and code harder to maintain.

  • The Semi-Colon

When you finish a sentence in English, you must use a period, a “.”, to tell the reader you’re done with that statement. The same is true with PHP, at the end of a statement you need to use a “;” to tell the parser that the statement is done and ready to be parsed.

Some things to note here. First, the “;” is marking the end of each statement, but as you can see from the second echo statement, you can span multiple lines of code before you use the semi-colon to mark the end of your statement. The third echo is just showing you that spacing doesn’t matter in terms of where the echo is placed, nor the spacing for the semi-colon, though it is considered bad form.

  • Code Commenting

In PHP you can use comments. Comments are used to document the use of the code directly before or after the PHP code and are not interpreted as code themselves (the parser ignores them). Beginners often make the mistake of not commenting on their code, assuming they’ll remember what they were trying to achieve. However, you will be surprised at how fast one’s memory fades, and you will be grateful that you commented on some code when you come back to it. However, PHP comments have another important use, debugging. If you’re programming and you no longer need a line of code but want to keep it around just in case, you can comment it out. Or perhaps you believe this line of code, while not producing any PHP errors, is messing with your code logic, you can comment it out.

You May Also Like  Deploying PHP Apps to the Cloud