Sign In | Subscribe

Enter your Sign on user name and password.

Forgot password?
  • Follow us on:
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to PHP
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Lecture Comments (13)

1 answer

Last reply by: Janet Bargewell
Thu Nov 8, 2012 1:23 PM

Post by Janet Bargewell on November 8, 2012

I am having the same problem as Katherine. I have saved my file as .php and still get the (' ; ?>) at the end of Hello World. Additionally the script is not returning the text as a header even though I am using the <H1> tag.

1 answer

Last reply by: Joe Springer
Tue Oct 23, 2012 4:20 PM

Post by katherine Hernandez on July 30, 2012

Hi: when I test my php file I get all the headings, but on each php output is showing the ('; ?>) ad the end of the heading. Why is that?

1 answer

Last reply by: Mark Deming
Sun Jul 29, 2012 1:46 AM

Post by Mark Deming on July 29, 2012

Rookie status how come when I put this code exactly like you have it in dreamweaver CS5 the code doesn't work. I know the code should work there as well.

0 answers

Post by Jorge Guerrero on March 21, 2012

Sorry, foks, my bad. I was not opening the file INSIDE http://localhost/intro2php/sample.php

That's why code was behaving funny! :)

1 answer

Last reply by: Matthew M.
Wed Apr 11, 2012 4:01 PM

Post by Jorge Guerrero on March 21, 2012

This video dates back to July, 2011. I don't know what's going on, but the only way I could get PHP to work on my computer was like this

<?php echo <h1>Second PHP Block

No further syntax was needed nor recognized by the browser. You guys should check this, as the OP was suggesting the above notation.

Also, you should add a year to the date return of when the comments were posted. If something was posted. It gives one a good sense of how far back the question dates and if it may still be pertinent.

1 answer

Last reply by: Matthew M.
Wed Apr 11, 2012 3:09 PM

Post by Pawel Orzeszak on February 9, 2012

your tutorial is for real idiots... and I'm one of them ! Even and idiot like me can understand everything from your course. Thank you so much ! No problems so far

1 answer

Last reply by: Matthew M.
Tue Feb 7, 2012 3:12 AM

Post by Herve Gnidehoue on December 21, 2011

I can't get my php file displayed. I followed all the steps to to install Xampp. Here is what I did as you suggested :
I installed Xampp under C:\Users\user1\Xampp.
In the xampp folder, I created a file named MyFirst.php (I mean the first homework)in the folder htdocs. But when I tried to open it, it did not work. The same source code is sent back to me but when I changed the extension to .html, of course my web browser open it but the php tags were trailing around. My concern are where exactly to save the files ? Once Xampp is installed, do I have to configurate any other file ? Does my Apache really communicate with the PHP ? I had confirmation that Apache is working though.

Your First PHP Script

  • For web development, the PHP interpreter usually takes as its input a .php file containing a mixture of HTML and PHP code and generates as its output, HTML.
  • PHP delimiters, or PHP 'opening and closing tags', are used to delineate PHP code within a text file.
  • When using PHP delimiters to separate PHP code from HTML, this is called “escaping from HTML mode” into “PHP mode”.
  • One can escape into and out of both PHP and HTML mode as often as needed within a file.
  • Additional Resources:

Your First PHP Script

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:19
    • Lesson Overview
  • .php To HTML 1:00
    • .php To HTML
  • PHP Delimiters 2:20
    • PHP Delimiters: Opening & Closing PHP Tags
  • 'Hello, World!' Example 4:34
    • Echo Statement & PHP Tags
    • Adding Second Heading
  • Homework Challenge 9:56
    • Homework Challenge

Transcription: Your First PHP Script

Hello again, and welcome back to's Introduction to PHP course.0000

In today's lesson, we are going to be introducing you to your first PHP script, a script called helloWorld.php,0005

that is a mixture of PHP code and HTML and outputs a simple HTML message.0011

What specifically are we going to cover in today's lesson?0022

First of all, we are going to cover how a PHP file gets converted to HTML.0024

We know that, on our web server, when we request a PHP file, Apache invokes the PHP Interpreter to process it.0032

And the PHP Interpreter processes the file, generates HTML, and sends it back to Apache, which sends it back to the client.0040

We are going to talk a little bit more about that process.0047

We are also going to introduce the concept of PHP delimiters, which are also known as PHP opening and closing tags.0049

And we are going to have another homework challenge to discuss.0055

PHP to HTML--how does that work?0062

Well, first of all, as mentioned, we know that, when a PHP page is requested, the PHP Interpreter is invoked by Apache.0065

And basically, from an input/output point of view, the PHP takes a text file as input and generates some sort of output.0073

Now, PHP is a general scripting language, so it always is going to take a text file as an input, because it is not just using the Web...0080

It doesn't always generate HTML output; it can generate things like PDFs and images, and other kinds of text files, like XML.0090

However, for this course, because it is a Web-based course, we are going to be dealing with text files0098

that have the .php extension and/or a combination of both HTML and PHP code.0103

The PHP Interpreter is going to process these files, and on the other end, it is going to generate HTML output.0130

Essentially, it is going to be generating HTML dynamically, on the fly.0136

How does PHP know when it receives a .php as its input what is PHP code and what is HTML?0143

The answer is in what are known as PHP delimiters, also called opening and closing PHP tags.0150

Basically, these are special tags that inform the PHP Interpreter that it's about to interpret some PHP code.0158

The pair of tags are right here: this one is the opening PHP tag--it is a less than sign,0165

followed immediately by a question mark, followed immediately by the lowercase characters php.0175

And the closing tag is simply a question mark followed by a greater than sign.0180

So basically, anything the PHP Interpreter encounters between these two tags, it is going to treat as code.0186

Now, everything outside of those tags, PHP just treats as text, and it simply passes it to output.0192

As PHP processes your file, if it encounters HTML, it simply passes it along to output.0198

Once it reaches a group of PHP delimiters, it executes any code in there; and if there is output to be generated,0205

adds it to the output it has already passed along.0212

When it exits out of the PHP code snippet, then it goes ahead and passes on any more HTML0215

or other text it finds, until it reaches the end of the file.0222

Now, when entering PHP mode, or when a PHP Interpreter encounters a PHP opening tag,0225

That is called escaping from HTML, and you are entering into PHP mode.0232

Once the closing delimiter is reached, the ending PHP tag, you are said to be returning to HTML mode.0238

Within any single PHP file, you can intermingle PHP code and HTML as often as desired.0247

So, you can basically start off in HTML, process a little PHP code, get back to HTML, output something,0256

go back into PHP code, do a little processing, and so forth, as often as needed, throughout your file.0264

Let's actually take a look at our first PHP file, called helloWorld.php.0269

This is helloWorld.php; we have opened it up in our text editor, PSPAD, which we installed in the last lesson.0276

And you can see that we have some simple HTML tags: opening and closing HTML and body tags.0282

And then, we can see the PHP opening and closing tags, or delimiters, that we had just talked about.0293

Within the PHP delimiters, everything that PHP finds, it executes as code.0300

And so, basically, there is one line of code within these PHP tags, and it's what is known as an echo statement.0307

An echo statement simply outputs any text after the echo statement that is between a pair of quotation marks, and adds that to the output.0314

And the way you do that: you write echo, and then a space, your text you would like to output--0324

in this case, it's an HTML header tag with the "Hello, World!" message--0329

and then, after the quotation ends, you finish it off with a semicolon.0337

When PHP processes this file, it starts at the beginning, and all it sees is HTML, so it starts just passing that on as is to output.0342

It reaches this PHP tag, and it says, "OK, now I'm in PHP code; I have escaped from HTML."0352

"Let me process what is in here...OK, I have found an echo statement, and an echo statement outputs text."0357

So it says, "What is it outputting?" It outputs "Hello, World!" in an H1 header tag.0363

What it is going to do is add that to the HTML up here that it has already passed along.0370

Once it finishes processing the echo statement, it is going to exit out of PHP mode back into HTML mode,0374

and then pass everything else it finds onto the output.0380

It is going to add it onto the "Hello, World!" text it outputted, as well as these intro tags.0384

If we were to go and view this web page...start Firefox...0391

and I have it saved in the lecture_4 directory of the intro2php directory that we talked about in the last lesson...0398

and if we view helloWorld.php, we will see the message "Hello, World!"0406

And actually, if we go ahead and view the page source for this (I'll blow it up a little bit here),0410

We can see the HTML code, which is passed on as is by the PHP Interpreter, as expected,0417

and instead of the PHP code fragment in between the code delimiters right here--the PHP delimiters--0424

it has been replaced with the HTML itself that was output.0435

So, this PHP section up here is replaced with the HTML that that code section output.0440

And that is how PHP interprets the file when it goes into and out of HTML mode.0447

As mentioned, you can do that as often as needed.0454

So, as an example, you could actually add another header statement here in HTML mode.0456

We're in HTML mode right now, because we are not between the PHP delimiters.0463

Let's call it Second Heading and close the header statement.0467

And begin another block of PHP code, using the opening and closing tags.0476

I'm going to create another echo statement, which is just echo, followed by a space, followed by a quoted piece of text.0483

In this case, I'm going to create another header and head it up with the message Second PHP Block, and end it with a semicolon.0491

Now, if we save the file, and we go to view it, what we should see is the code up here outputted as before.0505

When we leave the PHP code, we will enter back into HTML mode.0516

So, we should see this statement just as is in our HTML source code.0519

And this PHP code is going to do the same thing as up here: it is going to output another piece of text.0527

Let's go ahead and view this in our web browser again.0533

We saved it, and we refreshed the page; and now we have the three different headers.0536

If we go and look at the page source (let's pull it up again), we can see the HTML opening and closing tags and the body tags.0541

We can see the original HTML output by our first PHP code block.0550

And if we look back at the source code file, we can see the second heading here0559

that was output as is by the PHP Interpreter, because it was in HTML mode.0566

And then again, the second echo statement right here was simply output0572

and added to the rest of the output that the PHP had generated along the way.0578

Finish off with the closing tags, and it reached the end of the file.0583

That is how PHP works, going into and out of PHP mode.0586

And we are going to be using that all throughout this course; this is sort of a basic example.0590

So, I would like to introduce you to another homework challenge that I would like to have you do.0598

Again, it is another hands-on opportunity to work with PHP code.0603

What we would like would be to have you re-create the Hello World example, or script, that we did in this class, by hand, on your own.0610

Open PSPAD; type everything by hand; and it may have the tendency to want to copy and paste,0617

but the idea is that you really want to code the text yourself, because you will learn about making mistakes.0624

You will learn that maybe you forgot how to write the PHP closing tag correctly.0629

Maybe you left off the question mark or something; maybe in your echo statement, you forgot to put quotation marks around your text.0633

So, I really encourage you to go ahead and code the file on your own.0643

Then, you should save the file as helloWorld.php, and I want you to save it in the appropriate folder0648

of your Apache document root, so that you browse to it at this URL right here.0656

And the reason we are having you do that is, again, to reinforce what we learned in the last lesson,0664

which is learning how the local files saved on your computer below the document root map to localhost URL's.0669

And as you go along, you might see (if you make any mistakes) some error messages.0680

Try to figure out where you went wrong with your code--if you didn't save it in the right spot...0685

for example, if you are having trouble viewing it, or if you are getting an error message from PHP.0690

Also, be sure to make sure that Apache is properly started0694

and that you appropriately escape in and out of HTML using the PHP delimiters.0699

And also, if you make any mistakes, PHP will give you error messages that have a line number listing where the error was made.0706

And so, you can go back to your source code.0713

Maybe it says there is an error on line 11--maybe you left out this initial quotation mark here.0715

So, if we go and run this again, we will get an error that there was an error on line 11.0724

We can go back to PSPAD; and the way you do that is: you have to turn on line numbers in PSPAD.0731

And then, that way, you can see that on line 11, this is where your error was.0739

You can go back in, add the quotation mark, and save the file.0743

And when you reload it, everything should work as before.0750

That reaches the end of today's lecture; thank you for watching, and we look forward to seeing you at the next lecture.0755