This is a tutorial of the very basics and how to get PHP running on your system. If you're a master at PHP that's perfect, but this won't be for you. Assumes no programming knowledge, and a Windows PC. 1. Installation First, you need a few things. Don't worry, you can get them all for free. What I recommend downloading: The WAMP server. This will download PHP, MySQL, and acts as a webhost on your local machine. It also has a nice tool called PHPMyAdmin that allows for easy database manipulation so you don't have to mess around in the MySQL command console as much. A programming-oriented text editor. Microsoft Word and similar text editors aren't going to work. They add too much extra formatting that will mess up any code that you add. I recommend Komodo Edit. You can get it for free online (http://www.activestate.com/komodo-edit). The reason I recommend this type of text editor is that it has PHP syntax recognition and even autofill. You can definitely use notepad, but it provides no assistance and honestly I hate notepad. Once WAMP is installed, you should see it in the bottom right corner of your screen. Click the WAMP Icon, and click Start All Services as well as Put Online. This will start your web server on your local machine. In order to see the code you write, you need to save it in the correct folder established by WAMP. By default, this folder is located at: C:\WAMP\www You can either save your php files in this folder, or create subfolders. To see these files in your browser, you type localhost/Filename.php in the address bar. If you have a subfolder in the www folder, you'll need to add that as well. For example, localhost/MyFirstSite/Filename.php.
2. Getting Started With PHP First, let's set up a basic HTML file and save it in our WAMP directory. We'll go back to it every time we start a fresh document. <html> <head> <title>Template</title> </head> <body> <?php ?> </body> </html> PHP: Now, let's write your very first PHP script to display output on a website. As the tradition goes, we'll have PHP produce the worlds "Hello World!" using the echo function inside the PHP tags: <html> <head> <title>Template</title> </head> <body> <?php echo "Hello World!"; ?> </body> </html> PHP: This will produce: Hello World! in your web browser. Remember that most lines in PHP end in a semi-colon.
3. Strings What is a string? Most simply put, a string is a series of characters. They are often what you use to output text to the browser using one of two PHP functions, the echo function or the print function. Most PHP programmers including myself prefer to use echo. (Not sure why, someone once told me it's faster, so I went with it ). If you followed the previous lesson, you've actually already had experience with strings when we used the echo function in the Hello World example above. <?php echo "Display your text here"; ?> Code (markup): This code will show Display your text here in the browser, without quotes. When echoing words and letters, it's necessary to wrap the text in quotation marks. You aren't limited to double quotes, however, single quotes will work as well. <?php echo 'Display your text here'; ?> Code (markup): This code will produce the same thing as the above one. Strings can also contain numerical values, and in most cases, do not require quotes. <?php echo 1+2; ?> Code (markup): As you'd expect, this will produce 3 in the browser; however: <?php echo "1+2"; ?> Code (markup): This will literally produce 1+2 in the browser; it won't perform the addition. So whether or not to use quotes in a string with numeric values depends on if you want the numbers to be taken as numbers or as characters in a string. PHP can perform more advanced arithmetic as well, for example: <?php echo 2*3-4*(453-32); ?> Code (markup): Apparently the answer is 1678. (Would have been my first guess ). Again, the print function can be used in place of the echo function in the above examples, if for some reason you have something against echoes. E.g. <?php print 1+2; ?> Code (markup): In the next lesson, we'll take strings to the next level by introducing variables and string functions.