November 7th, 2003, 09:54 AM
Well it's me again with another outragously boring tutorial. This time I am talking about PHP.
Before I hear a million and one people jump down my throat about there are already exsisting PHP tutorials.
You can do one of a couple things. One go check neg's Sticky all the tutorials there are about specific things nothing to help you get started. Second you can neg me and keep going. Or better yet you can stop reading now.
Now that we got that out the way.
Time for us to begin. First thing first if you pick up a PHP book RTFM. I had a book and I didn't read everything and missed something so I had to go back and re-read the entire thing. That and you will probably miss something that you might need.
Ok if you don't know HTML at this point you should probably stop and go to www.davesite.com and learn it.
You can't really do much with PHP if you don't know any HTML. Kind of like trying to drive a car without a wheels. Yeah you might look cool sitting behind that wheel but you know your not going Anywhere.
Next hears a little bit of background information on PHP it was origionally created by Rasmus Lerdorf in 1994 and it stood for "Personal Home Page" Now when it grew in popularity it became known to be "Hypertext PreProcessor"
According to PHP.net the official PHP website.PHP is a server-side, cross-platfrom, HTML embedded scripting language.
Ok now let me explain to you how PHP works.
PHP is a server-side language, which means that the code resides on a host computer.
The server would read the PHP code and process it according to its scripting directions. The PHP code tells the server what to do with the code. Then the server sends that information to your Web Browser. So the pattern is like this.
This differs from plain HTML because on plain HTML the server and the Client talk back and forth.
Now that you kind of understand what is going on time to learn something.
Here I will give you the always lovable "Hello World" code.
<Title> I can see, I can see
print (" Help I'm blind");
Now you can also you (echo) instead of print, they are both interchangable.
Now that code will print on your screan. "Help I'm Blind. Just like if you had of not put the php code there and just wrote "Help I'm Blind" in between <body></body>.
The (;)semicolon has go after all statements.
Now so you know, your browser will not show this code if it is PHP, you need a server to do it. Now considering that you used just the HTML then it would print the "Help I'm Blind".
The () are not mandatory when coding it just makes your code more readable. The print("Help I'm Blind"); would also work as
print "Help I'm Blind"; I recommend that you use them it makes your code easier to read.
print does what it says print what ever is between the ("") Qoatation marks
echo does the exact thing.
Now when ever you start any php code you have to start it with <?php and end it with ?>
Now this does change, There are some servers that only require you to start it with <?.
But you will always end with ?>.
Now that you are done with that, you have created your first PHP code. Though very simple you have still created it.
Everyone jump up and down and dance. *Play Party Music*
Now you will need to send it to a server that has PHP enabled for it to work.
Now the next thing we are going to do is get you some info about the server your on.
Here is the code you will use.
Now we have to take that code apart you already know what everything but the phpinfo(); does.
phpinfo() is a function that basically prints a table showing the specifics of the PHP installation on that paticular server
Now like all languages you can add comments to your code. Because we all know that when we look back on our code and see that part talking about dogs, Trees, and Other things. We wonder WTF were we thinking.
In PHP there are 2 ways to do it.
One is (//) which starts the comment so let's say I had something like.
print("Hello Guys I'm an Idiot")// I some times need to put that last drink down.
I will know just what was going on at the time that I wrote the code.
You can also do
//I am about to go to the store I will be back and finish this project
All by itself in your code. PHP will ignore everything on that line.
The other way is to add the (#) it does the same thing as the //
So you could have #Hi Kids do you like Violence.
and once again all that would be ignored.
Ok. Now that we got that stuff out the way time to go on to something else. Next we have Variables.
So you are tired of those damn boring old, not doing anything HTML pages. Well this is where you start to change that
Variables allow you to temporarily store and manipulate data. So before you on. Your going to ned the Variable syntax.
Variables in PHP are represented by a dollar ($) sign followed by the name of the variable. The variable name is case-sensitive.
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus:
So by knowing that we know that "Tom" is not a Variable. Why not, because Tom doesn't start with a ($) sign. We also know that $Tom and $tom are 2 different Variables.
There are several different types of Variables the ones I am going to talk about are Numbers, and strings.
Now there are actually 2 different tpe of numbers. Integers and Floating point numbers.
A floating point is a decimal or fraction doesn't matter if it is postive or negative.
Now if Floating Point numbers are fractions and decimals, then that should leave you to believe that intergers are not. Therefore an integer should be a number that is not a Fraction or decimal.
Next on the list of dirfferent types of Variables we come upon. (String's)
A variable is a string if it contains characters. Enclosed in either a pair of Single qoutaion mark (') or double
Quotation marks (")
Example of Valid Strings
"what my name"
Examples of Invalid Strings
Hello World // Invalid because it doesn't have any quotation marks.
"I said," say my name"" //wrong because the quotation marks are wrong. There should be an in the middle without a \ before it.
So now you are wondering how do you put quotation marks in, your code so they show up.
Just place a "\" backslash before the quotation marks.
So to get that string to work you would put.
"I said \" say my name"
There are several characters that you should try to avoid.. Those being ($,',").
When coding strings there is an advantage for Double quotation mark over single. The advantage is that your strings will not print Variables values correctly when using Single quotation marks
Let me explain let's say you assign
$name = Tina
If you do (print"Hello, $name")
it will print Hello, Tina
But if you put (print 'Hello, $name') you will get. Hello, $name
Arrays in my opinion are beyond the scope of this tutorial. So I won't even speak about them.
Assining Values to Variables
At this point you should be curious how to assign a value to a variable. To assign a value to a variable.
You muust create the variable by intializing it. To initialize a Variable just type "$VariableName" then that is followed by the equals sign (=) what the Variable is equal to, followed by a semicolon (;)
$new = 45;
if you were to do, (print "$new") you would get 45.
Now the last thing I am going to talk about is predefined Variables
Predefined variables are a type of variable that the webserv applicatiooon, PHP module, or the Webserver Operationg system have already defined. Now it is good to know these because you don't want to make any that will be the same as the predefined ones on the server.
Now to find out what they are take out that little program we wrote earlier that has the phpinfo() function in it.
Now just load that to the server and go look on the server and you will see all the predefined Variables.
I hope that is enough to help you get started. If anyone has any questions post them or Pm me. I am always awaiting questions and comments.
Now I am going to have to say Thanks to Chsh he helped me out on this. Well besides that. I am out. Someone hand me a beer I am going to sleep I haven't been to sleep in 2 days. I think it is time for me to get some z's.
Here are all the other PHP tutorials on AO. I thought it would be a good Idea to link them at the bottom. There all excellent tutorials, and I recommend that you read them all.
I would really like to hear your questions and/or comments.
November 7th, 2003, 10:05 AM
good work whiz...
Nice you state the links to other members efforts ....
I think that should become a standard for tuts, links to other similar or additional tuts
November 13th, 2003, 12:42 AM
thanx for that...i have atleast 3 books, can't understand a damn one of 'em....
(I am a n00bie) anywell, keep postin' you've got the gift of clarity
Suicide bombers?..............there are too many of them