You are on page 1of 13

Introduction

Overview:
PHP is the most popular server-side language used to build dynamic websites, and though
it is not especially difficult to use, nonprogrammers often find it intimidating. This
introductory workshop from David Powers is designed to change that by teaching you PHP
through a series of clear, focused, easy-to-follow lessons. After briefly explaining what PHP
is and how it integrates with HTML, David introduces the features of the language. He
covers variables, conditional statements, calculations, loops, and functions, as well as
server-side includes and error handing, in a series of hands-on exercises. He then moves to
a larger project that involves building a script to validate and email user input from an online
form. By following along, you'll come to understand the value of creating reusable code, the
importance of PHP security, and how to deal with multiple-choice form fields.
Topics include:
Naming variables
Storing text as strings
Doing calculations with PHP
Using conditional statements to make decisions
Creating custom functions
Deciphering error messages
Emailing the contents of an online form
Dealing with multiple-choice form fields

Transcript 1:

- Hi, I'm David Powers. Welcome to this revised edition of Introducing PHP. PHP is the
language behind four out of every five websites that use a server-side technology. After
showing you how to check your setup, I'll explain how PHP adds dynamic features to a
website. Then we'll dive into the basics of writing PHP scripts. Storing values in
variables, making decisions with conditional statements, using loops for repetitive
tasks, and simplifying website maintenance by storing common page elements in
external files.

Along the way, I'll set you various practical challenges to put your newfound knowledge
to the test, and show you how I solved each problem. The final project shows how to
process user input from an online form and send it by email. It not only brings together
everything you've learned, but it also places emphasis on secure coding practices. Even
if you've never done any programming before, I'm sure you'll find PHP easy to use.
So, let's dive in to Introducing PHP.

Transcript 2:

- Before we start, let's take a look at what you'll need to follow this course starting with
who this course is for. This is a beginner's course.You need absolutely no experience of
PHP or of programming. But don't let that put you of if you've already dabbled with
code. I intend to keep the theory short and focus on getting things done. However you
should have a good knowledge of HTML. Although I won't be using a lot of HTML in the
early part of the course, PHP is most commonly used to generate webpages.

If you try to mix PHP and HTML without really understanding the HTML, you'll end up
with a mess. A knowledge of JavaScript is useful, but it's not essential. PHP and
JavaScript are very diferent, but they both use variables, functions, loops and
conditions so understanding those concepts will speed up the learning process. This is
a hands-on course so you'll need a PHP-enabled web server. Your remote server, the
one that hosts your website will need to support PHP and you could use it for testing
and development, but it's not a good idea, it's much better to setup a local testing
environment.

If you're impatient to get started, you might wonder why I'm telling you to spend time
setting up a local server. First of all, it's much quicker.You don't need to keep uploading
your files every time you make a change. In the early days, you'll likely to make
mistakes so working locally avoids exposing your mistakes in public. Even when you
become more experienced, you'll appreciate being able to develop offlinein your local
testing environment. David Gassner's course, Installing Apache, MySQL and PHP here
on lynda.com will guide you through the setup process.

There are several all-in-one packages that handle everything automatically. What's
more, they're free. The other essential requirement is a script editor. PHP is written in
plain text like HTML and CSS so any text editor will do. However you'll find writing PHP
scripts much easier if the editor has the following features: A PHP syntax checker to
make it easy to spot mistakes in your code. Syntax coloring to highlight the diferent
parts of the language, this also helps identify mistakes.
Code hints to remind you of how to use builtin and custom functions. Line
numbering, and a feature to identify matching opening and closing braces, brackets and
parentheses. During this course, I'll be using a commercial program called phpstorm
9, but it doesn't matter what you use. If you need advice on choosing a PHP
editor, check out Joe Lowery's course here on lynda.com. He looks at a wide range of
options including free editors that are suitable for writing PHP scripts.

It doesn't matter which operating system you're using. PHP is completely platform
neutral. Most PHP websites run on Linux servers, but PHP runs equally well on Mac OS
X and Windows. I recorded this course on a Mac, but I've also tested the code on
Windows and Linux, it runs exactly the same in each operating system. A quick word
about PHP versions: This course is based on PHP 7.0, but it's not exclusively PHP 7
course.

The great thing about PHP 7 is that with very few exceptions, it's backwards compatible
with older versions. PHP 6 was never released publicly so many websites are likely to
continue running on PHP 5 possibly for several years. My recommendation is that PHP
5.5 should be the minimum version you should be running. Official support for PHP 5.4
ended in September 2015. Having said that, the overwhelming majority of the code in
this course will run on PHP 5.4.

