Home

Introduction to PHP

The PHP: Hypertext Processor

Introduction

PHP is a scripting language used to create interactive webpages. An interactive webpage allows a visitor to submit values and get instantaneous feedback. This is usually the case for databases, email, animation, etc.

Fundamentals of PHP Code

PHP is used to perform actions on a webpage. Code necessary to do this is created in a computer file. The page is stored in a computer or application named web server. Examples of web servers are Apache or Microsoft IIS. This means that the PHP file must be stored in a computer that has the appropriate web server. On the browser, a visitor types the address of the webpage and the server delivers the webpage.

Testing the Lessons

Working With PHP on a Web Server

To follow these lessons, you can work locally on one computer or remotely directly on your website. To work locally, you must install Microsoft IIS or the Apache server on your computer. The installation depends on your operating system. You can check the website of your operatiing system or of Apache to find out how to install it. For example, if you are using a Ubuntu (Linux), you would open a terminal and type sudo apt-get install apache2. When you press Enter, you will be asked to provide your password and the installation should proceed.

When the installation is over, to check it, you can open your browser, change the address to http://127.0.1.1 and press Enter. You should receive a type of standard webpage that indicates that the installation was successful. Here is an example on Ubuntu:

This would produce:

The first webpage after Apache installation

The first webpage after Apache installation

Working Remotely

Working remotely consists of creating webpages directly on the webserver of the website. One way to do this is to create your own website. If you contact a hosting company to create a website, if you choose a Linux-based hosting, the company will likely automatically take care of the Apache installation and configuration. If that's the case, you can just access the control panel of your website, followed by the File Manager. Here is an example:

The first webpage after Apache installation

Locate the folder where you must create your files, usually named public_html:

The first webpage after Apache installation

(It may be a good idea to first a sub-folder in which to create your exercise files.)

Create a file (you can click the New File button), save it, and access it by its address on a browser.

A PHP File

To get an interactive webpage using PHP, you can start a document in which you write code. The code is just simple text-based. The document can have regular HTML code and include sections of PHP code. The scripting PHP code can be created in a section that starts with a tag as follows:

<?php

After <?php, include what is nececessary to display on your webpage.

To get the webpage, you must save the file. This may depend on how you proceeded. If you are working locally, create a file or start a document, write you code, and save it. Then either access the file from a browser or upload the file and use its address on a browser. If you are working directly from your website, in the File Manager, click the button to create a file, open the file, write code in it, save the file, and access it from a browser.

A PHP file has the extention .php:

The first webpage after Apache installation

As mentioned previously, after creating the file, open it. If you are working from your control panel, you can click the file and click the Edit (or Open File) button.

Introduction to PHP Code

A Message in PHP

A message is just a sentence you want to display on your webpage. To support it, PHP provides an element named echo that is followed by a sentence. The sentence can be provided in double-quotes. Here is an example:

<?php echo "Welcome to the wonderful world of PHP!"

The section that includes echo combined to what follows it is called an expression. You should end such an expression with a semicolon. Here is an example:

<?php echo "Welcome to the wonderful world of PHP!";

This would produce:

The first webpage after Apache installation

Instead of double-quotes, you can include the value of echo in single-quotes. Here is an example:

<?php echo 'Welcome to the wonderful world of PHP!';

In some cases, you can omit the semicolon. In our lessons, when the semicolon is required, we will point it out.

To make your code easy to read, and especially if the echo section is long, you can put it on the next line. Here is an example:

<?php
echo "Welcome to the wonderful world of PHP!";

You can also leave an empty line between both lines of code.

You can create an echo that includes long text that covers many lines. Here is an example:

<?php

echo "A polygon is a geometric plane figure made of straight lines 
      also called segments. Polygons categorized based on their number 
      of sides. Examples of categories of polygons are triangles and 
      quadrilaterals.";

This would produce:

The first webpage after Apache installation

In the same way, you can create as many echo sections as you want. In fact, and/or for any reason you judge necessary, you can use different echo sections and put any content you want in each. Here are examples:

<?php

echo "A polygon is a geometric plane figure made of straight lines 
      also called segments. Polygons are categorized based on their 
      number of sides.";

echo "Examples of categories of polygons are triangles and 
      quadrilaterals.";

This would produce:

The first webpage after Apache installation

PHP and HTML

PHP can be easily used alongside HTML. Probably the easiest way is to embed HTML code in PHP. To do this, include the HTML code in a call to echo. In this case, the HTML code would be included inside the echo's quotes. Here is an example:

<?php

echo "<h1>Tax Preparation</h1>

<p>This application is used to evaluate tax.</p>

<h2>Federal Income Tax</h2>

<p>This section is for taxes paid or owed to the federal government. The Internal 
Revenue Service (or IRS) collects such taxes.</p>

<h3>Social Security/Medicare</h3>

<p>Social security is money set aside for retirement days.</p>

<h4>State Tax</h4>

<p>Many states collect taxes for local expenses.</p>

<h5>Other Collections</h5>

<p>There are many amounts of money collected from a paycheck for different 
reasons. Examples are 401(k), health insurance, life insurance, etc.</p>";

This would produce:

PHP and HTML

If you want, to show where the PHP code starts, you should indent it. Here is an example:

<?php

echo "<h1>Tax Preparation</h1>

    <p>This application is used to evaluate tax.</p>

    <h2>Federal Income Tax</h2>

    <p>This section is for taxes paid or owed to the federal government. The Internal 
    Revenue Service (or IRS) collects such taxes.</p>;

