Broad Network


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

Introduction

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

http://www.cool-mathematics.biz

Requirements
Here, I give you the requirements to study PHP in this series.
- PHP 5.3 Installer
- Apache HTTP Server 2.2 Installer
- Browser
- Text Editor
- Your Personal Computer and its operating system.

You can download PHP 5.3.23 Installer and Apache HTTP Server 2.2.10 Installer 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
I give you here, my experience with Apache Web Server 2.2.10 and PHP 5.3.23. From my expresience you can do your own installation. This is to do with localhost (personal web server). I installed Apache following the default settings typing "localhost" in the first two fields without the quotes and an email address in the third and last field, in the window that opened for input. Everything went well for the web server.

I then installed PHP choosing all the features. When the PHP installer asked me for the Apache configuration file directory path, I gave "C:\Program Files\Apache Software Foundation\Apache2.2\conf", which is correct, for my system. At the end of the installation, the PHP installer gave a warning message suggesting that I might have to configure the Apache web server manually. So the two setups did not work together.

After looking through the manual I deduced the solution: In the Apache configuration file (above path), at the bottom, the following was typed by the PHP installer:

#BEGIN PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
ScriptAlias /php/ "C:\Program Files\PHP\"
Action application/x-httpd-php "C:\Program Files\PHP\php-cgi.exe"
#END PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL

That is wrong. So I replaced that segment with:

#
LoadModule php5_module "C:/Program Files/PHP/php5apache2_2.dll"
AddHandler application/x-httpd-php .php

# configure the path to php.ini
PHPIniDir "C:/Program Files/PHP"

And the Apache web server and PHP interpreter worked together.

PHP Script
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

    <?php
    - - 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.

Your first PHP Script
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"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>
<?php
    echo '<p>Hello World</p>';
?>
</body>
</html>

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 '<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.

Chrys

Related Links

Basics of PHP
PHP Directory Function Basics
Understanding PHP Reference
PHP Function Arguments
Understanding Variable Scope in PHP
Object Oriented Programming in PHP
PHP Data Types Simplified
Exception Handling in PHP
PHP Regular Expressions
Sending Email with PHP
PHP Strings
Date and Time in PHP
PHP String Functions for Website Design
Advanced Course
PHP Variable Scope
Advanced Features of PHP Function
Array in PHP
PHP Two Dimensional Arrays
Understanding Object Oriented Programming in PHP
Advanced PHP Regular Expressions
Some features of PHP Entities
PHP Namespace
PHP Web Application

More Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course

NEXT

Comments

Become the Writer's Fan
Send the Writer a Message