Learn PHP - Lesson 1 Tutorial - How to get started with PHP - Part 4, First PHP page
Learn PHP - Lesson 1 Tutorial - How to get started with PHP - Part 4 of 4, creating your first PHP page.
There are basics one must know when getting started with PHP. In this lesson, we will cover the following:
1) At a minimum you need a high level understanding of what makes up that which we call the internet
2) Understand the basics of what constitutes a web server (at least as far as we're concerned to get your coding)
3) The two easiest ways for you to start creating php web applications. 4) Learn how to create a php page.
Before we dive into coding a page up, let's go over a few things:
Next, You have now either setup your own server, in which you'll store your files (html, php, css, images, etc.) in the apache 'httpdocs' and access the root directory in a browser by typing http://localhost into the browser address bar...
OR, you've rented a web server in which you'll FTP into your rented server's root directory and store aforementioned files (html, php, css, images, etc.) there.
For the purpose of learning PHP it does not matter to me which route you went... from here on out I will refer to the storage of files in the root directory of your specific setup.
Next, the difference between absolute and relative paths.
This is important not only in anchor tags (links), but also in navigating within the file structure dynamically in many php function calls so you better understand it.
Getting right to it -
What is the absolute path?
An Absolute path is the full representation of a location. On the web
What is a relative path?
The idea of a relative path is easy to grasp, yet a tad harder to explain.
Let's assume you are in your web site's root directory, and your website is www.phphaven.com. if you saved a file in that directory called 'bob.txt', you could access it by it's absolute path of www.phphaven.com/bob.txt
Now, let's say you have an html file in your root directory called index.html and within that file you want to display an image. Instead of putting all the images in the root directory, you create an image directory to store your images. Here is what your root directory now looks like:
Inside of index.html, you've added some code to display the image like so:
Relative path example - You could also code the image tage as follows to access the testImage.gif:
./ represents the current directory of the file currently in request. In our example the request is for index.html file in the root directory
Now then, let's say the index.html file is in another directory off root and testImage.gif stays where it's at. The file structure would look like so:
There is no 'img' directory within the 'about' directory. Thus src="./img/testImage.gif" will no longer suffice, however the following code will:
../ represents the directory one level prior to the current directory of the file currently in request. In our example that request is now for index.html file in the about directory one level deep of root.
What's even better, we can use this tactic over and over again if necessary. Observe, our index.html file is now 4 levels deep, yet our image has stayed in the same directory:
To reference it, we change our code to the following:
Next - You need to know where to save the files to have them be viewed on your web server. If you setup your own server, go to the httpdocs folder in your apache install (you should have gotten to this point in the installation, if not, go back a step in this lesson to learn more). If you rent shared web hosting, you'll need to know where to store your files and that usually occurs via FTP.
Let's get to the code finally!!!
With HTML you have an opening <html> tag and a closing </html> tag
Similar concept with PHP, only you can open and close a PHP section multiple times, where you can only open and close an HTML tag one time. That may be a tad confusing, and don't worry about it now, we'll dive more into that later.
Let's start our first PHP page... Hello world:
1) First create a place on your local hard drive to work on your files. Maybe c:\websites\mysite\ , and if you're on linux, same thing, find a place. Basically somewhere to store a working copy of the code that you'll move to your web server root directory later
2) Open Notepad ++
3) Use the "Save as..." command in the file menu, when the dialog box comes up, in the bottom make sure the "Save as type:" is selected as PHP. Save the files as index.php in the directory you created
4) We're going to show you the code and explain it afterwards... type the following code in and save to your root directory (Figure 1):
echo'Hello mother frakin world!';
5)Open this file in a browser
EXPLANATION of the code:
OPENING AND CLOSING PHP TAGS
In figure 1, with the two blue balls XD I have highlighted the opening and closing tags for PHP.
This is the normal opening and closing tags for php.
PHP short form tags - You can also use just <? and ?> to open and close a section of PHP code. WARNING: Using short form tags may confuse certain server configurations into thinking the file is XML or other. I suggest always using the normal opening and closing tags for PHP
GLOBAL PHP FUNCTION - echo()
In between the opening and closing tags is the use of PHP's built-in language construct - echo.
echo tells the browser to print a string as output. In our case we set the output to "Hello mother frakin world!"
The output in a browser
That concludes this lesson, look for lesson 2 soon!