PHP has been called many things, but I think the best (and most accepted) is Pre-HypertextProcessor.
Now what that means is that before the HyperText Markup Language (HTML) is "parsed" or viewed, PHP will do what it needs to do for that page.
PHP, like any language, has constructs, and a thought process. But it's not hard to wrap your head around. It's actually pretty logical. You say: If I get this, then I want to do that. The php code would be:
if($this == TRUE)
{
echo "that";
}
Pretty simple huh? Now as you learn more you can see some great additions like switches, loops, classes, objects, and arrays.
But for the general part, here's a mini, half-assed tutorial done by yours truly, just for you:
PHP, like HTML, has opening and closing tags delimiting where the PHP processor will parse PHP, and where it will stop. These tags are <? ?>. Depending upon your installed version of PHP and what settings you're using, you may have to open with <?php. So now you know how to enclose PHP code. There are basic functions that PHP comes with that are used a lot. Echo and print being very popular.
Echo takes the text string that follows and puts it into the code, or document. When the PHP parser sees this, it echoes it to the page, and the browser sees this and prints it for the user to see. Print does the same thing. They aren't identical, but most people use echo to print code, and use print to print debugging code so that when the script goes live, they can cut out all the print statements.
Another huge thing to remember in PHP is that each line has to end. Every line contained between the <?php ?> tags must be terminated (there are exceptions). A statement (line) is terminated by the use of a semicolon ( ; ).
Bad example
echo "This is some text"
Good Example
echo "This is some text";
Those are the very very basics of PHP. You can get more in-depth by picking up a book and reading it. They also give you good examples to create.
Normally, everyones first tutorial is the "Hello World" tutorial. That seems to always be the first. But here's what it would look like if I were to do it:
So you want to learn PHP. Using the following example, we'll get PHP to print out a message stating "Hello World. My name is Brett" in the browser window.
First thing we do is open a new document using any text-editor you want. I prefer EditPlus. Next we put our opening and closing PHP tags in the document:
<?php
?>
Now we know that we are coding in PHP, we can tell the browser what to print. We'll assign a variable to print (to add another dimension to PHP). So, we're going to assign the variable "greeting" a value. To do that, we use the variable delimeter ($) followed by the name of the variable "greeting". Then, to set the value, we use an equals sign. After the equals sign we put a "string" of text enclosed in quotation marks.
There are two kinds of quotation marks in PHP. Single (') and double ("). When using single quotes, the PHP parser will not replace variables with their values; however, variables within double quotes will be replaced with their values.
So, our greeting variable "definition" (when we assign values to variables we define them) looks something like:
$greeting = "Hello World. My name is Brett";
P.S. Don't forget to terminate your line with a semi-colon. Great!! We've got our text that we want to show in a variable that PHP will read. But how do we write it so that the browser will read it?
To do this, we use the internal PHP function of echo. As stated before, Echo just puts what string it has out to the browser document for the browser to display. To echo something, we just put the word "echo" (all lowercase) in front of a string that we want to echo. No equals signs or anything. So to echo a string, we do something like:
echo "This is cool. A real live working PHP script!!";
That will display the text This is cool. A real live working PHP script!! in the browser window. But we don't want that, we want to say hello world. How do we echo a variable?
Same way we echoed the string. We place the word echo in front of our variable, and then terminate our line. So to echo our variable, our code should look like:
echo $greeting;
Piece of Cake!!
So, now we have two things being shown on the screen. I don't like the first one we had there, but I don't want to delete it either, it may come in handy later. That's where "Comments" come in handy.
Since we only want to get rid of one line, we'll use a one line comment.
There are two types of comments. One line (//) and Multi-line (/ /). The only difference is the amount of lines that the comment "removes" from the parser.
So we need to "remove" that one line. So we just add two (2) forward slashes to it (like in a url: [url]http://[/url]) before the word echo. So now our line should look like
// echo "This is cool. A real live working PHP script!!";
That's it!!
Save your document as "test.php" and upload it to a server that has PHP installed on it. If your own computer has PHP installed, you don't need to upload it. Then point your browser to where you put test.php. What should show up is a one line greeting saying: Hello World. My name is Brett.
Our complete code for test.php is as follows:
<?php
$greeting = "Hello World. My name is Brett";
// echo "This is cool. A real live working PHP script!!";
echo $greeting;
?>
Hope that helps.
~Brett