Screen Printing

TUTORIALS » PHP / MySQL » PHP essentials (guided tutorials)

Starting with PHP

Page 1 of 2

You are in page:
» 1. Strings and echo
PHP—just as any other programming language—is divided in instruction lines, each line contains one instruction and ends with a semicolon (;). Failing to end each instruction line with the semicolon will cause the script to return an error and stop.

Strings and echo

The first PHP instruction you are going to learn is the function
. In PHP
means something like "send the following text to the browser".

Let's take a look at an example:
 echo 'Hello World <br />';
 echo "How are you?";

There are two instruction lines in the example code. In each one I am starting with the instruction
followed by a string. A string is a set of alphanumeric characters, in other words, a text.

Notice how both strings are placed between quotation marks. The string in the first line is placed between single quotes and the second string is placed between double quotes. Then each instruction ends with a semicolon.

What is the difference between single and double quotes for strings in PHP? I will explain that in another tutorial, but don't go there just yet; there is more to learn first. For now let's just say that all strings in PHP must be place between quotation marks.

Now it is time for you to practice your first PHP script.

Read this before you start your practice
Do you remember how, when you created HTML files, you could simply double click on the .html file inside your computer and the browser would automatically open, displaying the rendered content of your file? Well, PHP is different.

Remember, PHP scripts are processed by the server, not by the browser; so all .php files must be uploaded to the server and accessed by entering the URL path into the address bar of your browser.

Also, make sure PHP works on your server.

  1. Create a plain text file named myfirstecho.php
  2. Write inside your file:
     echo 'Hello World <br />';
     echo "How are you?";
  3. Create a new folder inside your server and name it exercises.
    Note: In some servers, the folders are also called directories.
  4. Upload myfirstecho.php to the exercises folder you just created in your server.
  5. Test your file by opening your browser, entering the URL of your file in the address bar
    [ http://nameofyourserver/exercises/myfirstecho.php ], and pressing ENTER.
    Note: The name of your server can be if you signed up for a server on the internet or localhost if you set up your own computer to work as a local web server.
Q. What do you see on screen?
  1. Now right click on the screen of your browser (still in your page, not in this one!) and select View Page Source or View Source.
Q. Is the source of your page—as shown by your browser—the same as the .php file you created?

You are right! It is not the same. Before you started with PHP, all the .html files you created were sent to the browser unprocessed, so the sources presented by the browser and your actual .html files were pretty much the same.

Now, when we work with .php files, the server processes all the instructions and sends only the results of this process. In this case:
Hello World <br />
How are you?
This is the magic of PHP! and this is only the beginning!

Now follow into the next page of this tutorial!

More pages of this tutorial → Starting with PHP  12>

Comments & Questions