Writing the First Script in PHP- Introduction to Scripts

Creating scripts in PHP is very simple. We just have to open the text editor and write the code. The code file must be saved in .htdocs file with the extension .php. Consider the following code for example:

Let us say you save the file with the name: hello.php

< ?php
echo “Hello, world!”;
?

view the results in your browser by visiting http://localhost/hello.php.

This above script is very simple, but let ’ s break it down line – by – line to explore a few features of PHP.

  1. The first line tells the PHP engine to expect some PHP code to follow:
    <?php
    When the PHP engine first starts processing the page, it assumes it ’ s dealing with plain old HTML until
    told otherwise. By using the PHP delimiter, < ?php, you ’ re telling the PHP engine to treat anything
    following the < ?php as PHP code, rather than as HTML.
  2. The next line displays the message “ Hello, world! ” :
    echo “Hello, world!”;
    PHP ’ s echo() statement takes a string of text — in this case, “Hello, world!” — and sends it as part
    of the Web page to the browser. The browser then displays the “ Hello, world! ” text to the visitor. Notice
    the semicolon (;) at the end of the line; this tells PHP that we ’ ve reached the end of the current
    statement.

Note:echo() doesn ’ t have to be given a string of text; it can display anything that can be displayed, such as numbers
or the results of expressions. An alternative to echo() is the print() statement, which works in exactly the same way except thatit also returns a value (true). Generally speaking, we can use print() instead of echo() in our
code if we prefer.

3. The final line of your simple script tells the PHP engine that it ’ s reached the end of the current section of
PHP code, and that the following lines (if any) contain plain HTML again:

? >


Embedding PHP within HTML

The best thing about PHP is that you can embed PHP code within HTML. In fact, each .php script that we write is essentially treated as an HTML page by default. If the page contains no < ?php ... ? > tags, the PHP engine just sends the contents of the file as – is to the browser.

We can use this feature to make our “Hello, world!” example prettier by adding a proper HTML header and footer and including a CSS style sheet. Enter the following code and save it as hello2.php in your document root folder:

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

 <title>Hello</title>

 <link rel=”stylesheet” type=”text/css” href=”common.css” />

 </head>

 <body>

 <h1><?php echo “Hello, world!”; ?></h1>

 </body>

</html>

We can include any customized CSS code and include it in the root folder by the name common.css.

Run our new PHP script by typing http://localhost/hello2.php into your browser’s address bar. One should see a more stylish page.

How It Works

This example shows how we can embed PHP within an HTML page. The PHP code itself is exactly the same — echo “Hello, world!” — but by surrounding the PHP with HTML markup, we’ve created a well-formed HTML page styled with CSS.

First, a DOCTYPE and the opening html tag are used to declare that the page is an XHTML 1.0 Strict Web page:

Next, the head element of the Web page gives the page a title — “Hello” — and links to a style sheet file, common.css:.

Finally, the body element includes an h1 (top-level heading) element containing the output from the PHP code — echo “Hello, world!”; — and the page is then finished with a closing html tag:

Note that we don’t need to have the tags on separate lines.

In this case, the tags and enclosed PHP code are all part of a single line. Meanwhile, the common.css style sheet file contains selectors to style some common HTML elements — including the h1 heading used in the page — to produce the nicer-looking result.

Leave a Comment

Your email address will not be published. Required fields are marked *