I'll always make it clear where a later version of PHP is required and I'll ofer a
workaround for PHP 5.4. There's just one final requirement for this
course, patience. Like all programming languages, PHP is intolerant of mistakes, and
you'll bound to make them while learning. It can be frustrating when you don't
understand why your code doesn't work, but I'll try to point out common pitfalls to help
you troubleshoot problems. And take heart from this: even experienced programmers
make mistakes, the only diference, is that the experienced programmer can usually
spot the error more quickly.

And with patience, you'll learn to do the same.

Transcript 3:
- If you're a member of the lynda.com online training library, you have access to the
Exercise Files for this course. Download them from the course page and save them in a
convenient place on your local computer. I've got them here on my desktop. So let's just
take a look at them. All the Exercise Files are organized in folders numbered after each
chapter. And inside are subfolders for the individual videos that require Exercise
Files. When a file is in a diferent state at the end of the video, _end is appended to the
file name.

If the video starts with a blank file, only the version with _end is provided. And in some
cases, let's just take a look at Chapter Nine, where a lot of files change during the
course of the video, there are two separate folders: begin and end. This is what the files
look like at the beginning of the video and this is what you should end up with at the
end. PHP files need to processed by a web server. So you need to copy all of the
Exercise Files to your testing server root.

The location of your server or document root depends on your testing environment. In
MAMP on Mac OS X, it's at /Applications/MAMP/htdocs. If you're using the built-in
version of Apache on Mac OS X, it's at /Library/WebServer/Documents. In
WampServer, it's on your C:\wamp\www. And in XAMPP on Windows, it's on
C:\xampp\htdocs.

If you're using your remote server for testing or when you eventually want to show of
your new skills, the name of the document root can vary. On many sites, it's
htdocs. public_html is also common as is www. To use the Exercise Files, copy the
Exercise Files folder and all of its contents to your server document root folder. Rename
the folder to introducingphp as one word and all in lowercase.

File and folder names in a website become part of the URL, so they should never
contain spaces. It's also a good idea to use all lowercasebecause most PHP servers are
case-sensitive. If you set up the Exercise Files like this in a local testing environment the
URL for every file will
be http://localhost/introducingphp/ http://localhost/introducingphp/ followed by the folder
names and file name. After you've copied the files and renamed the folder, make sure
that your testing server is running and type the following URL in a browser address
box:http://localhost/introducingphp/introduction/test.php http://localhost/introducingphp/i
ntroduction/test.php And if everything is working correctly you should see the following
message.

This day of the week here is generated dynamically by PHP. So it will difer depending
on when you test this page. If the day of the week is out by one, it means you need to fix
the time zone on your server. We'll deal with that later in the course. And if this page is
working you're ready to continue with the course. If you couldn't display the test
page, check the following: Is the web server in your testing environment running? In
most cases, this will be Apache.

Can you view other PHP files on localhost? If you can't, you need to fix your installation
of Apache and PHP first. But if you can view other PHP files, did you copy the Exercise
Files folder to the server root? Did you rename the folder to introducingphp as all one
word? And did you spell the URL correctly? Mistakes in file and folder names are a
common cause of problems. Hopefully, that checklist will get you up and running.

Transcript 4:

- Let's take a look at what's new in this course. It's been completely revised since the
original version was incorporated into the Lynda.com online training library. The original
version was based on PHP 5.3 which is no longer officially supported. PHP 7, a major
new version of PHP,was released at the end of 2015 so the course has been updated to
take PHP 7 into account. But all of the techniques in this course are backwards
compatible with PHP 5.4 which at the time of this recording was still being used on
nearly a third of all PHP servers.

Although some code will work only in PHP 7, I always show you an alternative for older
versions of PHP. This version of the course no longer covers installing Apache, PHP
and MySQL because David Gassner's course of the same name goes into that subject
in far greater depthand is constantly kept up-to-date. However, the basic structure of this
course remains unchanged so you can easily pick up this version if you are halfway
through the original one.

In addition to bringing the course up-to-date with PHP 7, I've made some
improvements. I've removed the discussion of "magic quotes"because they're no longer
part of PHP. I've also used the simpler array syntax that was introduced in PHP 5.4. In
response to feedback from members, I've clarified the explanation of several
features such as the increment operator. There's also a whole new chapter on
interpreting PHP error messages.

But perhaps the biggest change is the introduction of challenges and solutions. Most
chapters now end with a short practical exercise that challenges you to think how to use
the techniques in the previous videos to solve a problem. The idea is to get you to start
thinking like a programmer rather than simply copying and pasting code. Most
challenges take only a few minutes then I show you how I solve the problem. Well, I
hope I've whetted your appetite and you're now ready to dive into PHP.

