1.0 Introduction

PHP as we know it today is a pretty strong and flexible language with a lot of options for web developers out there. Many of the functions and features in PHP allow web programmers like you and me create complex systems like this forum (phpBB).

With modern PHP, it is possible to connect to databases, create, use, and connect to APIs, and use a lot of plug-ins and add-ons. By that, I mean that since PHP is used pretty much world-wide and known by an estimated hundred thousand of web programmers, there are a lot of code examples out there. One example would be the PHP Extension and Application Repository; or PEAR for short.

The language is well documented from a lot of different authors (like me... this is considered as documentation), and the support for the language is extremely huge and well responsive, because a lot of people throughout the world know the language and would be able to help any body out. It's just a matter of where to look and whom to ask about it.

1.1 History

The PHP language has a pretty interesting background. PHP is a child of PHP/FI which was created by Rasmus Lerdorf in 1995 which consisted of a bunch of Perl scripts. He later rewrote it with implementations by C and released it to the public in 1997. PHP then used to stand for, 'Personal Home Page Tools'. Today though, it stands for 'Hypertext Pre-Processor'.

A bunch of University people who failed to write an eCommerce system with PHP/FI decided to strengthen the language with their own release of PHP, and the release became known as PHP 3.0. That language came to be what we know it today... or close to it.

I'll stop summarizing PHP site's history and just give you the link... History of PHP and related projects.

2.0 Installing PHP

You could follow along this tutorial by typing the PHP examples and having it parsed by downloading Wamp (Windows) or XAMPP (Everything Else). Using those local servers would enable you to follow along with the tutorial to strengthen your learning experience.

2.1 Checking your PHP

To be safe and sure, you could check if you have PHP by going to the 'www' folder (if using WAMP... I've never used XAMPP, so I don't know about that... probably 'www' or 'web' folder) and create a file there and title it test.php and put the following code in there:

<?php
phpinfo();
?>

Then open your browser, type in 127.0.0.1 or localhost in the address bar and then select test.php.

If you see anything in there, then you have PHP successfully installed. If you don't see anything... you need to look over the documentations or get support for the product you installed.

By the way, don't worry about what that code says yet... that would be explained (mostly) in the next section.

3.0 Introducing Lexical Structure

Before I continue with this section, let me define lexical structure to eliminate confusion as how it is used in this section and tutorial series.

French, German, English, etc. has a set of language rules called grammar, which tells the user how to speak the language. In this case, lexical structure is a sort of a set of grammatical rules to define how the language is typed, how the variables are set... grammar for PHP in other words.

Not only grammar though, but it's vocabulary and spelling as well. PHP has a certain amounts of grammatical rules, vocabulary used, and the spelling is important too. Without these three things, we don't really have a language, do we? So let me begin telling you the grammar for this language.

Different portions of PHP has it's own little set of grammar rules (lexical structure) and I will explain them as we come along to those portions of PHP. Right now, I'll just outline the programming language of PHP for you before we go dwell into the wild realms of PHP.

3.1 Basic Lexical Structure

Just like with any language, you usually be polite and start a conversation with a 'Hi!' and then end that conversation with a 'Bye!'. So, in this case, we have a moment when we tell the PHP parser 'Hi! You are know in the PHP section of the page." and then when we are done with our PHP, we have a moment, when we tell the PHP parser 'Good bye! You are finally leaving the PHP section of the page'. Let me introduce you to those.

<?php

?>

That is telling the PHP parser 'Hi!' and then 'Bye!' in that order. First line is 'Hi!' and the third line is 'Bye!'. We put all of our PHP code in between those two lines of code.

There are a few different ways to say 'Hi!' to the PHP parser. I like to think of them as 'slang' because I'm used to the way I showed the first time, and that is what is used 99.9% of the time (leaving there some doubt in case you come up here and tell me, "No! My friend uses the other way!"). Anyway, the 'slang' is:

<script language="php">

</script>

But like I said, just about nobody uses that 'slang' to converse with the PHP parser (or PHP engine... whatever you prefer).

3.2 Defining and Using Variables

A variable is a name that holds a piece of information, made available for you to use that piece of information for your modification needs and purposes. What you do with a variable is store some information into it and then pass it to a function, class, another variable, or use that variable in an operator... you do work on that variable or with that variable to do some function.

