Introduction to PHP Programming

Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

This is the first in a series of lessons that are intended to pragmatically teach you the practical components of PHP scripting language to get you quickly and easily started. In this series, we will take you through from PHP basics to very advanced concepts.

What is PHP?

PHP is a server-side scripting language. It is a powerful language that is used to develop static/dynamic websites or various web applications. Originally PHP used to stand for Personal Home Pages and currently stands for Hypertext Preprocessor. A PHP file ends with the ".php" extension.

Other server-side scripting languages include ASP -- Active Server Pages, JSP -- Java Server Pages, and CFML -- Cold Fusion Markup language.

PHP is a cross-platform technology proven open-source and a free widely-used language for web-based software applications. The latest stable release is PHP 7.4.3, released on 20th February 2020.

PHP is extremely important for beginners and professionals as well, especially those working in the web domain who want to become great software engineers.

What is a Scripting Language?

A scripting language is a language that interprets scripts at runtime. A script (usually embedded in other software environments) is a set of programming/computer instructions that are interpreted during runtime. Scripting language codes usually execute without compiling.

PHP scripts are server-side, meaning that they are interpreted on the server with PHP installed. Scripts enhance the performance or perform routine tasks for the respective applications. The client computers can only access PHP scripts through a web browser.

PHP code can be embedded into HTML code/tags using a standard syntax,

  1. <HTML> <?php PHP CODE ?> </HTML>


or can be used in combination with various web-based systems such as content management systems and web frameworks.

To get the full advantage of learning PHP, knowledge of a few other things is important, but not a must:
  • It is recommended to at least know the basics of HTML although not necessary.
  • knowlledge of the database management systems (DBMS) is an added advantage for database-powered applications.
  • Knowing JavaScript and XML is important for more advanced topics such as interactive applications and web services.
PHP Tags

When PHP parses a file, it looks for an opening and a closing tag, which are respectively, <?php and ?>. These tags tell PHP to start and stop interpreting the code between them, and anything outside of a pair of opening and closing tags is ignored by the PHP parser. A PHP file can contain HTML tags and client-side scripts such as JavaScript. We can also create pure PHP files without any HTML tags.

Requirements

We will use a local LAMP stack installed on Ubuntu Linux according to the instructions given here. However, one out of lots of PHP online editors can be used to follow the examples. For us to learn and run PHP scripts successfully, we will need a web server for example Apache, A database e.g., MySQL and a PHP parser for generating HTML output that can be sent to the web browser.

PHP Hello World

Just to whet your appetite about PHP, we start with a small conventional PHP Hello World program:

Code: Select all

<?php echo "Hello World!"; ?>
Create an empty file /var/www/html/tutorial.php using any editor such as using Gedit (no need of advanced and professional editors at this stage),

  1. $gedit /var/www/html/tutorial.php


and write into it the contents of the Hello World PHP program above.

To see whether your web server can correctly display the content generated by a PHP script above, open the browser, type

http://localhost/tutorial.php

and hit Enter.

You should see the phrase "Hello World!" displayed by the browser as shown below:

phphw.png
phphw.png (7.32 KiB) Viewed 1104 times
phphw.png
phphw.png (7.32 KiB) Viewed 1104 times

Once your PHP installation is working fine, you can follow this tutorial using the PHP and your local web server.

We can embed our PHP Hello World script within HTML and execute to get similar results:

  1. <html>
  2.    
  3.    <head>
  4.       <title>PHP Hello World</title>
  5.    </head>
  6.    
  7.    <body>
  8.       <?php echo "Hello World!";?>
  9.    </body>
  10.  
  11. </html>


In the code snippet above, all of the PHP syntaxes have been processed and stripped off; the only thing returned to the client from the web server is pure HTML output. Therefore, PHP scripts are executed on the server, returning the output in the form of HTML. The client applications do not need to have PHP installed. Note that, HTML tags start with <> and end with </>.

PHP Statements

PHP statements are used to refer to the PHP lines of code, they end with a semicolon (;). For just one statement, the semicolon can be omitted. But, we must end each statement with a semicolon if we have more than one. It is recommended that you always end your statement(s) with a semicolon for the sake of consistency.

This short tutorial is sufficient to get you started, the rest is just details! You can now continue to the next lesson.
Next >>>
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply

Return to “LAMP (Linux, Apache, MySQL, PHP)”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 3 guests