Transcript 5:

- I assume that you've already installed a testing server and are raring to go. Before we
can start, it's important to make sure we're all usingthe same basic settings. So we're
going to run a very simple PHP command to inspect how PHP has been
configured. Fire up your editing program. I'm using PHP Storm, and I've already set up a
project to save all of my files in the introducing PHP folder in my server document
route. Create a blank file. I'm going to create it in the ch01, 01_01 folder.
And I'm going to call it config.php. My editing program automatically adds the .php on
the end of the file name, and it also inserts the PHPopening tag on the first line. If your
editing program adds HTML code, delete all of the HTML and replace it with this
opening PHP tag on the first line. It's an opening angle bracket, or <, a ?, and then php.

Make sure there are no spaces between any of the characters. Then insert a space
after the opening PHP tag and type phpinfo followed by () and a ;. That's all we need to
type. There won't be any of a script in this file, so we don't need a closing php tag, but it
doesn't matter if you add one. Just save the page and then load it into a browser. Now I
can load directly from my editing program, but if necessary, if you've set up the same as
I have, the URL in your browser will
be localhost/introducingphp/ch01 /01_01/config.php.
And this configuration page contains a huge amount of information. And the contents
will difer depending on how your server has been set up. If you've never used PHP
before, this can seem like information overload. Fortunately, you don't need to
worry about most of it. Let's just highlight some of the most important points. Right at
the very top is the PHP version number. I'm using PHP version 7.0.0RC4.
RC stands for release candidate. I recorded this course shortly before the final release
of PHP 7, so the version I'm using contains exactly the same features as the final
release, but it was still going through some final quality tests. The final release of PHP 7
should be available by the time you watch this. You don't need to be using PHP 7
yourself, but do make sure that the version is not less than PHP 5.4. There were
important changes in PHP 5.4, and if you're using an older version, some of the code in
this course won't work.

Now halfway down the screen here is Loaded Configuration File. This is the location of
the main configuration file, php.ini. You need to make a note of the location because you
might need to edit it. So this is where my php.ini is. That's the active version of
php.ini. Then let's scroll down, and this configuration section lists in alphabetical
order all of the features enabled on your server.
The one that we want to look at is called call. So let's just scroll down, and here we are,
call. What we want to look at in here is this one here, display_errors. This tells you
whether error messages are displayed on screen, and the value that you need to look
at is the one listed under Local Value. And in my case, it's on. That's exactly what we
want. If it's set to of in your system, some errors will result in a completely blank screen.

In a live website, that's exactly what you want. Displaying errors can reveal
information that could expose you to malicious attacks. However, in a testing
environment, it's vital to see all error messages. So if display_errors is set to of on your
local computer, you'll need to change this setting as described in the next video. Then
the next thing we need to look at is error_reporting. It's this one here. And it's a numeric
value, and it should be set to 32767 as shown here.
If it shows a lower value on your system, follow the instructions in the next video. And
there's one other setting that you should check. It's a bit further down. It's in the date
section. Let's just find that. Here we are, date. Make sure there's a value for Default
timezone. I'm based in the UK, so I've set it on my system to Europe/London. If the
timezone is wrong for your location, time and date functions will display the wrong
values.

So that's all we need to do. Those are the most important settings. If you ever ask for
help with a PHP feature not working as expected, the first question you're likely to be
asked is whether you've run php info to check if a feature is enabled. Although this page
feels overwhelmingwhen you first start out, you'll quickly learn to value the insight it
gives you into your service configuration. In the next video, I'll show you how to
change the configuration settings on your server.
Transcript 6:

- You've seen how to check the configuration of your server using phpinfo. If you need to
make any changes, you need to edit the PHP configuration file, php.ini. The location of
the file depends on your server. It's listed here as Loaded Configuration File so that's the
location of php.ini on my server. You also need to check this value here, Additional .ini
files parsed. Usually, the value is none, as it is here.

But it's possible that something else is overriding values in the main configuration
file. We'll come back to that later. First, let's deal with the main version of php.ini that's
listed as the Loaded Configuration File. php.ini is just a plain text file so you need to
open it in a text editor. In Windows, double-clicking the file in the File
Explorer automatically opens it in Notepad. On a Mac, it doesn't have a default program
to open it.

So I'm going to use this program, TextWrangler, which is a free script editor for the
Mac that you can download from www.barebones.com.But if you're on Windows,
Notepad is just fine. And because I've been working with php.ini, it's already loaded it
into TextWrangler. Lines that begin with semicolons, like all of these lines here, are
comments. The configuration commands are on lines that don't begin with a semicolon.

