Getting started with PHP
Basics of PHP – Part 1
Forward: This is the first part of my series, PHP Basics. PHP is a computer language used in the Internet.
By: Chrysanthus Date Published: 28 Jul 2012
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at email@example.com. That is, contact me for the slightest problem you have about what you are reading.
In order to study PHP, you should have basic knowledge in XHTML (or HTML). If you have not studied this languages, then you should study it from the series I wrote in this blog (or another blog in my name). To arrive at the XHTML series that I wrote, type, “XHTML Basics” and my name, “Chrys” in the Search box of this page and click Search. If you have a Google Search box on this page, use it. Note: today, XHTML is like the latest version of HTML. XHTML is like strict HTML 4.
Also, in order to study PHP and master it, your level of mathematics should be at least that of Middle School. If you did not pass in middle school mathematics, do not worry. There is a way out. You should study the mathematics course easily at the link below. This site offers online interactive middle school math course in a step-by-step fashion, in as short as three months or as long as 10 months. There you have the right to ask questions from the authors of the web site. You can be studying the math course while studying PHP.
Here, I give you the requirements to study PHP in this series.
- PHP 5.2.8 binary
- Apache HTTP Server 2.2
- Text Editor
- Your Personal Computer and its operating system.
You can download PHP 5.2.8 binary and Apache HTTP Server 2.2 free from the Internet. Just search and download. They work with many operating systems.
In this series there are many code samples that you will be trying.
They are both easy to install. Just follow the instructions. When you install the server, make it to work with PHP (server module), if necessary. When you install PHP, make it to work with the server. For the server, you put your HTML and PHP files in the htdocs directory. In my computer, the path is, C:\Program Files\Apache Software Foundation\Apache2.2\htdocs ; I use the windows operating system. You can use a different server, if you know how it works.
A PHP script goes into the tag,
<?php - - script - - ?>
So, if you have a stand-alone script (program) then the beginning of the file would be <?php and then end of the file would be ?>.
If your script is embedded in an HTML document, then the beginning of the script would be <?php and the end of the script would be ?>. In the case of the HTML document, you effectively have a single HTML tag element, which is <?php - - script - - ?>. You can have many PHP scripts in an HTML document; that is, many <?php - - script - - ?> tags. This tag is better written as
- - script - -
The web page with the PHP script (or scripts) must be in the server. The script is executed by what is called the PHP parser, at the server. The PHP script in the HTML document at the server, is not seen when the browser is displaying the HTML document. However, after execution, the PHP script can produce HTML elements or text at the server, to fit in the position of the PHP script, replacing the PHP tag. When the HTML document is downloaded by the browser, the HTML elements or text produced at the server would be seen at the browser, in the position where the PHP script (PHP tag) was.
When one or more PHP scripts are embedded in an HTML document at the server, the file name extension of that HTML document has to become .php instead of .htm or .html.
Copy and paste the following code in your text editor:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
echo '<p>Hello World</p>';
Save the file as hello.php in your server’s htdocs directory or root (home) directory. Open your browser. Type the URL, http://localhost/hello.php in your browser and click Go. Your browser should display “Hello World” as the only content of a web page. If it were in the Internet (web), you would replace “localhost” with the site address and path to the PHP file, hello.php.
If you tried this example and you did not see “Hello World” at the browser, or it prompted for download, or you see the whole file as text, chances are that the server you are on does not have PHP enabled, or is not configured properly or you have not install PHP connecting it to the server properly.
The main code is an XHTML code with a single PHP tag. We look at what is in the PHP tag. There is just one line in the tag, which is:
echo '<p>Hello World</p>';
The word echo means output or type what is in quotes. The quotes may be single or double. What we have in the quotes is an HTML Paragraph element with content, “Hello World”. So, the word “echo” replaces the PHP single tag with the HTML Paragraph element, in the HTML document. That is why you see the Paragraph content at the browser. It is not all PHP scripts that output HTML elements or text to replace the PHP tag. Some scripts in the HTML document just do some execution without outputting anything.
You should now know basically, what PHP does. We stop here and continue in the next part of the series.
Related LinksMajor in Website Design
Web Development Course