PHP Tutorial - Part 1

The Basics

Sunday, May 29, 2005 by Latin4567 | Discussion: Tutorials

Having reccently learned the PHP scripting language and implemented it in my website and a few client websites, I have discovered how easy it is to use PHP to create your own interactive websites which are self-managing. You can even give your site a control panel allowing you absolute control of the site's content without having to modify code each time you update it.

Thus, I have decided to create a series of tutorials so I can share my (limited) knowledge.


Part 1 - The Basics


What you will need:
A web browser. If you are reading this then you most likely have one already
Macromedia Dreamweaver *Not required however refrences to dreamweaver are made in this tutorial. Any html editor will do. You can even use notepad!
A basic knowledge of HTML

Introduction:

As stated on the PHP homepage (http://www.php.net), "PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML"

PHP stands for PHP Hypertext Preprocessor and is a server side language. This means that PHP scripts are run at the webserver before being sent to the client web browser. The great part about this is it is virtually impossible for someone to access the source code of your PHP webpages because the PHP is rprocessed and then removed from the page before the page is sent to the browser. This means that you dont have to worry about the security of your scripts as you will most likely be the only one ever to see the source code.

PHP is similar to the PEARL language and also shows some resemblence to Javascript. Please realize, however, that php is really only intended for databse managment and dynamic content. Basically php can store information and change the contents of remote files but it cannot achieve some of the stunning visual effects which Javascript is capable of. Php and javascript are doubly powerful when used together because they make up for each other's weaknesses.


Does your webhost support PHP?

Since PHP is a server-side language, an actual PHP program has to be installed and running on the webserver at all times. When the server recieves a request for a php file, the file is first sent to the php processor and then is sent to the client's browser.

Most good webhosts now have php installed on their webservers however it is highly advisable that you check before you re-code your website only to discover the unfriutfulness of your actions...

If your webhost provides you with a control panel of some sort it will probablly indicate wether PHP is installed. To test and see if your webserver is php-enabled, copy the following code and paste it into a new file named "phpinfo.php".

Code:

<?php
phpinfo();
?>


Now upload the "phpinfo.php" file to your webserver and open it in a web browser (NOTE: for users using FTP to upload their files: if you want to execute a php file you uploaded, you have to do it via 'http://' and not 'ftp://' or the file will not run properly). A page displaying information about the webserver and php should now show up. If it does not then php is not properly enabled and you should probably contact your webhost to request that php be installed/re-installed.

Declaring in PHP

at this point, you are probablly wondering what the code from the previous step means. Don't worry, your not alone...
You can only write php within a php tag inside a php file (*.php, *.php3, *.php4...).

So you must declare that you are using a php script. The below code shows the most popular way of declaring a php script:

Code:

<?php
//PHP Code In Here
?>

PHP can go anywhere between the "<?php" and "?>" tags.

You probably noticed that I placed "//" before writing "PHP Code In Here". Those double slashes create a comment in php. Comments are pieces of text that arnt processed as code. Comments can be used to remind you what a line of code does... You can also have multi-line comments by typing "/*" and "*/. It is important to remember to always close this type of comment or everything that comes after it will be treated like a comment and will be ignored.

If you prefer to use the classic declaration method for scripts in html, you may use the following however the previous method is more widely used:

Code:

<script language="php">
//PHP Code In Here
</script>


Outputing Text

Outputing text is the most basic ability of PHP. Php has a number of commands for outputing text. You are free to choose any one of them however this tutorial will only use the 'echo()' and 'print()' commands. When either of these commands is used in php, the text contained within the parenthesis will output into the webpage.

The following code will produce a blank page with the words "Hello world!" when executed in a web browser

Code:

<?php
echo("Hello world!");
?>


Note that a semi-colon (";") was placed after the echo command. The semi-colon lets php know that the current command is done and to move on to the next command. Semi-colons must ALWAYS be placed after a command or the page will produce an error

The quotation marks tell php to treat the text contained between them as a string (text).

The important thing to remember is that if you want to output html code, you will usually have to use single quotes (') instead of double quotes because most html contains its own quotation marks which will disrupt the php script.

So lets say you want to output an html image tag. You would use the following script to do so:

Code:

<?php
echo('<img src="http://www.durosoft.com/logo1.jpg" border="0">');
?>


This would result in the following html code once the page has been processed:

Code:

<img src="http://www.durosoft.com/logo1.jpg" border="0">


which would look like this when opened up in a browser:




Variables

Variables are things which a script uses to store values and data. Variables in php can come in several forms including Integers (numbers), Strings (text), Booleans (true/false values) and many others (although we will only discuss integers, strings and booleans in this tutorial). In php, you must always put a $ sign before a variable whenever you declare or refer to it. The following script will declare 3 variables and assign each of them values:

Code:

<?php
$sometext = "to be or not to be?";   
$anumber = 4;
?>


You can also output variables just as you would output text except without quotation marks. The following code will output the values of the variables from the previous script and seperate them with html linebreak (<br>) tags.

Code:

<?php
$sometext = "to be or not to be?";
$anumber = 4;

echo('sometext = ');
echo($sometext);         //outputs the value of $sometext
echo('<br>');           //inserts a linebreak
echo('anumber = ');
echo($anumber);     //outputs the value of $anumber
?>


This code would produce the following html code:

Code:

sometext = to be or not to be <br> anumber = 4


which would look like this in the browser:

sometext = to be or not to be
anumber = 4


Operators

Operators are methods which can be used to change the value of a variable or modify it in some way. Here are some standard operators:

= (equals) Changes the value of any type of variable ($value = 'hello'; )
+ (plus) Finds the sum of two integers ($four = 2 + 2;)
- (minus) Finds the difference between two integers ($five = 10 - 5; )
* (times\multiplied by) Finds the product of two integers ($six = 3 * 2; )
/ (divided by) Finds the quotient of two integers ($six = 12 / 2; )
. A period will combine two strings, appending (adding) the second to the first

The following code will give the variable $answer a value of 2, then add 3 to its value resulting in 5, and finally output the result with some text preceeding it (using a period):

Code:

<?php
$answer = 2;
$answer = $answer + 3;     

echo('2 + 3 = ' . $answer);

?>


this code would output:

2 + 3 = 5


This concludes Part 1 of the PHP tutorial
Please keep your eyes pealed for additions to this set of tutorials
mrbiotech
Reply #1 Monday, May 30, 2005 6:44 PM
Thanks for doing this! You've got a natural progression of points here that doesn't leave me lost or wondering "where did that come from?" Very logical outline... thanks for sharing!
Latin4567
Reply #2 Monday, May 30, 2005 9:49 PM
Glad you found it useful... part 2 will be coming tomorrow

Eventually there will probablly be over 10 sections to this tutorial
CerebroJD
Reply #3 Thursday, June 2, 2005 11:56 PM
Wow! I have to be honest Latin, this is the FIRST bit of information from you that I've found useful! Incredible, and thanks a ton! Great timing, since I'm just learning the language. I look forward to more parts!
Latin4567
Reply #4 Tuesday, June 7, 2005 8:08 PM
thanks cerebro
FiascoAtLarge
Reply #5 Friday, June 24, 2005 10:50 AM
Thank You!!!! I look forward to coming posts.

Posted via WinCustomize Browser/Stardock Central
Latin4567
Reply #6 Saturday, July 16, 2005 6:10 PM
Sorry I havent written any new ones guys.. i've been very busy over the past few weeks doing some software for my school's tech department

i will try to write the rest of the tutorials in late August
Lotherius
Reply #7 Wednesday, July 27, 2005 7:32 AM
One tip - don't start your PHP learning experience by installing PHPNuke unless you really want to spend a lot of time patching unreliable code
joeKnowledge
Reply #8 Saturday, July 30, 2005 5:00 PM
Thanks allot for this.

The only thing I need now is an ability to get RSS feeds to parse in php. I only know how to do it in asp. I looked around for tutorials, but they seem a litle confusing...

Latin4567
Reply #9 Wednesday, October 26, 2005 7:17 PM
If you want to parse RSS feeds, your going to have to figure out how to read the xml and do it yourself with string functions... It shouldn't be to hard..
greek1234
Reply #10 Sunday, February 4, 2007 5:41 AM
So got any more tutorials on php?
PurrBall
Reply #11 Sunday, February 4, 2007 6:33 AM
joeKnowledge I've made an RSS feed in PHP.

Please login to comment and/or vote for this skin.

Welcome Guest! Please take the time to register with us.
There are many great features available to you once you register, including:

  • Richer content, access to many features that are disabled for guests like commenting on the forums and downloading skins.
  • Access to a great community, with a massive database of many, many areas of interest.
  • Access to contests & subscription offers like exclusive emails.
  • It's simple, and FREE!



web-wc01