<?php
Hey, this tutorial is meant to give you information on setting up php, and writing your first scripts. Have fun. I plan to release more tutorials, with more advanced scripting, but it's the basics first, so make sure you read it B) I recommend HTML knowledge.
PHP: What is it and how do I set it up?
PHP stands for pre-hypertext processing language. This means that all the code is read before the web page is submitted. PHP is considered the most powerful web-language and is used to develop most modern sites. It can be used to create things as simple as a tagboard, and as advanced as a complete portalling system.
To set up PHP is simple. First you will need a host with php and mysql support, or you will need your own server, I would suggest downloading phptriad from download.com for this. It comes complete with MySQL, php, and folders needed.
I have my own server, now what do i do? (If your using a host discard this information)
Now you need to unzip it to a main directory on your computer, which is most likely your C:/ Drive. Now I want to go how you will have to save now, so explaining things later will be easier. Open up the server folder, and find the folder htdocs. This is where you will save all of your php documents, otherwise your php scripts will not work. Also, when testing your php scripts, you will have to insert this into your browser's url box: localhost/nameoffile.php
ALSO, make sure you turn your Apache server on, otherwise your scripts will not appear.
I've got some type of server or hosting that supports PHP, now what?
Now I think your ready for your first script/s. Open up notepad or another text editor, and insert the following code. The comments (/* blah */ Are not required.
<?php /*This is the starting php delimiter. It is required for all php code. Why doesn't what I'm typing effect the php? Because this is a comment :P */
echo 'This echos'; /*This is the echo command, it displays all text between the single quotes. Just as in your css, after each command must end with a semicolon; */
/*This is the ending php delimiter*/ ?>
Now, I think you know the output of this one, but before we get to that, let's see how we save our document. If your using a hosting server, simply save it as whatever.php, and make sure a drop down menu says either HTML document or PHP document. But if your using your own server, find the drop-down menu that says "text-file". Click it, and then select "All Files". Now save this as whatever.php.
Now to view it, in your browser, type the url location. ex:
or with your own server
If done correctly, your browser should had This echoes on it. If not, make sure your server is on, supports php, you've added your <?php delimiters, and you've saved it as a .php file.
Great, now you've done your first PHP script! Exciting? I doubt it, that is probably one of the most used command (echo) you'll ever use, but it's completely useless to use without other things!
But first, let's explain what was in the comment tags. <?php and ?> are your PHP
delimiters. Everything in these tell the browser that it is a php script, they are required. The echo command simply let's the browser know that text, or something else is being displayed. Also in the single quotes you can add HTML tags. Such as <b> if you want bold text. Also good practice for including HTML tags into your quotes is adding backslashes before your true quotes in your tags. Ex.
<?php
echo '<font color=\"#000000\">Black text</font>';
?>
Putting in those backslashes keeps the browser from getting confused.
Now let's look at the last part of our Introduction, variables. I think you will find variables to be the most important factor of PHP, they save time, help with scripts, and so on so on.
First, what is a variable?
A
variable is something that can be changed.
Let's say we had a website that managed a company, and the admin's name was Jake. Now Jake's name appeared all over the website as an admin in hundreds of pages, but the admin changed. It would be very tedious to change Jake's name to the new admin's, so it would be much simpler to include the admin name in a variable, and change it when needed. Let's see:
<?php
$name = "SyringeX"; /*All strings (Variables) must include the $ sign in front of them, and the name could be anything you want. The name in quotes is what the variables values are, you can change this at any time. Things like numbers, do not need those quotes around them. And as always, remember your semicolor */
echo $name; /*When displaying just the variable, you do not need your single quotes. */
echo 'This site is run by' . '$name'; /*When including a variable with text, you must add . 'variablehere'; -- Otherwise the browser will actually read $name instead of the variables name you specified, in my case SyringeX. */
?>
Now, save this, open it up, and see the magic of variables. If done correctly, where your $name variables were, there should be the name you specified within the variable. You can change this to whatever you want, and both text areas would display the new name. See how this can make life easier?
Now test with variables and so on by yourself, see what you can come up with just by knowing echo and variable commands.
Look forward to Part 2 of the php tutorials, where you will learn much more scripting knowledge.
?>
Created by Syringex