For more information, please see full course syllabus of Introduction to PHP
For more information, please see full course syllabus of Introduction to PHP
Discussion
Study Guides
Download Lecture Slides
Table of Contents
Transcription
Related Services
Basic PHP Syntax
- There are four valid forms of PHP delimiters: long (<?php, ?>), script (<script language="php">, </script>), short(<?, ?>), and ASP-style(<%, %>).
- The short and ASP-style PHP delimiters are not always available, as they can be enabled/disabled using php.ini , so they should not be used for portability reasons.
- The long form delimiters are recommended because they are always available and are XHTML- and XML-compliant.
- php.ini , located in 'php' folder of the XAMPP installation directory, is the global PHP configuration file that controls how PHP runs on your local webserver.
- When using XAMPP, Apache must be restarted for changes to php.ini to take effect.
- The phpinfo() function outputs information about the current PHP configuration.
- Statements are individual instructions to be executed by PHP and are always separated by semicolons.
- Comments are text added to PHP code that are ignored by the PHP interpreter and used to aid programmers in understanding the code.
- Single line comments and are delimited by either // or #. PHP interprets all text after these delimiters until either the end of the line or a PHP closing tag is reached, whichever comes first, as comments.
- Multi-line comments are signified to PHP by the start and end delimiters, /* and */. All text between this style of comment delimiters is treated as comments.
- Care should be taken to avoid the nesting of multi-line comments.
- Coding conventions describe a set of rules for how code should be formatted and written that can only be enforced by the programmer and not by the programming language. The purpose of coding conventions are to make code easier to read, understand, maintain, and debug.
- Additional Resources:
Basic PHP Syntax
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:09
- Lesson Overview
- PHP Delimiters 0:38
- Long and Script Form
- Short and ASP Style
- Example
- php.ini: PHP's Configuration 3:40
- php.ini
- Configuration Directives
- Short and ASP Style: Enabled/Disabled
- phpinfo()
- Statements 14:28
- PHP Statements
- Example: PHP Statements
- Comments 16:53
- PHP Comments
- Single-line Comments
- Multi-line Comments
- Example: PHP Comments
- Coding Conventions 24:26
- Coding Conventions
- Example: PHP Coding Conventions
- Homework Challenge #1 33:51
- Homework Challenge #1
- Homework Challenge #1 (cont.) 35:41
- Homework Challenge #1 (cont.)
- Homework Challenge #2 36:09
- Homework Challenge #2
- Homework Challenge #2 (cont.) 38:07
- Homework Challenge #2 (cont.)
Introduction to PHP
Transcription: Basic PHP Syntax
Hello, and welcome back to Educator.com's Introduction to PHP course.0000
In today's lesson, we are going to be talking about basic PHP syntax.0005
We are going to be talking about, specifically, PHP delimiters, or PHP opening and closing tags,0011
which is something we learned about in our Hello World example.0016
We are going to be talking about two features of PHP language and all languages: statements and comments.0020
We are going to learn how to properly format them and what the proper syntax is.0027
We are also going to talk about what coding conventions are.0031
It is a very important topic, and we will review that at the end.0035
PHP delimiters: we already have experienced that when we wrote our Hello World example.0040
The delimiters are what lets the PHP Interpreter know what our code is.0045
There are four types that are available in PHP; what we have used is the long form, as you can see here; it should look familiar.0053
There are three other types that are allowed by PHP, as well.0060
One is known as the script form, and the other two are known as the short and ASP style forms.0064
The script form looks like this; and this is the opening script tag, and this is the closing script tag.0071
So basically, you would have all of your PHP code inserted in this area, in between them.0079
If you have worked with Javascript before, these tags may look familiar to you.0085
This type of tag is always available in your PHP scripts; and that is opposed to the short form and the ASP style, which are shown here.0089
These are the opening tags for those forms and the closing tags.0099
The reason the short tags and the ASP style tags are only sometimes available is:0105
you can configure PHP to allow the use of these tags or not.0109
For example, you can configure it to allow short tags and maybe not allow ASP tags.0114
If we go and look at some code in a file called delimiters.php, we can see each of these four types of tags in action.0122
Hopefully...yes, they all fit on the screen here.0137
We can see the long form that we have used before; we can see the script form; we can see the short form and the ASP form.0139
We know that these two, as mentioned, are always enabled; so the code within these should run fine.0150
Sometimes, the short form is enabled, and sometimes the ASP style is enabled; it depends on the configuration.0156
Let's load the file up in our web browser and see what happens.0162
We go to delimiters.php, and what we can see is: the long form and the script form work--they echoed the statements as they were supposed to.0166
The short form also processed the echo statement, so that worked, as well.0176
However, it looks a little weird down here with the ASP style code.0179
And so, it looks like they didn't process that; it looks like the ASP style tags are disabled, currently.0183
You can even go and look at the source code (I'll blow it up a little bit); you can see that PHP didn't even recognize the code0190
in between the ASP style tags as PHP code; it just printed it directly to output, which is what you can see here.0201
It printed out our code.0207
Now, the way to enable that is through a configuration file called php.ini.0211
What php.ini is, is a global configuration file for a particular PHP installation.0222
It contains what are known as configuration directives, which are basically key-value pairs that control how PHP functions.0229
For example, the directive for whether short tags are enabled or not, is the directive short_open_tag.0239
That is the property, and you set a value to it: you can set it either to on or off.0248
So, php.ini has a directive for both enabling short tags and enabling ASP tags.0253
As we can see, or as we just saw, the short_open_tag's are enabled in PHP right now, and the ASP tags aren't.0265
So, if we go look at php.ini, we should be able to confirm that.0272
And in XAMPP...it actually depends on how your PHP installation is set up--where your php.ini file is located.0277
But for XAMPP, it is located, by default, in the PHP directory under your XAMPP installation directory.0286
If we go and browse there--go to XAMPP directory and go to PHP--and we want to select the php.ini file to view,0294
and we are actually going to select to open it with PSPAD, our familiar editor,0305
this is a php.ini file, and this is what it looks like.0313
You can see, there is a lot of text in here; and basically what these are, are comments to you, the user,0316
about how the configuration directives work in the file.0321
These aren't actually configuration directives; they are comments, and the way you denote a comment in the .ini file is by using a semicolon.0325
All this here is comments, until we scroll down...until here, where we reach our first directive.0333
In this case, the directive is called engine; it's a property, and you can set the value.0342
We are not going to talk about that one today, but we are going to talk about the two we mentioned before,0347
the short_open_tag directive and the asp_tags directive.0352
Now, as you can see, there are two short_open_tag directives in here, and that is intentionally done by XAMPP.0358
I'm not exactly sure why they chose to do it that way; it is typically bad form to have the same directive listed twice.0366
But essentially, what happens when you do list something twice (just so you understand what is going on) is:0372
the configuration takes on whatever the last value was that was given in the script.0378
So, up here, we have short_open_tag set to Off, but then XAMPP added the same directive and set it to On.0381
The final value for open_tag is going to be on, which is what we saw in our file; the short tags worked.0389
Now, as we look down here at the ASP tags, we can see it's off; and we can expect that, because our ASP tags didn't work.0394
So, let's go ahead and set that to On; and we are going to save the file.0400
And if we go back and browse to our file again and refresh it, we should see everything.0406
And actually, it refreshed, and it didn't change everything; the ASP tags still aren't working.0415
And I actually did that on purpose, and the reason was to let you know that, any time you make changes to the php.ini file0421
in an XAMPP installation, you actually have to restart Apache for the configuration changes to take place.0428
And that is because Apache on the default XAMPP installation runs as a module.0435
So, we are going to open up the XAMPP control panel; we are simply going to stop the Apache server by clicking on Stop.0441
And click Start again; it will take a few seconds, and it will reboot the server.0449
Now, when we go back and load our page and refresh it, the ASP style tags work.0453
And as you can see, they worked; and if we looked back at the page source, you can see that0458
the ASP code that was just simply output before has now actually been processed, and echo statements were processed.0465
One other thing to note, related to php.ini, is: there is a function that is built into PHP called phpinfo.0478
We haven't talked about functions yet; it is sort of an advanced concept, and it is something that we'll get to later in the class.0489
But essentially, what a function is, is: it's an identifier that tells the PHP Interpreter to go off0495
and execute another branch of code, or another section of code, located somewhere else.0502
Now, in this class, we are going to learn how to define our own functions; but PHP actually has its own built-in functions,0508
which are defined elsewhere as part of the code base.0514
And so, it automatically knows how to find those; so just by including this function name in our code, we'll be able to make use of the function.0519
And what the function, phpinfo, does is: it is used to output your current configuration.0529
So, you have a file here called phpinfo, and it is actually just a solid PHP file.0535
There is no HTML; we have our PHP opening and closing tags, and then we just have the phpinfo function.0543
You need to follow that by a semicolon, as we will see in the next slide; we are going to talk about statements and the importance of semicolons.0549
This is how you call the phpinfo function; if we go and load this up in our browser, this is the output that we get.0556
And as you can see, there is a lot of information that it outputs for you.0568
It outputs things about how your computer is configured, how Apache is configured,0574
most specifically how PHP is configured, as well as its extensions.0579
And so, what we want to do is: using info, we can see whether short tags and ASP tags are enabled.0583
There is a lot of information here, but the section we are going to scroll down to is called Core.0589
These are sort of the core directives that are part of all PHP installations.0593
If we look here, we can see the asp_tags property, or directive; and we can see that this value is set to On.0600
You can see that there is actually a master value and a local value; the php.ini file configures the master value.0607
Local value is used for some of the properties and directives within the php.ini file; it can be changed on the fly and using other configuration files.0615
So, even though the master value might be on, you can maybe temporarily disable it using an advanced configuration file,0626
which is something we are not going to be using in this introductory course.0635
And not all directives are able to be overridden; so you can't always override the value in the php.ini file.0639
As expected, asp_tags is on; and these directives are listed in alphabetical order.0646
If we go down and look for the short_open_tag directive, we can see that that is on, as well; that is what we expected.0653
And as you can see, phpinfo lets us know that.0662
So actually, let's go back and alter the php.ini file so that both ASP tags and short open tags are off.0666
And if we go ahead and stop our Apache server and restart it, reload phpinfo, and scroll down to the Core section again,0682
we can see that ASP tags are now turned off, and also open tags are both turned off.0704
If we were to go back and view our original file, we can see that neither of the short tags or the ASP style tags work.0712
If we look at the page source, you can see that PHP just simply outputted all of the content0724
in between the short tags and the ASP style tags, and it didn't process it.0732
And for this reason, it is not recommended that short tags or ASP style tags be used; it is for portability reasons.0737
And because, on one configuration, PHP can be set up so that it allows the short tags or the ASP style tags,0745
and on another configuration, it might not be, you might develop your PHP code for a website on your home computer,0753
and you allow short tags, but then you go ahead and decide to make it live and upload it to your web host, and the web host disabled.0760
You are not able to change the configuration file (which may be the case at your web host).0768
In that case, in order to get your PHP scripts to work, you are going to have to go through and change all of the opening and closing tags0773
to the appropriate form, whether it be the script form or the long form.0779
And so, in essence, what that does is produces what is called portability; and that is being able to move your code0784
from installation to installation, without having to change it.0791
For that reason, these two forms are not recommended; so which forms should you use then?--because there are two left that are always available.0794
There is the long form and the script form.0801
As you can probably guess, because in all of our scripts so far we have used the long form, that is the recommended form.0804
And the reason for that is that it's XHTML and XML compliant.0810
Because XHTML, particularly, is becoming more popular, by using the long form of the PHP tags,0815
you will be able to insert PHP code within your XHTML files and have it still be compliant XML or XHTML.0823
For that reason, we are going to always use the long form; and that, in general, is the recommended form, even by PHP.0833
It is pretty much the standard that is used out there.0841
The second one that you will see most often is the short tags; and they are used a lot in the past,0846
but it has changed and moved to the long form, for reasons such as...not XML compliance, but because there are some security bugs with them,0852
which is why PHP created the ability to turn them on and off.0860
Let's talk a little bit about statements now: what statements are, are something just like the phpinfo function we just had, or an echo statement.0870
They are individual instructions to the PHP Interpreter that need to be executed.0878
The way you separate statements in PHP, the syntax that you use, is through semicolons.0883
Semicolons are what delineate one statement from the next, and that is how PHP knows that.0892
So, we are going to look at a file called statements.php; we can see, as some of the files we have seen before, it's a mixture of HTML and PHP.0896
And within the body tag of the HTML, we have a PHP code block using the long form of the PHP opening and closing tag.0907
And you can see, we have four echo statements here, and each echo statement is separated by a semicolon.0916
This one is off the screen, but if you scroll down to the end, you can see, there is a semicolon there, as well.0924
That lets PHP know that, once it reaches the end of this line and hits a semicolon, that echo statement is done.0929
It will start processing the next line and a new instruction, which in this case, is an echo statement.0936
So, if we go and view this in our browser, we can see that all of the statements were properly output.0941
This spells it out: all of the statements were separated by semicolons in the PHP code.0953
The reason is that that semicolon, again, lets PHP know that you have separate statements.0959
There are different instructions.0966
For example, if we came back, deleted the semicolon, saved the file, and tried to view it again in our browser, we are going to get an error from PHP.0968
Basically, it is saying that it is expecting a semicolon, and that there is an error on line 10.0979
If we go back and look, it's not actually line 10; sometimes the error messages are off by a line or two for different, complicated reasons.0983
We can see that PHP threw an error because it didn't recognize that we had two statements.0992
When it didn't find the semicolon here, it saw this all as one statement, and that is not the proper format for an echo statement.1001
Now, I want to talk about comments: you have statements in your code which are code that gets executed by PHP.1016
Comments are text that you add to your PHP code within the PHP code block that are not interpreted by the PHP Interpreter,1023
and are used as an aid to the programmer.1032
They are information that you encode in your PHP file that gives information about how the code works and what it does, along with a lot of other useful information.1037
And as mentioned, PHP does not process the comments; it does not execute them; it just ignores them completely.1049
Now, in PHP, there are a couple of different styles or ways, or syntaxes, for defining comments in your code.1057
Single-line comments, which are comments that just span one line, can be delimited or started with the two forward slashes or the pound sign.1064
And when using these styles of comments, everything after the two forward slashes or the pound sign,1077
until either the end of the line is reached or a PHP closing tag is reached, are treated as comments.1084
There is also a third style of comment known as a multi-line comment, which are also called C style comments.1094
Because they span multiple lines, you need to be able to delineate the start and end to them.1103
And so, PHP has a starting delimiter, which is a forward slash followed immediately by an asterisk,1109
and an ending delimiter, which is an asterisk followed by a forward slash.1115
And basically, all text between those two character sequences are treated as comments.1120
Let's take a look at comments in action.1127
Here we have another simple HTML and PHP combination file, and within our PHP code block, we want to have some comments.1132
Right here, this is an example of the single-line comment using the forward slashes.1143
For those of you that may be familiar with C++, this is known as the C++ style comment.1148
This right here is an example of a single-line comment in what is called Perl style, which some of you may be familiar with.1153
It is a style used in other programming languages.1160
In this course, we are only going to be using the C++ style, as that is the more commonly-used variety, and it is what you will see in most places.1164
We will not be using the pound sign for our single-line comments.1171
Now, single-line comments can be on a line all by themselves, as in here--as in this comment itself--and also as in this line right here.1176
We have down here an echo statement, "Hello, World!" and we want to add a comment to it that says, "This statement outputs 'Hello, World!"1184
Now, that is not, in this case, a particularly useful comment, because it is a very simple thing that we are doing in the echo statement.1192
But as you will see, as your code base grows and you end up having PHP files that are hundreds of lines long,1198
these comments are going to become very useful to you, to figuring out what you were doing in your code,1204
because sometimes you might work on your code and you won't have a bug that comes up for maybe a month.1209
And then, when you go back to look at your code to figure it out, the comments are there to help you understand what is going on.1214
This is a single-line comment on its own line; you can also place single-line comments on the same line as a statement or a PHP code.1219
You do that by just placing it after the PHP code, and this is also known as an in-line comment.1228
As mentioned, everything from the forward slash until the end of the line (which would be here) or a PHP closing tag is reached are interpreted as comments.1236
If you look here, you can see: we have three single-line comments, and in this one, we have a PHP closing tag.1247
What is going to happen is: when PHP is processing this file, it is going to go through and process the echo statement.1253
It is going to see that this is a comment; this is a comment; this is a comment; and this last line here is a comment.1261
And so, everything until the end of the line or until a closing tag is reached is a comment, so PHP ignores all this.1267
Then it reaches the closing tag; and so, what actually happens is: at that point, it jumps out of PHP mode.1272
So, all of the rest of the code that was in our file (which we'll look at in a minute) is no longer treated as PHP code,1278
because you have escaped out of PHP mode.1285
So, if we go and view this file in our web browser, we can see that everything after that PHP closing tag was simply output.1287
As you can see, we have the closing tag here; it was reached; here is the text that was afterwards, and there was more text in the rest of the file.1304
That was simply output as is, because we have jumped out of PHP mode.1312
Now, let's talk about multi-line comments; and I'm going to go ahead and remove this, so the rest of the file works.1326
Multi-line comments are also called C style comments, for those of you that are familiar with the C programming language.1332
They look like this in code: they start with this character sequence here and end with this character sequence here.1341
It is typically for comments that span multiple lines, and PHP treats everything between those two comment sequences as a comment.1347
Now, one thing you need to be careful about is nesting comment sequences, or nesting multi-line comments.1359
What that means is: if you have one multi-line comment that goes from here to here (let's say), and you had another comment within it1364
(let's say we wanted to add a comment here), what is going to happen is: this part after this nested comments ending tag1379
is no longer going to be treated as a comment, and it is going to throw an error.1389
The reason for that is: because the rule is that PHP treats everything from here, from this sequence,1394
until the next sequence of an asterisk and a forward slash as comments, when it comes across the nested comments' forward slash,1399
it is going to stop and say "this is no longer a comment," and it is going to start treating this right here as code, which is invalid code.1407
If we go and run this file, we are going to get an error, and it says that I get an error on line 39.1413
And if we go back and look at our code on line 39, here we have our error.1423
I'm just going to remove this now, so that the error is deleted.1427
I wanted to make one other comment about multi-line comments: the multi-line comment style can also be used for single-line comments.1433
For example, this is a comment that just spans one line, but it uses the multi-line comment style.1440
The reason for that is that sometimes, you want to make a single-line comment stand out.1446
And maybe you have been using, for example, the forward slash style for all of your comments.1450
By creating it with the multi-line style, it may make an important point stand out, and so that is what they can be used for, as well.1456
Now, I want to talk about a very important topic called coding conventions.1468
What coding conventions are: a set of rules for how your code should be formatted and written.1474
And specifically, it is rules that are not enforced by the language.1480
For example, we have learned that PHP enforces that statements have to be ended with a semicolon; that is part of the PHP syntax.1484
We learned that comments have to be started with two forward slashes,1491
or multi-line comments need to be enclosed in the forward slash and asterisk character sequences.1494
If we don't follow those rules, PHP is going to complain and give us an error, and our code is not going to work.1501
But there are other rules that we can apply to our code that PHP won't care about.1507
For example, we could put all of our statements on the same line.1513
As long as they are separated by semicolons, PHP is going to treat them as individual statements, and it doesn't care that they're not on separate lines.1516
However, to make the code easier to read, we are going to want to put statements on separate lines.1523
That is one of the main purposes of coding conventions: to make your code easier to read, understand, maintain, and debug.1527
So, when you go back and look at your code later, if you see three statements--three echo statements, for example--1537
all in one line, it is a little bit harder to read than if they were on a separate line; and so, it makes the code easier to read.1543
And again, coding conventions can only be enforced by the programmer, and not the language itself;1549
so it's very important that the programmer make sure to use these rules.1554
And there are actually many different coding conventions that are available out there and have rules for all sorts of different things--1559
how to format your comments, how many statements you can have on a line, things like that.1564
Which convention you use is not so important as the fact that you consistently apply the convention that you choose.1571
And so, the conventions that we are going to be using in this course, I am going to go over in this file called codingConventions.php.1580
One of the first conventions is regarding the PHP delimiters.1590
As mentioned, in our PHP code for this course, we are only going to be using the long form of the PHP delimiters.1593
This really emphasizes the point of why coding conventions are important, because let's say we allowed either the long form or the script form.1600
Now, when a person...maybe we wrote some code, and we have another person on our team that goes back to edit the code...1610
when they go through a particular PHP file looking for PHP code, if they don't know that we have,1615
for example, allowed the script version of PHP delimiters to be used,1622
when they are quickly browsing through the file, anything they see in a script tag, they may just ignore and overlook,1628
whereas everything between the long form will stand out.1632
So, what we do is: by making the convention to always use the long form, that way the programmer will be able1636
to key in quickly on exactly what the PHP code is.1643
Again, PHP allows you to use either the script form or the long form of the PHP tags, and it is not going to complain.1647
But by convention (which is something you enforce), you allow yourself to make the code easier to read and understand.1655
And again, it is something you have to enforce on your own.1663
For statements, we are going to use the convention of having each statement on its own line, or having them separated by new lines.1666
For example, these two echo statements are on separate lines, and that is using our convention.1676
Against the convention is to have multiple statements on the same line.1683
The reason for that is a readability reason; this is a common convention used out there.1688
We also have conventions for comments and how comments should be used.1694
Basically, for the single-line comments, you should always have a space between the two forward slashes and your comment.1701
This comment right here is using the convention; this comment without the space is against the convention.1709
This is just a readability thing, and it is also a common convention that is out there on the Web.1715
And again, whatever convention you choose to use on your own, just be consistent in whatever you use.1720
Additionally, if we used an in-line single-line comment, which is a single-line comment after some PHP code,1727
put a space between the end of the statement and your comment, because again, if we took out that space,1735
it makes it a little harder to read; and also, if we took out the space here, it makes it even a little bit harder to read.1744
That is why we have these conventions.1749
In this course, we will always have a space after the two forward slashes, and always a space after the semicolon for in-line comments.1751
Multi-line comments are a little more complex, as far as the convention that we are going to use.1759
This is actually the required form for multi-line comments; you just need to have this starting comment sequence and this ending comment sequence.1766
Everything in between is treated as a multi-line comment.1775
However, the style that we are going to use is down here; and what the style dictates is that the opening of the comment,1780
or the beginning of the comment, starts with a forward slash followed by two asterisks instead of one.1788
That opening sequence is on a line all by itself.1793
Additionally, the closing sequence is the same; but it is also on a line by itself.1797
Basically, the comments go on every line in between, and before each line of text in your comment, you have an asterisk and a space.1802
You want all of the asterisks to line up with the first asterisk of the opening tag.1813
Fortunately--this seems a little...it would be hard to line all of these things up;1818
fortunately, PSPAD (one of the nice things of it being a text editor with added features) will automatically help you to format multi-line comments.1821
And actually, if you are in PHP code in PSPAD (which we are), if you just type a forward slash followed by two asterisks and hit the Enter key,1832
you can see, it already has started to format a comment for you.1842
You can say Comment Line 1, and you hit Enter, and it adds the asterisk and a space for the next one, Comment Line 2.1845
And then, to finish the comment, you just add the forward slash to the last asterisk.1854
And so, we have the opening comment sequence on its own line and the closing comment sequence on its own line.1860
Additionally, single-line comments, which we mentioned can also use the multi-line style (which is the forward slash and the asterisk),1867
follow a similar convention; and it is almost the same as the required convention, except that1875
we start with a forward slash, followed by two asterisks (as we had done up here for the multi-line comments).1882
And again, this is just a common convention that is out there; it is very popularly used, and so we are going to be using it in this course.1890
And you can see that, because of the syntax highlighting in PSPAD, it lets you know when you have formatted a comment according to convention,1899
because right now, this comment is blue.1909
And if we go ahead and just make it a regular multi-line comment, without the extra asterisk, and save it,1912
we can see that PSPAD turned it to gray.1922
So, PSPAD also helps us out in this regard, and it lets us know if our comments are not formatted correctly...1925
or--excuse me--"formatted according to convention," because as far as PHP is concerned, you can do it either way; but you want to follow convention.1931
A last thing that I want to talk about, a coding convention that we are going to use in this course, is regarding the indentation of your code.1942
This is something that is going to come up all throughout this course, as we introduce different code structures in the PHP language.1948
Basically, what the code indentation convention that we are going to use is: any time you open a block of PHP code with opening and closing tags,1957
you want to have the opening and closing tags lined up on the same indent,1968
and you want to have all your PHP code within those tags one indent further inward.1972
So, these opening tags are aligned here; we have a tab in between here to begin our code.1978
This code section right here is formatted according to convention; this code down here is not.1986
We can see that the code within the PHP block is lined up at the same indent as opening and closing tags.1992
And hopefully, you can see that it is not as clean of a look; it makes it a little harder to read.2000
And so, that is why the indentation is used--to make things easier to read.2007
You could even go back and put these all the way back here if you wanted to; PHP is not going to complain, because it's something that is legal.2010
But by convention, you don't want to do that, because it is not going to be easy to read.2018
So, we are going to have them look like this, and that is the convention for this course.2023
Now, I want to talk about a homework challenge that we are going to give you to try out, to try some of the material that we have learned in today's lesson.2032
Basically, what I want you to do is replicate some of the things that we did today.2041
I'm going to have you create a phpinfo.php file only containing what we had in our course today,2046
which was the PHP delimiters and the phpinfo function in a statement.2055
That means that, when you use phpinfo, you have to have a semicolon afterwards; so remember to use that.2063
That will allow you to see how your current configuration is set up.2069
I also want to have you save it in a folder called lecture_5 in the intro2php folder within your document root.2073
You will be able to access it through this URL here.2080
And again, the reason for mentioning that is just to reinforce the concept we have been learning,2084
about how URL's on a local web server match up to the actual files located on your hard drive.2090
Once you see your configuration, you can see, its short tags and ASP tags are enabled.2099
You can open up the php.ini file in PSPAD, like we did today; and again, it is located in the PHP folder of the XAMPP installation directory.2105
And assuming you have the default setup--by default, in XAMPP 1.7.4 (which is what we standardize in this course),2116
short tags are enabled, as we saw, and ASP tags are enabled.2126
I would like you to find the asp_tags directive, change its value to On, save the file, and go ahead and restart Apache.2130
And then, when you look at the phpinfo file, you should be able to confirm that the changes took place.2137
And then, I want you to go back and also disable the short tags and the ASP tags, because as mentioned,2144
those are not recommended tags, and we are not going to be using them in this course.2151
And that is also going to give you a second opportunity to practice playing with the php.ini file.2154
And again, after you have made changes to the ini file, you are going to need to restart Apache in order for those changes to take place.2160
I also have a second homework challenge for this lesson.2170
What I am going to have you do is create a file called lecture_5-hw.php; it is basically just a name for Lecture 5 homework.2174
Within that file, create an HTML skeleton, with just HTML opening and closing tags, head tags, title tags, and body tags.2182
And within the HTML body, I want you to escape into PHP mode, using the long form of the PHP delimiters.2191
I want you to create three echo statements and make sure that they are all on separate lines2203
and they are indented one indent deeper than the opening and closing tags.2207
I also want you to add a comment for each echo statement.2211
I want you to do one with the style of single-line comments on their own line, an in-line single line comment2215
(which is a single-line comment after code, on the same line as code), and also using a multi-line comment (a comment that spans several lines).2223
Make sure that each comment you have is formatted according to the conventions we talked about.2232
And also, practice letting PSPAD help you format your multi-line comments.2238
And then, after you do this, view your script in your browser to ensure that only the contents of your echo statements,2245
and specifically not your comments up here (because your comments were not properly delimited with forward slashes or the /*)...2251
PHP is not going to interpret them properly, and they may appear in your output; so make sure that you have done that right.2262
And basically, what this little exercise is going to do is give you practice learning how to format comments according to convention.2269
It is also going to teach you, up here in this step, how to follow the convention of statements all being on one line, as well as indentation.2277
Then, as we have done in class today, after your first echo statement, I want you to remove the semicolon and save it and re-browse to it.2288
You should see an arrow with a line number, and the line number specified--take note of that.2298
You go back to that line number in your file: so for example, maybe, if we took out this line here, it might say the arrow was on line 9.2304
Let's browse the file; actually, it's saying it's on line 10.2317
The reason for that is because it sees this as all one statement, and so it sees the arrow as occurring when the statement ends, which is line 10.2327
But if you note the line number, you will go back and be able to see where the error is approximately.2335
I am going to have you do what is known as commenting out; this is a common debugging practice.2342
It is a way to narrow down errors in your code when you are experimenting with them.2347
What you do is: you just put two forward slashes at the beginning of the line, and what that does is:2352
as we know, it has PHP treat everything afterwards as a comment; so this is no longer an echo statement.2356
Now, what that should do is no longer cause an error, assuming that is where the error was (which we know it was).2363
If we go back and browse the page again, we can see that the error didn't happen.2370
It also, at the same time, didn't output the echo statement, because we had it commented out.2376
So then, I'll just have you go back and un-comment the echo statement, and review things to make sure that they all work right.2380
Oops, I forgot to add the semicolon back.2389
Everything works.2397
The reason for this example of removing a semicolon is to learn how PHP errors work and what information they provide to you.2400
and also, to learn the concept of commenting out code, which is a common debugging practice in PHP (and, in fact, all programming languages).2411
That ends today's lecture; thank you for watching Educator.com, and we look forward to seeing you next time.2419
1 answer
Last reply by: Kitt Parker
Mon Dec 29, 2014 4:01 PM
Post by Douglas Sunday on July 28, 2012
just wondering, can I get this course on DVD?
3 answers
Tue Feb 7, 2012 3:18 AM
Post by essica slaton on December 29, 2011
Help! Why is my code visible on the page?