Learn PHP - Lesson 1 Tutorial - How to get started with PHP - Part 4, First PHP page
Bookmark and Share

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:

First, I  assume you know HTML.  If you don't, learn it here before going any further. You need to know it before learning PHP.


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:

Learn PHP - Lesson 1 Tutorial - How to get started with PHP - Part 4, First PHP page

Inside of index.html, you've added some code to display the image like so:
 
<html><head><title>
Relative Path Example</title></head>
<body>

<img src="http://www.phphaven.com/img/testImage.gif" border="0" />

</body>
</html>

In the src attribute, this is the absolute path.  

Relative path example - You could also code the image tage as follows to access the testImage.gif:

<img src=
"./img/testImage.gif" border="0" 
/>

./   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:

Learn PHP - Lesson 1 Tutorial - How to get started with PHP - Part 4, First PHP page


There is no 'img' directory within the 'about' directory. Thus src="./img/testImage.gif" will no longer suffice, however the following code will:

<img src="../img/testImage.gif" border="0" />

../   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:

Learn PHP - Lesson 1 Tutorial - How to get started with PHP - Part 4, First PHP page

To reference it, we change our code to the following:

<img src="../../../../img/testImage.gif" border="0" />


This is the power and curse of relative paths.  There are other implications as they relate to SEO and beyond, but for now, we just need to understand the differences.

Now I believe we all understand those differences, we can move on.

Next, we need to understand 'What is the difference between PHP vs. ASP.NET/JAVA(JSP) or What is the difference between a compiled language (.NET) and a scripting language(PHP)?

Let's make this easy.  At the highest level, here is the fundamental difference:

PHP is compiled at runtime (when a request is made) and output is sent to the browser

.NET on the other hand is compiled during a deployment phase, it is then converted into machine code and cached to be served up upon request

Also, compiled languages require all pieces to be ready and put together (compiled) into an application.  
Scripting languages can be applications, or they can be fragmented pieces of code.

Use the google machine for more information between the two... but hopefully now you have an understanding of the high level difference.



Let's code your first php page - hello world 

First you need to have the right tool - A great tool to use for a PHP interpreter is notepad ++  .  It's free, it's great color scheming, download it here.  

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):

<?php
 
echo 'Hello mother frakin world!';
 
?>


Learn PHP - Lesson 1 Tutorial - How to get started with PHP - Part 4 of 4, creating your first PHP page
Figure 1

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

Learn PHP - Lesson 1 Tutorial - How to get started with PHP - Part 4 of 4, creating your first PHP page



That concludes this lesson, look for lesson 2 soon!

 

 
 
Recent Comments