PHP Lesson 1 – First PHP Script

If you are new to PHP, this is for you. Here you will learn how to create and run a PHP web page for the first time.

Introduction

In this tutorial you will learn how to run a PHP script.

Open a text editor application. Be sure to use a simple text editor, nothing like MS Word because a big text programs will add extra characters and bugs to your code.
Advertisement

In order to run PHP, you are going to need a web server with PHP installed. We recommend iPowerWeb.com for only $7.50 with Unlimited Disk Space, Subdomains, and Emails Accounts. Another requirement for running PHP scripts on a server is the extension file name format. By default you must use a “.php” extension on your PHP web page files. Other extensions that may work depending on you server are “.php3” and “.php4”.

O. K. Let’s start on our first script. To begin a block of PHP statements we need to start it with a PHP open tag:

<?php

or

<?

And now we end our code with a closer tag:

?>

Lets create a new file and name it “first.php”. Type the code below (or copy and past it) in to the file named “first.php” and upload it to your server:

<?php
print “Hello World!”;
?>

Now access the url page through the browser to see the out put. It should read:

Hello World!

Note that if you view the source html code from the browser application, all php code is hidden. It was all parsed (processed) before the browser application could view the page.
Structure

You can structure the PHP code all on one line if you prefer like this:

<?php print “Hello World!”; ?>

You can also use the short start tag ” <?” with out the “<?php” like this:

<? print “Hello World!; ?>

You can also change the “print” function to “echo” or “return” functions and achieve the same results like this:

<? echo “Hello World!”; ?>
or
<? return “Hello World!”; ?>

You can also combine HTML and PHP all in one page like this:

<HTML>
<HEAD>
<TITLE>newsourcemedia.com</title>
<META http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
</head>
<BODY>

<? return “Hello World!”; ?>
</body>
</html>

You can also use more then on line for the string of data like this:

<?
print “Hello
World!”;
?>

or

<?
print ”
Hello
World
“;
?>

Tryout some of these above samples. You can also change the statement inside the quotes if you like to see if all still goes well.
Understanding the Code

Let take a look at how the code works. We already know from above that “<?” and “?>” are the start and end PHP tags. Next we used three deferent word: “print”, “echo” and “return”. These word commands are built-in PHP Functions that outputs data (you’ll learn soon how to build your own functions).

A Function is a command that performs an action and is modified by other data usually supplied to it. The data ( a collections of characters or a “string”) is usually always placed inside single or double quotes. Most functions must have parentheses after their name like function( ). However the three functions we have use above are exempted from that role.

Lastly, we used a semicolon “;”. The semicolon tell PHP that this is the end of the line of code and to execute this line of code.

Most programmers forget to end there code with the semicolon. You will find that most of the reasons for your codes not working properly is that you forgot as well.
Summary

In this tutorial you learned how to use a text editor to create a PHP script, how to name your PHP files, how to start and end blocks of code with start and end tags, how to output data to the browser with functions, and how to combine PHP and HTML codes.

This is just the tip of the iceberg so be sure to click here to check out our other tutorials on PHP.

If you like our script, please rate it!

You may also like...

7 Responses

  1. Ellen says:

    I can get a simple PHP file with only < ? return "Hello World!"; ?> to work, but when I put that code into an existing HTML page (and rename the page so that it’s a PHP page), nothing shows. Same server. Why?

  2. Alex says:

    Try working backwards. For example, try adding the HTML code to the first PHP file that worked instead of add the PHP to the HTML.

  3. Mark says:

    Firstly I want to thank you for the online lessons. I need to learn PHP in a hurry for a project that is about to kick off. I have very basic HTML skills and need to learn another language that can utilise databases and PHP is the best for this due to the open source software that it can utilise. The problem that I am experiancing is this when I use the return function on the structure age the text does not appear. However if I use the echo function it does. I am using Apache 2 webserver and PHP 5. Could it possibly be a configuration problem?

  4. Alex says:

    Do this:
    // setup your
    function yourfuncname(){
    $variable = “value”;
    return $variable;
    }
    //Now grab the value of the function
    $new_ver = yourfuncname();
    echo $new_ver; // returns “value”

  5. Ojo Jacob says:

    please, how do i up load my codes. (the tutorial was cool and simple to understand). Please keep up the good work.

  6. SAURABH CHOUDHARY says:

    How to upload PHP file on the server.

  7. adeel says:

    hi sir plz i join the php classes…..my contect number is 92 335 2590 474

Leave a Reply