PHP is a scripting language, which is used at the server of the web. PHP can be used in a stand-alone program or it can be embedded (typed) into an HTML document. PHP stands for Hypertext Preprocessor. The 'P' in front of HP was added. PHP is an interpreter language. This is the first part of my series, Basics of PHP.
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
Prerequisite
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, http://www.cool-mathematics.biz . 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.
Requirements
Here, I give you the requirements to study PHP in this series.
- PHP 5.2.8 binary
- Apache HTTP Server 2.2
- Browser
- 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.
Installation
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.
PHP Script
A PHP script goes into the tag,
So, if you have a stand-alone script (program) then the beginning of the file would be .
If your script is embedded in an HTML document, then the beginning of the script would be . In the case of the HTML document, you effectively have a single HTML tag element, which is . You can have many PHP scripts in an HTML document; that is, many tags. This tag is better written as
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.
Your first PHP Script
Copy and paste the following code in your text editor:
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
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.
Explanation of Code
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 '
Hello World';
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.
Chrys
Published by Chrys Forcha
I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a... View profile
- PHP Comparison and Arithmetic OperatorsIn this part of the series, we talk about some common PHP operators.
- PHP Loop StatementsIn PHP you have the do-while loop, the while loop and the for loop. We shall see what all these mean in this article.
- Some PHP Predefined Functions and ArraysIn this part of the series, we look at some predefined functions and arrays.
- PHP and Web Hosting with MacMany Mac users might not know that your OSX operating system comes equipped with the software required to host your own web pages. Learn how to start up your server, enable it with the easy and fun-to-use language PHP...
- Basics of PHPA brief introduction to PHP, including how to setup a Wamp server and writing your first "Hello World" PHP script.
- PHP Basic Syntax
- PHP Regular Expressions
- Basics of PHP Variables
- PHP Function Basics
- PHP Object Basics
- Boolean Logic and PHP Conditions
- PHP Array