The above code included only the content of the body element of an HTML-based webpage. In reality, the whole code of HTML can be created inside PHP by including the HTML Code in the quotes of echo.

If the HTML section(s) contain(s) quoted strings, which is usually the case for the values of attributes of HTML elements, you can put those values in single-quotes. Here are examples:

<?php

echo "<html>
      <head>

      <title>Watts A Loan: About Us</title>

      </head>
      <body>

      <h1 align='center'>Watts A Loan: About Us</h1>

      <h2 align='center'>Mission Statement</h2>

      <h3 align='center'>Our Mission</h3>

      <p>To build-up people who inspire.</p>

      <h3>Our Strategy</h3>

      <p>To be the most recommendable financial lending institution in the areas where our office is located.</p>

      <h3>Our Ambition</h3>

      <p>To re-invent the way people interpret commercial lending.</p>

      <h2 align='left'>Company Leadership</h2>
      
      <p>Watts A Loan is lead by a team of business-minded professionals.</p>

      <h3>Owner and CEO: Catherine Watts</h3>

      <h3 align='center'>Marketing Director: Frank Heyman</h3>

      <h3 align='right'>Human Resources Officer: Justine Bene</h3>

      <p align='center'>Our financial statements include</p>

      <p>Administration 3.5M</p>
      <p>Revenues</p>
      <p>Margin 23.33%</p>
      CO 480K

      </body>
      </html>";

This would produce:

PHP and HTML

In reality, you can do this in reverse: If you want, put the echo value in single-quotes and the internal quoted sections in double-quotes. Here are examples:

<?php

echo '<html>
      <head>

      <title>Watts A Loan: About Us</title>

      </head>
      <body>

      <h1 align="center">Watts A Loan: About Us</h1>

      <h2 align="center">Mission Statement</h2>

      <h3 align="center">Our Mission</h3>

      <p>To build-up people who inspire.</p>

      <h3>Our Strategy</h3>

      <p>To be the most recommendable financial lending institution in the areas where our office is located.</p>

      <h3>Our Ambition</h3>

      <p>To re-invent the way people interpret commercial lending.</p>

      <h2 align="left">Company Leadership</h2>
      
      <p>Watts A Loan is lead by a team of business-minded professionals.</p>

      <h3>Owner and CEO: Catherine Watts</h3>

      <h3 align="center">Marketing Director: Frank Heyman</h3>

      <h3 align="right">Human Resources Officer: Justine Bene</h3>

      <p align="center">Our financial statements include</p>

      <p>Administration 3.5M</p>
      <p>Revenues</p>
      <p>Margin 23.33%</p>
      CO 480K

      </body>
      </html>';

If you are planning to use relevant PHP code only in some parts of your webpage, you can create (a) PHP section(s) only on that (those) parts. Consider the following example:

<html>
<head>
<title>Database Management: Security</title>
</head>
<body>

<h4>Database Management: Security</h4>

<?php echo "Other database security issues" ?>

<p>One of the techniques to secure a database <cite>object</cite> is through 
a <samp>trigger</samp>, which is an action performed when an event occurs on a 
database or on a table (or view).</p>

</body>
</html>

This would produce:

PHP and HTML

In this case, the PHP parser will consider PHP code only in the section(s) that starts with <?php until the next non-PHP section.

The Short ECHO Tag

Instead of <?php echo ... ?> in your code, you can replace php echo with <= and a space, to get <?= ... ?>. Here is an example:

<?= "A square is a geometric figure made of four equal sides and four right angles."

PHP and CSS

Cascading Style Sheet (CSS) can be used in PHP. If CSS is used inline, you can simply include the HTML elements and their styles in echo. Here are examples:

<?php

echo "<p style='font-size: 0.42cm;'>Hydrogen is the most widely available substance on earth.</p>

      <p style='font-size: 0.32in;'>Hydrogen is the most widely available substance on earth.</p>

      <p style='font-size: 10pt;'>Hydrogen is the most widely available substance on earth.</p>

      <p style='font-size: 1.48pc;'>Hydrogen is the most widely available substance on earth.</p>";

This would produce:

PHP and CSS

If the style is created in its own section along with the rest of the HTML code, you can include that CSS section in echo. Here is an example:

<?php

echo "<style>
	h1 { font-family: Georgia, Garamond, 'Times New Roman', serif;
	     font-size: 14pt;
	     color: maroon; }
	p { background: #66cc33; }
      </style>

      <h1>Wind Power</h1>

      <p>Wind power is a form of energy based on air flowing in a public area. The 
      power is actually seized using mechanical turbines</p>

      <div>Wind power is currently being widely considered to produce electricity 
      instead of fossil fuel and other traditional or costly means</div>

      <p>Wind power is valuable when used in a wind farm, in which case many wind 
      turbines are erected in an area with high air flow.</p>";

This would produce:

PHP and CSS

Terminating PHP Code

So far, to indicate where a PHP expression ended, we used a semicolon. As an alternative, PHP provides a closing tag as ?>. Based on this, you can include what ever you want to display on your webpage between the start tag <?php and the end tag ?>. There are rules you must, and suggestions you can, follow:

Comments

A comment is a section of text that the PHP parser will not read. As a result, you can put anything in the comment. A comment is created in a section. PHP supports two types of comments:


Home Copyright © 2015-2022, FunctionX Next