And because it's such a long file, it's best to use the script editor search command to
find the configuration setting you're looking for. I'm going to search for
error_reporting and then click Next. Here is a line that begins with a semicolon, so that's
a comment, that's not what we want. Let's search for the next one. This line here doesn't
begin with a semicolon so this is the actual command.

The value of error_reporting is set to E_ALL, all in uppercase. This is a PHP constant so
it must be always in uppercase. And this is the value that you should have. This results
in even minor errors being reported. Some developers prefer a less strict level but I think
it's very important to see every error message, particularly when you're learning. So
make sure that that is set to E_ALL.

The next thing that we need to look at is display_errors. And that's actually very close
by. Here it is. In my version, display_errors equals On.That's what we want in a local
testing environment. Now, the default value in MAMP is of so you must change this to
on. The last configuration command you might need to change is date.timezone so let's
search for that. This is on a comment line but the next line doesn't have a semicolon at
the beginning so that's the actual value.

So date.timezone, I've set it to Europe/London. The value is normally a continent or


ocean followed by a slash and the name of a city. This is because PHP automatically
adjusts for daylight saving time. You can find the correct timezone setting for your
location in the PHP online documentation. Let's take a look at that. This is a list of all the
supported timezones in PHP. There's a very large number of them and they're all
organized geographically.

America covers the whole of the American continent, North, South and
Central. Something which is important to realize about America is that it doesn't include
ofshore islands. For example, if you're in Hawaii, you actually have to look in
Pacific because that's where Hawaii is listed. Europe, that lists most of the countries in
Europe but some ofshore islands are listed as being in Atlantic. Find the correct
location and then change the value of date.timezone in your php.ini.

When you've made the changes, save php.ini and restart the web server then reload the
script that runs phpinfo. This is phpinfo here, and when you've made the changes, you
should be able to see the changes reflected in here. Those changes will normally be
relisted in Coreunder Local Value, or in the case of date, also under Local Value but of
course not in the Core section, it's in the Date section.

However, if the changes didn't take efect and the file is listed alongside Additional .ini
files parsed, that was up here at the top, if you've got a diferent file listed here, then
locate that file and make the changes there too. Making changes to your testing server
configuration through editing php.ini sounds a daunting prospect because it's such a
long file, but it's really quite simple. Just make sure you edit the configuration settings on
lines that don't begin with a semicolon.

And don't forget to restart your web server after making the changes.

Transcript 7:
- You can still change configuration settings, even if you can't edit the main PHP
configuration file, php.ini In this video, I'll cover briefly three alternative approaches. This
could be useful if the company hosting your website doesn't give you control of a
php.ini. The most common alternative is to use a file called .htaccess, but this woks only
if your server is running Apache. In the exercise files in the Chapter 101_03 folder, I've
created this file, htaccess.txt and this shows you how to change the configuration
settings on Apache for error reporting, display errors, and date.timezone.

The lines that begin with a hash or pound sign are treated as comments, so the lines
that don't have the pound or hash sign are the actual configuration directives. Each
setting must be on a separate line, and where the configuration directive sets of
value, the line begins with php_value, followed by a space, the name of the directive
that you want to set, another space, and then the value. In the case of error_reporting,
you need to use an actual number, rather than e_all, because Apache doesn't
understand PHP constants.

When the directive toggles a setting on or of, the line begins with php_flag, then the
name of the directive, and after a space, either on or of. To change the settings on an
Apache server, save this file in the server's document root folder as .htaccess and
remove the .txt on the end of the file name. I've put that .txt on the end of the file
name because on a Mac, file names that begin with a .

are hidden in the Mac finder. Using a .htaccess file has an immediate efect. There's no
need to restart the web server. Some installations of PHP support a diferent type of
user configuration file called .user.ini, and there's an example of that here in
user.ini.txt. This uses exactly the same syntax as php.ini. So if you check with your
hosting company and is says that it supports .user.ini files, you can rename this file.

Just take of the .txt on the end and begin it with .user.ini. And then you can upload it to
your remote server and it will change the configuration settings. If neither of these
alternatives is available to you, or you simply want to change the settings for a single
file, you can change some settings on the fly at run time, and there's an example of how
to do that in this file: runtime_config.php. For most settings, you use ini_set and then
between a pair of parenthesis the first argument is the configuration settings that you
want to change, then after a comma the value that you want to set it to.
In the case of e_all, that's a php constant, so it's not in quotes. But normally, the second
value should be in quotes. And if you want to change the timezone setting, you need to
use date_defaut_timezone_set and then between the parenthesis, the timezone
setting. So, that's three alternative ways to change configuration settings in PHP. You
should consider these only if you don't have direct access to editing the main
configuration file, php.ini If your web server is running Apache, you can use this style,
the .htaccess style.