The way you would set a variable is:

<?php
$variable = 'Some Information';
?>

You would notice that the variable name is detonated with the dollar sign (I don't need to show the sign... we all know what a dollar sign is). A variable has it's own small grammar rules that makes it a variable and not an error detonator.

A variable should always start with a dollar sign. Followed by either a character (as in a letter) or an underscore ( $_). The character could be of any case... uppercase or smaller case (I'm sorry, lowercase). Followed by 'Z' number of characters, numbers, underlines, or dashes.

Than that variable is set equal to some information. That variable would represent that information only and nothing else. Think of math here... 2 = 2 (Two equals two), correct? Then after setting $variable to Some Information then $variable would equal Some Information. That would be correct math and you would get the problem correct.

To test your knowledge, I want you to look at the following set of variables and write down the ones you are sure are correct. The ones that are variables and not error detonators.

<?php
$var = 'Information';
$_VaR = 'Information';
$31_codes = 'Information';
$SuperCallofragilisticsexpialladocious = 'I did not spell this right';
$.dot = 'com';
?>

The correct ones are lines 2, 3, and 5 because they either start with an underscore ( _ ) or a letter (A-Z or a-z). The others are wrong because they either start off with a number or a punctuation or... well, anything else that isn't an underscore or a letter.

Using a variable is pretty simple. Consider the following code:

<?php
$variable = 'Some Information';

echo $variable;
?>

Don't worry about what the code actually does, just see how the variable '$variable' is used... well, that's it really for the usage of variables.

There is some lexical structures to this... notice that after the variable is being set to some information, there is the semi-colon at the end of each line. What that semi-colon does is tell the parser is that the PHP is done with that particular set of PHP code. It keeps the code separated into understandable portions for the parser.

It keeps expressions separated from being confused/mixed with other expressions and pretty much everything else. In JavaScript the parser automatically adds in that semi-color where the logical flow of the code seems to make sense to the parser, but not in PHP. You will get a parse error if you do that.

A little troubleshooting hint:
This is mostly a hint for the time when you actually do start some serious coding or anything like that, but when you come across a parse error similar to the one shown below, look at the immediate line of code before the line that the parse error gives you.

Parse error: parse error in path\to\file.php on line 4

The contents of the file.php are:

<?php
$variable = 'Some Information'

echo $variable;
?>

Notice that the parse error tells you that the error is on line four, but if you look at line four, you would see that there is actually no error there. It's good and correct like it should be. So look at line three... empty space. Line two... ohho! That line is missing that semi-colon at the end, which throws off the parser and makes the parser think that line two is part of line four, which it isn't.

When naming the variable, I would recommend that you name that variable in such a way that it defines it's function the best it could. Let's say that you are setting a city's name to a variable. You wouldn't have that variable named '$types_of_alligators' now would you? That would be silly. You would either have $city or $city_name. Remember, $city's_name would be incorrect because no quotes in variable names.

3.3 Setting Values to Variables

You may think that this simple little section is a simple redundancy to the complex big section right above, but it's not. This simple little section actually tells you a little important piece of information that you need to know in order to prevent your parser from throwing you fatal (You better watch out) or parse errors at you.

To set a value to a string, you can use two different types of quotes... single-quotes ( ' ' ) or double-quotes ( " " ). Below is an example of each in use.

<?php
$single_quotes = 'This is some single quoted string';

$double_quotes = 'This is some double quoted string";
?>

There are a few things you should notice here: one thing is that the same quotes is used to wrap the end of the value as it did starting it. So $single_quotes was started with single quotes and finished with single quotes. You can't just change in one variable unless you used some sort of concatenation (next section).

One other thing you should notice is, I don't have any of the same quotes that is used to wrap the string with. The following code would be incorrect and would throw a parse error.

<?php
$var1 = 'I can't do this';
$var2 = "I also can't do " this";
?>

In order for you to do that, you would need to escape that single-quote or that double-quote. You would also see by the highlighting, that the code is highlighted differently and incorrectly... that's if you know how it's supposed to be highlighted that is. To escape them, you would use the backslash "\" in front of the quote that you are trying to escape.

<?php
$var1 = 'I couldn\'t have done this until now';
$var2 = "I also couldn't have done \" this as well";
?>

You don't need to escape the single-quote if you are using double-quotes like I didn't on like 3.

The reason for all this is because the quotes tell the parser where the string begins and where it ends. If you have an unescaped quote in the string, the parser tries to parse the rest of the characters (after the unescaped quote) as PHP code and can't do that, because those characters aren't PHP code.

3.4 Concatenation

In PHP you could concatenate two or more different datatypes together to make one line piece of information. This is useful when you want to combine two variables together to allow your modifications or whatever you're doing to be done correctly.

There is more than one way to concatenate two variables together. One way is:

<?php
$var1 = 'Hello ';
$var2 = 'World!';

// Concatenate the two variables ($var1 and $var2)
$var3 = $var1 . $var2;
?>

$var3 would be holding the information, 'Hello World!' because $var1 (Hello ) was concatenated with $var2 (World!). Combining those two values would create 'Hello World!'.

You would notice that the first method of concatenation used a period ( . The Dot). In JavaScript it would be a plus sign (+), but in PHP you don't want to mess around with concatenation when you want to do simple addition, so it's a dot.

The other way involves double quotes:

<?php
$var1 = 'Hello ';
$var2 = 'World!';

// Concatenate the two variables ($var1 and $var2)
$var3 = "$var1 $var2";
?>

The PHP parser parses variables written within strings if the quotes used to wrap the strings are double-quotes ( " " ). If you simply use simple quotes, it wouldn't concatenate the two values together. The value of $var3 would simple be '$var 1 $var2'.

To change between quotes used to make a string you would do something like the following:

<?php
$var1 = 'world!';

// Bellow is the string concatenated with $var1 and another string.
$var2 = 'Hello ' . $var1 . " I'm using two different types of quotes here! So excited!!!";
?>

3.5 New Lines and Spaces

The PHP Parser doesn't give any meaning to the new lines and spaces in the PHP files that you code which allows you to format the code in the PHP file the way that is readable to you and makes more sense to you. I strongly recommend that you organize your code and format it in the way that would allow for easy future editing and easy human readability. Unless you are purposefully trying to obfuscate the code, keep it well organized.

3.6 Comments

Comments in your code would be useful to keep your logical flow of code understandable to other programmers, so you would remember what you are doing, what you need to do, what each line does, and things like that. When I code my systems and anything else that I code, I add a ton of comments there to make sure that I would be able to edit that code later on when I might have forgotten what I was doing there. Basically, comments allow the programmer edit the code later on much more easily than without comments.

There are three ways you could put comments in a PHP file. Two of which are single line comments only and the third way is to allow a huge block of comments to be put in the PHP file. Before I go on and show you how to comment, I want to point out that you can comment out the code and the parser wouldn't parse it. Alright, comments are:

<?php
// This is a single line comment
# This is a single line comment
/*
     This is a big (well... multi-line) comment
     block. This could come in useful (just as single line comments would
*/

// $variable = 'Some Information';
?>

All those lines of code (except for lines 1, 8, and 10) are an example of comments. Line 2 and line 3 are an example of a single line comment and lines 4-7 is an example of a multi-line block comment... You can't nest multi-line comments.

Line 9 is a single line comment and it's purpose there is to show that the code $variable = 'Some Information'; would not be parsed by the PHP parser. It is a comment and it would stay a comment. PHP parser skips over any comments and doesn't try to make sense of your comments (good thing too).

3.7 Reserved Words

As in many other programming languages, there are a few words that are reserved and that you shouldn't use as a variable name, function name, class name or any other name. They are generally referred to as 'Reserved Words'. They are: Listed here

4.0 Datatypes

Just as in every language you got books, art, music, and any other means of communications, you got datatypes in PHP. With these datatypes you can do different things with different datatypes. Allow me to list them and explain them to you:

  • Integer numbers
    A positive or negative whole number. That means no fractions and no decimals... just whole numbers. (-4, 4, 3453453453).
  • Floating point numbers
    A positive or negative number that is a decimal or a fraction. (2.4, 2.34567, .0009).
  • Strings
    A series of single characters. (Hello World, Some Information).
  • Booleans
    Basically true/false value. Anything that lists false or true*****
  • Arrays
    An ordered map with values associated to numeric or named keys.
  • Objects
    Objects are initiated instances of classes.
  • Resources
    Instances of resources initiated from databases or other APIs.
  • Null
    Basically a constant 'null'. Similar to the 'undefined' in JavaScript.

***** The following values would equal FALSE.

  • Boolean False
  • Integer zero (0)
  • Double zero (0.0)
  • Empty string and string "0"
  • Array with no elements
  • Object with no elements
  • Special value NULL
    Every other value is considered TRUE.

Any of these datatypes could be set to a variable and then retrieved through that variable.

One thing about the datatypes and variables, is that the PHP parser does simple type conversions between the datatypes. The PHP parser is capable of converting a string to an integer data type. Consider the following code example (Taken straight from PHP.net):

<?php
$foo = "0";  // $foo is string (ASCII 48)
$foo += 2;   // $foo is now an integer (2)
$foo = $foo + 1.3;  // $foo is now a float (3.3)
$foo = 5 + "10 Little Piggies"; // $foo is integer (15)
$foo = 5 + "10 Small Pigs";     // $foo is integer (15)
?>

That is called juggling... as in juggling between datatypes.

To learn more about juggling and how to juggle between other data types, please consult the PHP.net's documentation.

This page was published on It was last revised on

Contributing Authors

0

8 Comments

  • Votes
  • Oldest
  • Latest
Commented
Updated

Very useful Bogey! This helps a lot in understanding the PHP code. Now I can identify what I'm looking at, and can fix up some errors.

I have a question.

In your other PHP Mechanics post, you showed how to make the Footer.php, Header.php, content.php, etc.

Now my question is, how do I link all of those and make them appear in all my pages? I have to do that through the < head > < / head > (without spaces of coarse), correct? Or is their another way?

Been researching on w3schools for PHP knowledge, so far it's been helpful, but it's a thoroughly broken down step-by-step. Right now I'm looking to just add a basic front page until my main page is up. So I want to do something as such on my splash page (below is an idea)

These will be links on my splash page under my site log.

About Us | Contact Us | Forums

The footer, header, content is a really simplified way to keep all your tags, web description, fonts, etc in a condensed version, but I'm not understanding how to connect them to all my pages. Any advice or redirection is appreciated.

add a comment
0
Commented
Updated

I'll apologize for the double post, but I had a question. In your other PHP Mechanics post, you showed how to make the Footer.php, Header.php, content.php, etc.

Now my question is, how do I link all of those and make them appear in all my pages? I have to do that through the < head > < / head > (without spaces of coarse), correct? Or is their another way?

Been researching on w3schools for PHP knowledge, so far it's been helpful, but it's a thoroughly broken down step-by-step. Right now I'm looking to just add a basic front page until my main page is up. So I want to do something as such on my splash page (below is an idea)

These will be links on my splash page under my site log.

About Us | Contact Us | Forums

The footer, header, content is a really simplified way to keep all your tags, web description, fonts, etc in a condensed version, but I'm not understanding how to connect them to all my pages. Any advice or redirection is appreciated.

add a comment
0
BO
443 9
Commented
Updated

Maybe I didn't explain it correctly in that tutorial... I'm planning to redo that tutorial and have it more 'newly-introduced-to-php' friendly.

Anyway, what you would do is include the components to the content pages.

You would have a separate page for each content. You would include the header.php or any other page you are going to include where the code would reside on that content page.

include_once "path/to/included/file.php";

The extension of the included file could me HTML.

I don't know if this answered your question or not... I'm currently planning my second part of this tutorial...

add a comment
0
Commented
Updated

Got it on the spot. That one line of coding was what I was looking for. W3Schools told me to use something else, but it wasn't working right.

add a comment
0
Commented
Updated

Very Nice! Thanks for sharing!

add a comment
0
Commented
Updated

Thank you for all of the information, that might just help out when I am trying to figure out some PHP coding.

add a comment
0
Commented
Updated

Thank you for all innovative information. I am learning PHP and this contents are very helpful for me...

add a comment
0
Commented
Updated

Wow!) Found some tips,which i need) Ty you wery much) Php is cool)

add a comment
0