1

What is PHP?

PHP: Hypertext Preprocessor (PHP) is an open source general-purpose scripting language that is widely used for web development and can be embedded into HTML.

PHP code is executed on the server and is commonly used for:

  • Creating, reading, writing, and manipulating files on the server.
  • Generating dynamic page content.
  • Collecting form data.
  • Modifying information stored in your database.
  • Controlling user-access and encrypting data.
  • Sending and receiving cookies, etc.

Why PHP?

  • PHP is FREE!
  • PHP is compatible with most modern servers, such as Apache, Nginx, IIS, etc.
  • PHP runs on numerous platforms, including Windows, MacOS, Linux, etc.
  • PHP supports a wide range of databases.

PHP Tags

A PHP script starts with <?php and ends with ?>.

Example:

<?php
    // Your script here
?>

Let me show you an example of a simple PHP file which uses a built-in function called “echo” to output the text “Coding with PHP is fun!” to a web page:

<html>
    <head>
        <title>A Simple PHP Page</title>
    </head>
<body>

<?php
    echo “Coding with PHP is fun!”;
?>

</body>
</html>

Note: PHP statements always end with semicolons (;).

The “echo” Function

PHP has a built-in “echo” function or language construct used to output text. It does not require the use of parentheses.

Let’s take a closer look:

<?php
    echo “PHP is cool!”;
?>

Note: The text should always be inside a single or double quotation marks. All echo statements always end with a semicolon (;).

HTML markup can also be added to the text in the “echo” statement:

<?php
    echo “<strong>Bold text with line break at the end</strong> <br />”;
?>

Comments

As with any other programming language, we can also add comments within a PHP script by using the following syntax:

Single-line comments:

// This is a single-line comment

Multi-line comments:

/*
This is a multi-line comment.
You can write anything between the slash-asterisk
and asterisk-slash symbols.
*/

A single-line comment starts with // while a multi-line comment starts with /* and ends with */.

Comments are not executed as part of the program. They are mainly used to remind yourself of what the code does or to communicate to others and make it easier for them to understand what the code is doing.

To Be Continued…

We will be discussing PHP Variables and Constants next, so stay tuned.