If your server supports .user.ini you use this style. And then if you want to change just
the run time, you can use this ini_set. But this is the least efficient way to do it, unless
you need to make only a temporary change, or make the change for a specific script.

Transcript 8:

- Great! So you've decided to learn PHP. Before you can do anything useful, you need to
learn the basic grammar or syntax of the language.A small amount of time spent
learning the basics will more than repay itself in the end, saving you hours of frustration
and confusion. Let's start with a broad overview of what PHP is and what it does. PHP
stands for PHP Hypertext Preprocessor, it's a rather ugly name for a very powerful
scripting language that's relatively easy to learn.

You can use PHP in your websites for a wide range of tasks such as process and email
online forms, upload files from a webpage,automatically generate thumbnails from a
larger image or watermark them. PHP can also read and write files directly on your web
server.And that's not all. You can use it for date and time calculations that take into
account diferent time zones and whether it's daylight saving time.
It can change the content of a page depending on the date, day of the week or
time. And one of its important uses is communicating with a database. Most of the time,
PHP is used with a open-source MySQL database, but it also works will all leading
databases. There's much, much more that you can do with PHP. But let's not get carried
away. We need to start with the basics. When someone visits a website, the browser
sends a request to the web server, with a static webpage that consists of just HTML,
CSS images and maybe JavaScript, the server simply responds by sending all the
necessary files.

However, if you use PHP on your webpages, the server sends the page to the Zend
engine that powers PHP for processing before sending the response to the
browser. And if a database is involved, the Zend engine is responsible for sending the
request of the database and processing the results. All these usually takes only a
fraction of a second, so there's no noticeable delay. PHP was designed as an
embedded language. What that means is that PHP code is often mixed or embedded in
HTML markup.

This is a basic feedback form and it's got PHP commands mixed in with the HTML
code. So immediately after this h1 tag on line 23, the following line has a block of PHP
code, that's followed on line 25 by more HTML. This is a paragraph with a class
warning and it says "Please fix the items indicated", and on the following line, line 26, is
another small block of PHP code. So let's see what happens to this paragraphwhen we
load the page into a browser.
Paragraph is completely hidden, it's been hidden by those PHP commands. But if I try to
submit this form without filling in the fields, let's do that, the paragraph is now
displayed as are these other error messages. But let's take a look at the page's source
code. There is that h1 tag and it's immediately followed by the paragraph with the class
warning, "Please fix the items indicated". There's no PHP code inside at all, it's just
HTML.

That's because PHP is a server-side technology. The PHP code remains on the
server and after its been processed or passed, it outputs text and HTML. PHP can look
daunting when you start out. But you'll soon become familiar with common features that
are used in most PHP scripts. Variables act as placeholders for unknown or changing
values. Arrays hold multiple values rather like a shopping list.
You use conditional statements to make decisions and loops to perform repetitive
tasks. Functions and objects perform preset tasks. When using PHP in a website, use
the filename extension .php for all webpages. Although you can mix pages with the .html
filename extension in the same site, it's a good idea to use .php for all files, even if they
don't contain PHP code.

By doing so, you can add PHP code later without needing to change the URL. PHP
code normally needs to be enclosed in special tags. The opening tag consists of a less
than symbol, a question mark and php without any spaces between the
characters. Don't use the shorthand opening tag that omits the letters php, it's not
supported on all servers. The closing tag is a question mark immediately followed by a
greater than symbol.
The closing tag is essential if your PHP script is embedded in HTML. But if can be left
out if there's no other code following your PHP script.PHP needs to be processed by the
web server before the page can be viewed in a browser. So as a rule, you need to store
all pages inside the server root folder. In many cases, this is a folder called htdocs, but
the name can vary. On some servers, it's called www, wwwroot or public_html.

There are cases where PHP files can be stored outside the server root for security
reasons, but that's something you can learn about at a later stage. The need to process
the PHP script afects how you view PHP files locally. Don't try to double-click a PHP
file in Windows File Explorer or the Mac Finder, it won't work. You'll either just see the
raw code or you'll be prompted to download the file. Make sure that your local web
server is running.
And always view the page using a URL. If you're using a similar setup to me, that means
http://localhost/introducingphp/ and that's followed by the location and name of the
file within the introducing PHP folder. So that's a broad overview of PHP. Let's get on
with the practical details.

Transcript 9:

You might also like