IntroductionWelcome| 00:04 |
Welcome to PHP MySQL Essential Training.
My name is Kevin Skoglund.
| | 00:08 |
In this course, we'll review the basic
PHP data types, and learn to use PHP's
| | 00:12 |
control structures, like logical
expressions and loops, in order to
| | 00:16 |
control how our code executes.
We'll cover many of PHP's built in
| | 00:22 |
functions, we'll also learn to define our
own custom functions.
| | 00:26 |
We'll cover the basics of SQL and learn
how to use PHP to connect to a MySQL
| | 00:30 |
database, which will allow us to create,
read, update, and delete records.
| | 00:36 |
We'll learn the fundamentals of user
authentication, that is the process of
| | 00:39 |
requiring a username and password to log
into a PHP application.
| | 00:43 |
And finally we'll build a complete PHP
web application from start to finish.
| | 00:48 |
A content management system that will
allow admins to log in and manage the
| | 00:51 |
website content that gets displayed to
the public.
| | 00:56 |
I'm excited and I hope you are too.
So lets get started learning PHP.
| | 01:00 |
| | 59:59 |
(MUSIC).
| | Collapse this transcript |
| Using the exercise files| 00:00 |
If you are a premium member of the
lynda.com training library, you will have
| | 00:03 |
access to the exercise files that are
used throughout this title.
| | 00:06 |
If you are a monthly or annual subscriber
to lynda.com, you won't have the exercise
| | 00:10 |
files that accompany this tutorial, but
you can follow along with me.
| | 00:14 |
Everything that's in the exercise files,
we will create during the tutorials.
| | 00:18 |
So, as long as you continue to work along
with me, your files will exactly mirror
| | 00:22 |
what's in the exercise files.
Remember, that you can pause the video or
| | 00:25 |
rewind if you need more time to copy
something down.
| | 00:29 |
The exercise files for this title are
arranged by chapter and by movie.
| | 00:32 |
And you can find the exercise files that
correspond to the movie that you're
| | 00:36 |
watching, by first looking for the
chapter number, and then the movie number.
| | 00:40 |
In order to make use of the exercise
files, you'll want to first make sure
| | 00:42 |
that you install and configure the
necessary software, as shown in chapters
| | 00:46 |
2 and 3.
That is PHP, MySQL, and the Apache web server.
| | 00:49 |
Once you're confident, that you have
everything installed and configured.
| | 00:53 |
Then you will want to copy the contents
of the exercise files folder into your
| | 00:57 |
web document root directory.
If you don't know where that is, I show
| | 01:00 |
you how to find your document root in the
installation chapters.
| | 01:03 |
On my Mac, that's going to be inside my
User directory, inside the Sites
| | 01:09 |
directory, which we also create in the
installation chapter.
| | 01:12 |
It's a good idea to make a copy of the
exercise files, so that you still have
| | 01:16 |
the original to refer back to if you make
changes.
| | 01:18 |
On my Mac, I will Option drag the folder
Sandbox over into my Sites directory.
| | 01:25 |
Now, your files will match mine at the
start of the movie.
| | 01:28 |
Beginning in chapter 13, we'll be
incorporating a database into our
| | 01:31 |
project, and for the exercise files to
work, your database needs to match what
| | 01:35 |
the files expect.
In the exercise files for these chapters,
| | 01:38 |
you'll find a database file that you can
load into MySQL, and put your database
| | 01:43 |
into the same state as mine.
If you don't already have a database to
| | 01:46 |
load the file into, you'll want to review
the beginning of chapter 13, where we
| | 01:49 |
first create our database and set up the
necessary permissions.
| | 01:53 |
You can load that file directly into a
MySQL database, either by using a tool
| | 01:57 |
such as PHPMyAdmin, or by going to a
command line application, such as our
| | 02:02 |
Terminal, and typing in mysql-u followed
by a user name that has access to the database.
| | 02:09 |
We use widget_cms in our examples, dash p
that key that would be entered in the
| | 02:14 |
password followed by the database name,
widget corp.
| | 02:18 |
Then a less than sign followed by the
path to get to the SQL file that you want
| | 02:24 |
to load.
You can either type that in by hand or on
| | 02:27 |
the Mac, you can actually drag the file
into the terminal and it will output the
| | 02:31 |
full path for you.
Once you've located the file that you
| | 02:34 |
want to load into the database, simply
hit Return.
| | 02:36 |
It will prompt you to enter the password
that the username requires to access the database.
| | 02:43 |
And then it will update the database with
the instructions in that SQL file.
| | 02:47 |
Note, that the import will remove all old
database data at the same time as it
| | 02:51 |
imports the new data.
Importing can also be useful if you do
| | 02:55 |
experimenting on your own, but then want
to get your data in sync with mine.
| | 02:59 |
Once you have the same files and the same
database, you'll be able to work right
| | 03:02 |
along with me.
Don't forget that you can also use the
| | 03:05 |
exercise files to check your work as you
go along.
| | 03:07 |
| | Collapse this transcript |
|
|
1. PHP OverviewWhat is PHP?| 00:00 |
We're going to start out by talking about
what is PHP?
| | 00:03 |
PHP is a server side scripting language.
Now you may have thought that PHP was a
| | 00:08 |
programming language.
Well, technically speaking, it's not.
| | 00:11 |
So how's a scripting language different
from a programming language?
| | 00:14 |
The distinction between them is largely
artificial, and the lines can get a bit blurry.
| | 00:18 |
But we can do a general comparison.
A script only runs in response to an event.
| | 00:24 |
It also usually runs a set of
instructions by working down the page
| | 00:27 |
from the start to the end.
It has little or no user interaction
| | 00:31 |
after that initial event.
So PHP script does not run until a web
| | 00:35 |
page is requested.
Then it launches, follows its
| | 00:38 |
instructions from top to bottom, and then
quits until another action launches the
| | 00:41 |
script again.
On the other hand, a program, runs even
| | 00:45 |
when not responding to events.
It continues to run, and to wait for interaction.
| | 00:49 |
Whether that interaction comes from a
user, making choices, or from other
| | 00:52 |
programs or input.
A programs also jumps around with
| | 00:55 |
instructions a lot more.
So that there's often not a clear start
| | 00:58 |
and end point.
And it often involves lots of user interaction.
| | 01:02 |
Photoshop is a good example of an
application.
| | 01:05 |
After you launch it, it keeps running,
waiting for more interactions or for you
| | 01:08 |
to tell it to quit.
The task that it performs are not a
| | 01:11 |
linear set of instructions.
It jumps around based on the task that
| | 01:14 |
you want to do at that particular moment.
But as I said, the lines get blurry as
| | 01:18 |
scripts get more complex, they start to
resemble programs.
| | 01:21 |
And the simplest programs are basically
just scripts.
| | 01:24 |
So you could say it's a distinction
without a difference.
| | 01:26 |
But we still call PHP a scripting
language.
| | 01:30 |
Now what does it mean when we say PHP is
server side.
| | 01:32 |
When we talk about server side and its
opposite, client side.
| | 01:36 |
What we're talking about is where the
code does its work.
| | 01:40 |
Does the code run on our web server,
which is server-side, or on the user's
| | 01:43 |
computer, which is client-side?
The client-side, when we're working with
| | 01:47 |
web pages, is the user's browser.
As a contrast, JavaScript is an example
| | 01:52 |
of another popular scripting language,
but JavaScript is a client-side scripting language.
| | 01:56 |
JavaScript code is sent to the user's
browser and then it does its work there.
| | 02:00 |
PHP code is never sent to the user.
It runs entirely on the web server, and
| | 02:06 |
the results of that code is what's sent
to the user's browser.
| | 02:09 |
That's an important difference.
Because PHP runs on a web server, that
| | 02:13 |
means it generally can't run on its own.
We'll need to have a running web server
| | 02:16 |
in order to use PHP.
PHP code does not need to be compiled.
| | 02:20 |
It's executed by the web server exactly
as it's written.
| | 02:24 |
Other programming languages, such as C or
Java, require the code to be compiled or
| | 02:28 |
translated into another form before it
can be used.
| | 02:31 |
We'll be able to just write our PHP, put
it where our web server can find it, and
| | 02:35 |
then we can load up the web page and see
the results.
| | 02:37 |
PHP is designed for use with HTML.
It can be embedded in our HTML.
| | 02:42 |
And we can use it to generate HTML.
In the end PHP is going to return HTML to
| | 02:47 |
the browser.
PHP code is going to be our input and web
| | 02:50 |
pages are going to be our output.
Now if you've been working with HTML
| | 02:54 |
you're already familiar with having .htm
or .html at the end of your file names.
| | 02:58 |
PHP is going to work exactly the same but
we're going to put php at the end.
| | 03:02 |
The php is going to tell the web server
that this file contains php codes that
| | 03:07 |
needs to be executed.
PHP is going to provide more flexibility
| | 03:11 |
than html does on it own.
HTML pages are static by their nature.
| | 03:15 |
So all visitors to a web page see that
same page all the time.
| | 03:19 |
A PHP lets us create dynamic pages.
And page content can change based on conditions.
| | 03:26 |
Such as interactions with the user, or
data stored in a database.
| | 03:29 |
You can think of PHP as turbo charging
your html.
| | 03:32 |
PHP syntax is going to be very similar to
C, Java, and Perl.
| | 03:37 |
The small details are going to vary quite
a bit, but the structure of logical
| | 03:40 |
expressions and loops, those kinds of
things will be kind of familiar to anyone
| | 03:43 |
with programming experience in one of
these languages.
| | 03:45 |
Now that we know what PHP is, in the next
movie, we'll find out a little bit about
| | 03:50 |
the history of PHP.
| | 03:52 |
| | Collapse this transcript |
| The history of PHP| 00:00 |
To understand more about what PHP is now,
it's helpful to examine the history of PHP.
| | 00:04 |
Version 1 of PHP was written by Rasmus
Lerdorf in 1994, with a set of CGI
| | 00:10 |
binaries in the C programming language
that he wrote to replace a set of Perl
| | 00:14 |
scripts that he'd been using to maintain
his personal homepage.
| | 00:17 |
Version 2 was a more formalized version
that he put out in 1995.
| | 00:22 |
This version improved the original
version and also combined it with code
| | 00:25 |
that would interpret data submitted by
web forms.
| | 00:28 |
This was the first public release of PHP.
At the beginning, PHP is an abbreviation
| | 00:33 |
for Personal Home Page Tools, as in PHP
Tools.
| | 00:37 |
And that's because, that was its primary
purpose, was helping him maintain his
| | 00:41 |
personal homepage.
But in version 3, PHP changed dramatically.
| | 00:45 |
Two other developers got involved, Zeev
Suraski and Andi Gutmans.
| | 00:50 |
These two developers rewrote major parts
of PHP between 1997 and 1998.
| | 00:56 |
they also renamed it PHP hypertext
preprocessor.
| | 01:00 |
Now you can see that in those three words
we still have the initials P, H and P,
| | 01:04 |
but the first word in the acronymn is the
acronym itself.
| | 01:08 |
This is called a recursive acronymn so if
you ask what the first PHP in that stands
| | 01:12 |
for, it is PHP Hypertext Preprocessor.
This is what passes for programmer humor,
| | 01:17 |
so get used to it.
PHP version three is the first version
| | 01:20 |
that resembles the PHP that we still use
today.
| | 01:22 |
And it was this version Who is
responsible for making PHP incredibly
| | 01:26 |
popular in version 4, PHP changed
significantly once again.
| | 01:31 |
Sir Andi Gutmans had formed a new company
called Zend.
| | 01:34 |
And Zend rewrote the core of PHP.
This is a big update that made PHP very
| | 01:39 |
full featured.
Partly in response to the fact that it
| | 01:41 |
had become so popular.
Their goal was to make the code more
| | 01:44 |
modular and to allow it to work better
with other peoples code.
| | 01:48 |
They released it in May of 2000, and the
core code is referred to as the Zend Engine.
| | 01:53 |
Finally, we have version five of PHP,
which came out in July of 2004, which is
| | 01:58 |
based on another new and improved version
of the Zend Engine.
| | 02:01 |
PHP 5, includes a lot of new features,
such as better performance, improved
| | 02:05 |
object oriented programming and some
better database connectivity.
| | 02:09 |
Within each of those five major versions,
there are also mini minor versions.
| | 02:13 |
At the moment, PHP 5,4 is the most
current version.
| | 02:17 |
Most everything that we will discuss in
this course, will work with all versions,
| | 02:21 |
of PHP 4 and PHP 5.
Even future versions that haven't been
| | 02:25 |
released yet.
And that's because we are going to be
| | 02:27 |
focusing on the essentials of PHP and
those don't change very often.
| | 02:31 |
Most of what's being changed are either
the internal improvements that make PHP
| | 02:34 |
work faster or more efficiently.
Or high end improvements that are used by
| | 02:38 |
advanced users.
The essentials stay pretty much the same
| | 02:41 |
and they have stayed pretty much the same
since version 3.
| | 02:44 |
Before we get started by installing PHP,
let's examine why you should use PHP, and
| | 02:49 |
we'll do that in the next movie.
| | 02:50 |
| | Collapse this transcript |
| Why choose PHP?| 00:00 |
In this movie, we're going to examine why
you would want to use PHP over other web technologies.
| | 00:05 |
There are many reasons to use PHP.
This first is obviously that you've
| | 00:08 |
discovered the limits of HTML and now you
need something more.
| | 00:11 |
PHP is also open source and free
software.
| | 00:15 |
Open source means, the source codes
available for everyone to study, to use,
| | 00:18 |
or to modify.
Any developer can create an improvement
| | 00:21 |
to the way PHP works, and submit it for
consideration.
| | 00:23 |
And it's free software, which means that
it's free to download and it's free to use.
| | 00:28 |
There's nothing to purchase, no licensing
fees, no royalty fees.
| | 00:31 |
And I'm sure I don't have to tell you why
free is a good feature.
| | 00:33 |
PHP also cross platform to develop, to
deploy and to use.
| | 00:37 |
That means we can put PHP on a Windows
server, on a Mac server, or on a Linux
| | 00:41 |
server, and run the same PHP code with no
problems or any differences.
| | 00:45 |
And if you remember, we're going to send
the results of our PHP code to the user's
| | 00:49 |
browser's HTML.
So it doesn't matter what computer type
| | 00:52 |
they're using.
They don't need to have any kind of
| | 00:53 |
plugin or anything installed on their
browser to be able to view our website.
| | 00:57 |
By learning the PHP language, you're not
tying yourself to a specific piece of
| | 01:01 |
hardware or an operating system, or a
vendor.
| | 01:04 |
PHP is also powerful, robust, and
scalable.
| | 01:07 |
Now, this quality might not affect you so
much when you are developing, when you
| | 01:10 |
are finally deploy your finished website,
you are going to want to know that it's
| | 01:13 |
going to be able to handle the amount of
traffic that it gets.
| | 01:16 |
That it's not going to fail on you, just
going to be able to grow with you as your
| | 01:19 |
traffic grows.
The important point here, is that just
| | 01:22 |
because it's open-source and free.
Doesn't mean that it's inferior and that
| | 01:25 |
it's not as powerful and stable as
technologies that you pay for.
| | 01:29 |
Big sites like Facebook, Yahoo, and
Wikipedia use PHP.
| | 01:33 |
PHP is web development specific.
It was written from the very beginning
| | 01:37 |
with web development in mind.
If you'll remember, version one was
| | 01:40 |
written to help someone maintain their
personal website.
| | 01:43 |
So the functions in PHP are going to be
designed for web usage.
| | 01:46 |
And you don't have to learn a much
larger, more comprehensive language
| | 01:49 |
that's full of lots of features that you
won't need during web development.
| | 01:52 |
PHP is also fully object oriented since
version five.
| | 01:56 |
Object oriented programming won't come up
while we're learning the essentials.
| | 01:59 |
But it's nice to know that the language
that you're about to dedicate yourself to
| | 02:02 |
learning, has some advanced features that
you can take advantage of as your skills
| | 02:06 |
continue to progress.
PHP has great documentation in many languages.
| | 02:10 |
If you go to php.net, that's the main web
page for PHP, and you go to docs.php,
| | 02:16 |
you'll see some of this documentation.
Let's take a look now.
| | 02:19 |
So in my browser, I'm at
php.net/docs.php.
| | 02:23 |
If you just go to php.net and click this
link up here for documentation, it takes
| | 02:26 |
you the same place.
And you'll see here that I have
| | 02:28 |
documentation in a lot of different
languages.
| | 02:31 |
I'm going to go ahead and choose English.
You'll see that it comes up and it gives
| | 02:35 |
me a table of contents for all of the
different things.
| | 02:37 |
The basic syntax, variables, constants.
Everything I might want to know, I can
| | 02:41 |
look up here in the reference.
You'll also see that the top, there's a
| | 02:45 |
search bar where we can search for
things.
| | 02:47 |
So lets do a search for our first one
here.
| | 02:49 |
We're going to do Echo and then hit
Return.
| | 02:51 |
And you'll see that it's going to pop up
and give me a definition of Echo.
| | 02:55 |
Echo outputs one or more strings.
And it's going to go ahead and give me
| | 02:58 |
some examples, and some best practices to
follow.
| | 03:01 |
If you go all the way down here, you'll
see that there's user-contributed notes,
| | 03:05 |
and this is a great resource.
This is where the developer community can
| | 03:09 |
actually input feedback, provide helpful
hints, tips, tricks, even bits of code
| | 03:13 |
that they've written that might help you
out.
| | 03:14 |
So you definitely want to become familiar
with this resource and use it.
| | 03:18 |
And that brings us to the last reason why
PHP is a great language to learn.
| | 03:21 |
Which is that it has a large active
developer community.
| | 03:25 |
Over 20 million websites use PHP.
In terms of popularity, PHP is always
| | 03:29 |
among the top 10 of all programming and
scripting languages.
| | 03:32 |
And consistently, one of the top three
for web development.
| | 03:36 |
And that large active developer
community.
| | 03:38 |
Has also already created some great
examples of PHP applications that you may
| | 03:42 |
have already come across, like WordPress,
Droople, Joomla, Magento, Expression
| | 03:47 |
Engine, Media Wiki, and PHP BB.
These are all free tools that you can
| | 03:51 |
download, that can help you to create
websites like blogs or bulletin boards.
| | 03:55 |
By learning PHP, you're going to be able
to work with those tools more efficiently
| | 03:59 |
and even customize them for your specific
needs.
| | 04:01 |
Now, that we are comfortable with the
choice to choose PHP for our website,
| | 04:05 |
let's get an overview of how we get PHP
installed, so we can start developing
| | 04:08 |
with this powerful language.
| | 04:09 |
| | Collapse this transcript |
| Installation overview| 00:00 |
Hopefully, after looking at what PHP can
do for you, you're excited to get started.
| | 00:04 |
So let's examine what we're going to need
before we install.
| | 00:07 |
In order to install PHP on your computer,
so we can start learning it, you're
| | 00:09 |
going to need a few things.
We're going to need a web server, and
| | 00:12 |
that's going to be to serve up the web
files that we're going to look at in our browser.
| | 00:16 |
We're going to install that on our local
machine, not on some special piece of
| | 00:20 |
server hardware that exists somewhere
else.
| | 00:22 |
It's not problem for us to have the web
server exist on your local computer and
| | 00:26 |
also have the browser that's making
requests to that web server also exist on
| | 00:30 |
the same computer.
Typically you don't do that.
| | 00:32 |
Typically the web server exists on one
piece of hardware and a browser on a
| | 00:37 |
different computer is what's contacting
that web server.
| | 00:39 |
When we do local development they're both
going to exist on the same machine.
| | 00:43 |
We're going to need to have PHP
installed, so our web server will
| | 00:46 |
understand the PHP code that we create.
We're going to want to have a database
| | 00:50 |
installed, so that we can start creating
PHP applications that connect to that database.
| | 00:54 |
That'll give us the ability to create
more full featured applications, by being
| | 00:57 |
able to store data, and to pull data out
of the database, and display it
| | 01:01 |
dynamically to the user.
We'll also need a text editor to edit our
| | 01:04 |
PHP files.
That is to write our PHP code.
| | 01:07 |
And we'll need of course a web browser.
Any web browsers going to work, Firefox,
| | 01:11 |
Chrome, Safari,Internet Explorer, or
Opera.
| | 01:14 |
Remember, PHP just outputs HTML.
So anything that understands HTML is
| | 01:18 |
going to suit our purposes just fine.
Now for myself, I'm going to be using
| | 01:23 |
Apache for my web server.
PHP, I'll be using version 5.4.
| | 01:27 |
That's the latest version.
If you're using something later than
| | 01:30 |
that, don't worry about it.
As I said earlier, the essentials are
| | 01:34 |
going to be pretty much the same for
every version of PHP.
| | 01:38 |
And if there are changes, I'll make note
of those as we go along.
| | 01:41 |
The database I'm going to be using will
be the latest version of MySQL 5 and the
| | 01:44 |
text editor I'll be using is TextMate.
That's a Macintosh specific text editor.
| | 01:49 |
And the browser I'll be using is Firefox.
Now, your set up may be different than mine.
| | 01:53 |
You may be working on a Windows machine.
You may have a different text editor or
| | 01:57 |
different browser.
Don't worry about remember PHP is cross platform.
| | 02:00 |
The installation will be different but
the PHP lessons will be the same
| | 02:04 |
regardless which platform your working
on.
| | 02:06 |
And if you're windows I'm going to walk
you through a windows installation.
| | 02:11 |
So our installation plan is going to be
that we're to install all these things on
| | 02:14 |
our local machine.
We're going to develop there.
| | 02:16 |
We're going to run the web server on the
local machine.
| | 02:19 |
Have it interpret our PHP code and return
HTML to our local browser.
| | 02:23 |
Now, I just told you that you can use any
combination of platform, web server and database.
| | 02:28 |
But I want to go ahead and tell you about
the most common combinations.
| | 02:32 |
Those combinations we refer to as The
Stack.
| | 02:35 |
And the different stacks that are most
popular are referred to as LAMP, MAMP,
| | 02:39 |
WAMP, and XAMP.
For LAMP, it's LAMP, which stands for
| | 02:44 |
Linux, Apache, MySQL, and PHP.
And then for the M, it stands for
| | 02:49 |
Macintosh, the W in WAMP is for Windows,
and the X is for all three of those.
| | 02:54 |
It's sort of a wildcard that can be L, M,
or W.
| | 02:56 |
So those are the abbreviations.
This is the most popular set of
| | 03:00 |
combinations that there are.
There's more and more people now who are
| | 03:03 |
swapping in different things for these
different parts, but these are still the
| | 03:07 |
most popular.
Now you don't have to go with these, but
| | 03:09 |
it may make your life easier if you start
out with them because you may find more
| | 03:13 |
resources out there to help you if you
run into problems.
| | 03:17 |
Now if you do run into problems, or if
you have a different configuration from
| | 03:20 |
the start I want to point you to a
resource that could help, and that is the
| | 03:23 |
php.net website.
That's the main PHP website that we saw
| | 03:27 |
earlier, and php.net/manual/en, for
english, /install.php, is going to give
| | 03:30 |
you everything that you need to know to
get installed regardless of your platform.
| | 03:37 |
And that's going to be kept up to date.
It's always going to be there as a resource.
| | 03:42 |
And as we saw when we looked at Echo,
there's even going to be community feedback.
| | 03:46 |
Helpful tips and tricks that might help
you if you run into problems.
| | 03:48 |
Php.net's also going to be a great place
to always find out what the latest
| | 03:52 |
version of PHP is.
It's always right there on the home page.
| | 03:55 |
And if there are any particulars about
installation you didn't know about it, or
| | 03:58 |
something changes in the future.
Now, while you certainly could go to that
| | 04:00 |
URL and get it installed for yourself.
I'm also going to walk you through the
| | 04:04 |
steps on the Macintosh and Windows
platforms just to make sure everyone gets
| | 04:08 |
a good start, ready to begin learning PHP
together.
| | 04:11 |
So let's start doing that in the next
chapter.
| | 04:12 |
| | Collapse this transcript |
|
|
2. Mac InstallationOverview| 00:00 |
In this chapter, we're going to be
learning how to install PHP and MySQL on
| | 00:04 |
a Macintosh.
I want to start out by first giving you
| | 00:06 |
an overview of what we're going to do
before we go through each of the steps.
| | 00:10 |
The first thing we're going to need to do
is make sure that we have a web server.
| | 00:12 |
And that's going to serve up the web
files, process the PHP for us, and
| | 00:17 |
respond to requests from browsers.
So, we're going to have a browser, makes
| | 00:20 |
a request to the web server.
Apache web server is included with Mac OS 10.
| | 00:26 |
And it's been there for a number of
different versions.
| | 00:28 |
In Mac OS 10.8, we have version 2.2
included.
| | 00:31 |
So that's already there.
There are a couple of differences though
| | 00:34 |
from this version, from previous versions
of Mac OS 10.
| | 00:38 |
Starting with version 10.8, they removed
the web sharing option that you find in
| | 00:43 |
the system preferences.
So we're going to have to learn how to
| | 00:46 |
start and stop Apache without that.
So I'm going to show you how to do that.
| | 00:50 |
They also removed the sites directory.
There's normally a default directory
| | 00:54 |
called sites.
And that's where we would put our code,
| | 00:57 |
but they've taken that away now.
It's not there it wasn't deemed to be
| | 01:00 |
sort of an important feature for most
people, because most people aren't developing.
| | 01:04 |
So it's something that we'll have to
recreate there.
| | 01:06 |
And we'll have to do a little bit of
configuration on Apache to get it to do
| | 01:10 |
what we need it to do.
Then we're going to need to make sure
| | 01:12 |
that we have PHP installed, of course.
PHP also comes included with Mac OS 10.8.
| | 01:17 |
And that's version 5.3.
Now that's not the latest version of PHP.
| | 01:22 |
And that often happens, when they finally
ship a version of Mac OS 10.
| | 01:26 |
Sometimes it will be just a little bit
behind the latest PHP version.
| | 01:29 |
Now you could use 5.3 and save yourself
the trouble of upgrading.
| | 01:33 |
Just go right with 5.3 right from the
start, and almost every single thing
| | 01:38 |
we're going to learn will work perfectly
well.
| | 01:39 |
And I'll highlight the things that
wouldn't.
| | 01:41 |
However, I'm also going to show you how
to upgrade to the newest version, to make
| | 01:46 |
sure that we have the newest version as
well.
| | 01:48 |
And then we're going to need to do a
little bit of configuration to PHP's option.
| | 01:52 |
After we do that, we're going to make
sure that we have MySQL, that's going to
| | 01:55 |
be our database that we're going to use.
So we want to make sure that's installed.
| | 01:58 |
So we'll download and install it from the
MySQL website.
| | 02:00 |
It's not included with Mac OS 10.
And we'll need to do a little bit a
| | 02:04 |
configuration to it.
And we're going to need to set a root
| | 02:07 |
password, so that every time we access
the database, we're going to have a
| | 02:11 |
password to authenticate ourselves.
To say, yes, I'm allowed to get this data
| | 02:14 |
out of the database.
So we'll learn how to set a root password
| | 02:17 |
for that.
And then last of all, we're going to need
| | 02:19 |
to have a text editor.
So we're going to download and install TextMate.
| | 02:23 |
Now, you can install a number a of
different text editors and we are going
| | 02:25 |
to talk about what some of the options
are, but TextMate is the one I'm going to
| | 02:28 |
show you to use.
Okay, so let's get started by seeing how
| | 02:30 |
we set up the Apache web server.
| | 02:32 |
| | Collapse this transcript |
| Working with Apache Web Server| 00:00 |
In this movie were going to learn to work
with the Apache Web Server.
| | 00:02 |
You'll remember in the overview movie,
that I told you that the Apache Web
| | 00:06 |
Server is already installed on Mac OS
10.8.
| | 00:09 |
I also want to mention that there is an
alternative that you could use instead of
| | 00:12 |
the Apache Web Server.
Starting with PHP version 5.4, there is
| | 00:16 |
an included Web Server.
It's a very simple, lightweight web
| | 00:19 |
server, easy to start up, easy to stop,
and not very robust.
| | 00:23 |
I want us to go ahead and learn to use
Apache, partly because it's the most
| | 00:27 |
popular option out there.
But also because when you finally deploy
| | 00:30 |
your application, you're likely to want
to use Apache for serving your actual
| | 00:34 |
application to the general public.
So let's go ahead an learn how to use it now.
| | 00:38 |
You always will have this PHP simple
development one there, if you decide you
| | 00:42 |
want to switch over an start using that.
But I don't want to use that as a crutch,
| | 00:45 |
an then not know how to use Apache.
So let's work with Apache first.
| | 00:50 |
Now, even though we already have Apache
installed, it doesn't mean that it's
| | 00:52 |
simple to get to.
It's not something that sits inside your
| | 00:55 |
Applications folder.
It's a program that runs under Unix,
| | 00:58 |
which is under the hood of Mac OS X.
We used to be able to interact with it,
| | 01:03 |
from the system preferences, and from the
sharing preferences, menu.
| | 01:07 |
But starting in 10.8, that changed
because the option here that said Web
| | 01:11 |
Sharing was taken away by Apple.
It used to be that we could just go to
| | 01:15 |
Web Sharing and click on the check box
next to it and it would turn on Apache.
| | 01:19 |
Click it again and it would turn off
Apache.
| | 01:22 |
It was a way for us to have a graphical
user interface to interact with this
| | 01:25 |
program that's in Unix.
But they took that away, but that's okay.
| | 01:28 |
We don't actually need it.
We can do it by going directly to units.
| | 01:32 |
If you go to your Applications folder,
you'll see that you have a folder called Utilities.
| | 01:36 |
And inside there is a program called
Terminal.
| | 01:37 |
And this is the command line utility that
will allow us to interact with Mac OS X.
| | 01:42 |
I could just click on this and launch it,
but because we're going to be using
| | 01:45 |
Terminal a lot, I'm going to go ahead and
drag it down here to my Dock, and then
| | 01:49 |
launch it from there.
So here we are, we just have a Command
| | 01:52 |
Line that will let us type commands to be
sent to Unix for it to execute those commands.
| | 01:56 |
And it'll come back with responses to our
screen.
| | 01:58 |
If you've never worked with the Command
Line before.
| | 02:01 |
Don't be intimidated by it.
It's pretty easy once you get the hang of it.
| | 02:04 |
Now, yours likely looks different than
mine.
| | 02:05 |
I've just simply gone up to Terminal >
Preferences and customized some of my
| | 02:09 |
color and font settings.
You can pick different things for
| | 02:12 |
yourself as well.
I wanted something that was more easily
| | 02:14 |
readable by you.
All right.
| | 02:16 |
So let's now check out Apache.
The first thing you need to know about
| | 02:19 |
Apache is that it has a nick name.
It's nickname is HTTPD.
| | 02:25 |
You may recognize the HTTP from when you
write a URL in your browse.
| | 02:29 |
That stands for the Hyper Text Transfer
Protocol.
| | 02:31 |
The D at end stands for Daemon.
A Daemon is a little program that sits
| | 02:36 |
waiting to respond to requests.
So what is Apache?
| | 02:40 |
Well, it's a Hypertext Protocol Daemon.
It's sitting there waiting to respond to
| | 02:43 |
web requests.
So this is its nickname.
| | 02:45 |
So, httpd -v, will tell us the version
that we're running.
| | 02:51 |
So, this lets us know that we have Apache
installed and that it's version 2.2.
| | 02:55 |
In my case, it's .22.
Now, if you have something different than
| | 02:58 |
that, Apache 2.3 or something later,
don't worry.
| | 03:01 |
It really doesn't matter what version of
Apache you have.
| | 03:03 |
Its role in this whole process is pretty
simple.
| | 03:06 |
It's going to take our PHP, process it,
spit it out as HTML.
| | 03:11 |
Any features or enhancements that come in
later versions of Apache aren't going to
| | 03:14 |
affect us.
Now, incidentally, if we had said Apache
| | 03:17 |
-v, it comes up and says, I don't know
what that command is.
| | 03:20 |
In this case, It is called by it's
nickname.
| | 03:23 |
So sometimes you'll see it one way,
sometimes you'll see it the other.
| | 03:26 |
Is Apache actually running?
There are two ways that you can tell this.
| | 03:30 |
One is by doing a special Unix command
that you don't need to know all the ins
| | 03:32 |
and outs of.
But ps space aux and then pipe that
| | 03:37 |
through with that upright bar.
And then after that, grep httpd.
| | 03:41 |
What we're doing is we're saying spit out
a list of all of the processes running on
| | 03:45 |
this machine.
But send them to the grep search command
| | 03:49 |
and tell it to look for lines that have
httpd in them.
| | 03:53 |
When I hit Return, it comes back and says
oh I see one process running, and that is
| | 03:57 |
the grep command that you just entered,
the search command.
| | 04:00 |
So it's not actually running.
The only thing that's there was my search
| | 04:03 |
for it.
If it was running we would have expect to
| | 04:06 |
find at least one other instance being
returned to us.
| | 04:09 |
Something showing the actual process, the
Daemon.
| | 04:12 |
That's running and as a process waiting
to respond to web requests.
| | 04:15 |
The other way we could do it is to launch
a browser.
| | 04:17 |
And from the browser, we can ask for
http:// and then local host.
| | 04:23 |
That's a special name that means go to my
local IP address and tell me what you find.
| | 04:28 |
And it comes up and says, I'm sorry, I
couldn't find anything.
| | 04:32 |
If Apache had been running, we would have
gotten a result.
| | 04:35 |
So, let's go back and let's see how do we
make Apache run.
| | 04:38 |
To do that, we're going to need to have
another command.
| | 04:40 |
There are three commands I want us to
learn and they're all very similar.
| | 04:43 |
What they do is start, stop and restart
Apache.
| | 04:46 |
And we do that with Sudo Apachectl,
that's for Apache control, so we're
| | 04:52 |
controlling Apache, Apache control start,
and Apache control stop, and then Apache
| | 04:57 |
control restart.
The sudo at the beginning of each of
| | 04:59 |
those is a Unix thing.
It's saying, Unix, with my highest level
| | 05:03 |
of access privileges, I want to execute
this command.
| | 05:06 |
So that's what it's asking for.
And once we issue any of these commands,
| | 05:10 |
it's going to pop up and say you know
what, I need to have your password.
| | 05:14 |
And that password will be the same
password that you use whenever you log
| | 05:17 |
into your Macintosh or whenever you're
installing new software.
| | 05:20 |
And it comes up and says hey, I need your
highest level of access privileges to do this.
| | 05:24 |
That's the password it's talking about.
Let's try it now.
| | 05:27 |
Okay, so here we are in the command line
again.
| | 05:29 |
So it's sudo apachectl and then start.
So it'll come up and it will ask me for
| | 05:36 |
my password.
Now, yours may have come up and asked you
| | 05:38 |
with a big scary message saying, hey, be
really careful about using sudo.
| | 05:42 |
If you're not sure what you're doing stop
now.
| | 05:44 |
Ignore that message.
It's just a simple cautionary message
| | 05:47 |
that runs the first time that you run
sudo.
| | 05:50 |
After that, you can just type your
password, hit Return, and it comes up
| | 05:54 |
with no response.
Now that also is a change, unfortunately,
| | 05:57 |
for the worst.
It used to be that it would come up and
| | 05:59 |
say, Apache has now started, but they
took that away, it doesn't come back and
| | 06:03 |
tell us anything.
So, how do we know it's started?
| | 06:05 |
Well, we can do two things, we can issue
the same command for the command line.
| | 06:09 |
I can use my up arrow to go back up there
and just save myself some typing and then
| | 06:14 |
hit Return.
Now, you see it came back with some more stuff.
| | 06:18 |
It shows me these processes that are
running.
| | 06:20 |
You don't have to worry about what they
are or what all that means, you can see
| | 06:23 |
that the processes exist.
We also can go to our Firefox and if we
| | 06:28 |
just hit reload on the page, we come back
and we get something that says it works.
| | 06:33 |
Now, we know that Apache is working.
That's a default page that Apple has.
| | 06:37 |
It works with the exclamation point.
If you see that, Apache is running and is
| | 06:40 |
accessible via local host.
Now on your own you can try stopping it,
| | 06:45 |
starting it restarting it, issue all
three of those commands.
| | 06:48 |
I'm not going to go ahead and that
though, I think you can do those on your own.
| | 06:51 |
We do need to do a little bit more
configuration of Apache though.
| | 06:53 |
And we'll do that in the next movie.
| | 06:55 |
| | Collapse this transcript |
| Changing the document root| 00:00 |
In this movie we are going to learn to
change the Document Root that Apache is
| | 00:04 |
going to use.
The document root is the place where we
| | 00:07 |
are going to locate the web files that
Apache is going to be serving up when we
| | 00:11 |
make web requests.
Apache has a default document root and
| | 00:16 |
that exists at
Library/WebServer/Documents.
| | 00:19 |
So that's the root of your hard drive
inside each of those directories, and
| | 00:23 |
that's the place where we could drop
documents, right now.
| | 00:26 |
And we would be able to access them from
our browser.
| | 00:28 |
That's where that it works file lives.
We'll see that in just a moment.
| | 00:31 |
But instead of doing that, we want to use
our user document root.
| | 00:35 |
The first one is just a general location
that all users of our Macintosh can see.
| | 00:39 |
Now, even if we're the only user, It's
still a good practice to go ahead and
| | 00:43 |
locate our websites in our user
directory.
| | 00:46 |
So we're going to need to create a folder
for that, and that's going to be the
| | 00:50 |
Sites directory.
The unit shortcut for that is just the
| | 00:53 |
tilda sign that says go to my user
directory inside Sites.
| | 00:57 |
The full path for it is Users and then
your name and then Sites.
| | 01:01 |
And that's where this folder is going to
live.
| | 01:03 |
This folder does not exist, righ now, in
all likelihood, Apple used to provide it
| | 01:08 |
for us.
It used to be on all Mac OS 10 installation.
| | 01:11 |
But, starting with 10.8, they stopped
adding it.
| | 01:14 |
So, if you've upgraded from previous
versions of Mac OS 10, you may still have it.
| | 01:18 |
It may be a folder that still existed and
carried through the upgrade.
| | 01:22 |
If you've just installed 10.8 from
scratch, then you probably don't have
| | 01:26 |
this folder, and you'll need to create
it.
| | 01:28 |
And then, after we've created the folder,
we'll have to tell Apache that it has
| | 01:31 |
permission to serve up files from that
folder.
| | 01:34 |
So we'll have to give it a little bit of
a configuration in order to make that happen.
| | 01:38 |
Let's take a look now, so the first thing
I want to do is I want you to see the
| | 01:41 |
existing page that's there.
If we move around in Unix, and we can do
| | 01:45 |
that with the CD command for changing
directories.
| | 01:48 |
We change directory to Library, and then
Web Server, and then Documents.
| | 01:55 |
You don't need to do this, you can
actually just watch me.
| | 01:57 |
I've now moved into that directory.
If I type ls, it'll show me a listing
| | 02:01 |
what's in that directory and there's an
index.html.en page.
| | 02:02 |
If we take a look at that page, cat
index.html.en and that will output the
| | 02:08 |
contents of that file.
So you can see it's just the simple html
| | 02:14 |
page that says it works, that's what's
there by default.
| | 02:18 |
That's what's in that directory, we can
go ahead and keep using that directory
| | 02:20 |
and local host always points to that
directory.
| | 02:23 |
But instead, to get to our user
directory, what we want to do, go to the
| | 02:28 |
desktop here, I'm inside the Finder, I'll
pick New Finder Window.
| | 02:32 |
I'm inside my user directory.
And I'm just going to create a new folder
| | 02:36 |
inside here and call it Sites, capital S
and plural, so Sites.
| | 02:42 |
Notice that it picked up this special
icon that's built into Mac OS 10, because
| | 02:45 |
sites used to be a normal thing.
It used to be there all the time.
| | 02:48 |
So it still picked up that icon, even
though Apple has decided to leave it out
| | 02:51 |
of the install by default.
So this is now where I'm going to put all
| | 02:55 |
of my websites.
So from now on, all of my code is going
| | 02:58 |
to go inside this sites directory inside
here.
| | 03:02 |
In order to make that happen I need to
give Apache some additional commands.
| | 03:05 |
So in order to do that I'm going to cd
again but this time I'm going to go into
| | 03:09 |
Apache's configuration directory.
And that's inside /etc/apache2 So there
| | 03:16 |
it is, insideetc/apache2 and then inside
there, there's another directory called Users.
| | 03:22 |
So that's where I'm going to go to and
you should navigate there as well So you
| | 03:26 |
want to be in the same location.
if you type PWD It should report back
| | 03:30 |
that that is your current working
directory.
| | 03:32 |
That's where you are right now.
If we do ls in that directory, there's
| | 03:36 |
nothing there listed for me.
Now if it came back for you and it said
| | 03:38 |
that you had a configuration file, you
might want to take a look at that
| | 03:42 |
configuration file and see what's there
first.
| | 03:44 |
But since I don't have anything, I know I
need to create this file.
| | 03:47 |
I'll do that using the sudo command,
which we talked about earlier.
| | 03:50 |
Basically saying creative it with highest
level access privileges.
| | 03:53 |
When you use the nano text editor.
Just type the word nano and that will
| | 03:57 |
open up a text editor to allow me to edit
and then whatever your user name is, .conf.
| | 04:02 |
Now don't actually type user name, type
your user name.
| | 04:05 |
It's gotta match whatever your directory
is that you just created that folder in.
| | 04:09 |
So for me that's kevinskoglund.conf.
And then it's going to ask me for my password.
| | 04:14 |
I'll type in my main installation
password on the Mac.
| | 04:18 |
And now, it's says, alright, now what do
you want to put into this file?
| | 04:21 |
And I've got some text that I can just
Paste in here.
| | 04:24 |
And the only thing I'll need to do is
change username in this to be my
| | 04:29 |
username, kevinskoglund.
Alright, so that's what you want to put
| | 04:34 |
in this file, this is a set of directions
for Apache, saying that the directory
| | 04:37 |
users Kevin Skoglund Sites.
Should have these following permissions
| | 04:41 |
applied to it.
And these permissions are some basic,
| | 04:43 |
standard stuff Apache's saying Hey,
Apache, allow access to the directory.
| | 04:48 |
You should serve up things from this
directory, it's okay.
| | 04:50 |
So pause the movie if you need to Copy
that down.
| | 04:52 |
Make sure that you copy it exactly right.
Make sure that you got all the
| | 04:56 |
capitalization right, all the spacing
right, because it's all important.
| | 04:59 |
These are directions that we're giving to
Apache.
| | 05:02 |
And then when you're done, we're going to
hold Ctrl+X.
| | 05:05 |
You'll see that down at the bottom, it
says Exit.
| | 05:07 |
It's going to say, do you want to save
the changes here?
| | 05:10 |
And we're going to say Yes, we do.
Just have a Y.
| | 05:12 |
It'll say do you want to call it the file
name kevinskoglund.conf, and I do, so I'm
| | 05:16 |
just going to hit Return.
And now I had to Save that file, cat
| | 05:20 |
kevinskoglund.conf, and you can see the
file and see its contents there.
| | 05:25 |
So now we've given Apache the
instructions.
| | 05:27 |
There are two more things we need to do.
We need to change the permissions on this
| | 05:31 |
file to make sure that Apache has
permission to read it.
| | 05:35 |
So we're going to do that with sudo, and
then chmod, space, 644, and then the name
| | 05:39 |
of the file.
Once you do that, it will have changed
| | 05:46 |
the permissions, the ability for Apache
to read and write the file, to be the
| | 05:49 |
correct thing we need.
And the last thing is sudo apachectl
| | 05:54 |
restart, right, remember that.
We just need to restart Apache so that it
| | 05:57 |
picks up that new configuration file.
So now it should be working and we're
| | 06:01 |
ready to check it out in our browser but
we don't have anything in that folder to
| | 06:04 |
actually serve up yet.
So let's just real quickly create
| | 06:07 |
something in that folder.
I'm going to cd into my user directory.
| | 06:11 |
Remember the shortcut for that is that
tilde sign.
| | 06:14 |
Followed by a slash and then in that
folder is sights.
| | 06:19 |
So going into my sights directory now
from the command line.
| | 06:22 |
That's the same place, if I type open the
dot you'll that it pops open the sites directory.
| | 06:28 |
This is unix commands that I'm entering
to allow you to see the difference.
| | 06:32 |
That's the directory I'm in.
And I'm going to just do nano again.
| | 06:35 |
And let's create index.html.
We'll just type hello from my user directory.
| | 06:43 |
We'll do an exit again, Ctrl+X, followed
by Yes to Save my changes.
| | 06:48 |
Return to accept index.html.
You can now see the file appeared here.
| | 06:53 |
And let's go to Firefox.
If we hit Return here, we still get the
| | 06:58 |
It works page.
But if we now append to it, tilde and
| | 07:04 |
then your name.
So tilde, kevinscoglin, is now going to
| | 07:07 |
go to my user directory and return what's
in that directory.
| | 07:11 |
We've basically told Apache, in addition
to your main default document root, also
| | 07:16 |
serve up this user root file.
And now we're ready to be able to start
| | 07:20 |
putting our PHP files into this
directory.
| | 07:23 |
| | Collapse this transcript |
| Enabling PHP| 00:00 |
In this movie, we're going to learn how
to enable PHP on our Macintosh.
| | 00:03 |
If you remember the overview, I told you
that OS 10.8 ships with PHP 5.3 already
| | 00:09 |
built in.
Now it's already there, but it's not
| | 00:12 |
enabled, it's not turned on, meaning that
Apache isn't ready to serve up PHP files.
| | 00:17 |
We're going to have to make a change to
make that happen.
| | 00:19 |
In addition, 5.3 isn't the newest version
of PHP.
| | 00:23 |
So we're also going to want to upgrade to
a newer version, and I think it's
| | 00:27 |
instructive for you to see how to do that
process.
| | 00:30 |
But we're going to start out by just
getting version 5.3 up and running by
| | 00:34 |
enabling it.
Let's see how.
| | 00:36 |
So here I am in my terminal program, and
I'm just going to start by typing php -v.
| | 00:40 |
And that's going to return the version of
PHP that I have installed.
| | 00:43 |
You can see that I have PHP 5.315, yours
might be slightly different.
| | 00:48 |
Notice also that it tells me that it's
running Zend Engine v2.
| | 00:51 |
We talked about that in the history
section, that PHP now runs on a Zend Engine.
| | 00:56 |
So, PHP is installed and we have version
5.3.
| | 00:59 |
But it's not active yet.
We need to tell Apache, hey Apache, you
| | 01:03 |
need to be prepared for some PHP files,
be prepared to process those.
| | 01:07 |
And you do that by loading up one of your
modules that knows how to interpret PHP.
| | 01:12 |
That module's included.
It's just not turned on.
| | 01:15 |
So, in order to do that, we need to do
some configuration to Apache.
| | 01:18 |
We're going to start by moving to
Apache's configuration directory.
| | 01:22 |
That's going to be cd space slash etc
slash apache2.
| | 01:29 |
This is where apache's config files are
stored.
| | 01:31 |
If we type ls we will get a list of those
and you can httpd.conf that's the file
| | 01:36 |
that we want.
We know httdp is the nickname for Apache
| | 01:40 |
and conf for the config file.
So in order to edit that, I'm going to
| | 01:44 |
use sudo to get my maximum amount of Unix
privileges.
| | 01:48 |
And then nano for my simple text editor.
And then I'm going to edit that
| | 01:53 |
httpd.conf file.
It's going to ask me for my password,
| | 01:58 |
that's the password I'm using when I'm
installing software on my Mac.
| | 02:01 |
And here it is.
Now, I have the conf file now open, I can
| | 02:05 |
browse this and make all sorts of changes
and configurations to Apache.
| | 02:07 |
But I'm really only looking for one
change, which is PHP.
| | 02:10 |
So, instead of browsing through this long
document, says it's 500 lines long.
| | 02:14 |
I'm going to go straight to it, and you
can see that one of the options at the
| | 02:18 |
bottom is Where Is.
And the way to get that is Ctrl + W, and
| | 02:23 |
that will allow us to search for some
text.
| | 02:25 |
And what are we going to search for?
PHP.
| | 02:27 |
So PHP and hit Return.
It takes us straight there.
| | 02:31 |
So here it is, the PHP5 module.
This is the module that gives Apache the
| | 02:36 |
knowledge about how to interpret PHP
code.
| | 02:40 |
But it's not loaded right now.
And how do I know that?
| | 02:42 |
Because this pound or hash sign that's at
the beginning is telling it that this
| | 02:46 |
line is commented out, and not to be
loaded.
| | 02:48 |
So I need to take that out.
You won't be able to just use your mouse
| | 02:51 |
and click there.
We are on the Command line.
| | 02:53 |
We want to use your Arrow keys.
So move your cursor over to the L and
| | 02:57 |
just hit Delete to get rid of that.
Now it has been un-commented, the next
| | 03:01 |
time that Apache loads this config file
it will also load the module and to be
| | 03:05 |
able to read PHP code.
So let's hit Ctrl + X to exit, Y in order
| | 03:11 |
to save the changes, and then Return to
save them in the file name it suggests.
| | 03:15 |
Okay, so that's been added to Apache but
we need to also restart the server.
| | 03:21 |
We know how to do that.
apachectl restart.
| | 03:25 |
Now it restarted.
Now those changes have taken effect.
| | 03:28 |
PHP is loaded up, inside Apache, and it's
ready to process our PHP files.
| | 03:34 |
Now, don't just take my word for it,
let's actually try it out.
| | 03:37 |
Let's navigate to our sites directory,
that's inside our user root which is that
| | 03:41 |
little squiggle followed by the forward
slash and then our sites directory.
| | 03:46 |
By list you'll see that I have my
index.html file in there.
| | 03:50 |
I'm going to make a new one now using
nano.
| | 03:53 |
And we don't need sudo in front of nano
this time, we'll just making a simple
| | 03:56 |
text file, we don't need high level
privileges.
| | 03:58 |
And I'm going to call it my_phpinfo.php.
That's going to be the name of my file.
| | 04:05 |
It's going to open up here and I'm
going to write my very first PHP code here.
| | 04:09 |
So just follow along.
It's going to be a less than sign, a
| | 04:13 |
question mark php, a space phpinfo open
and close parenthesis, semicolon space,
| | 04:21 |
question mark and then greater than sign.
We'll talk about what all of these things
| | 04:26 |
mean, but the short version is that I've
got PHP tags on either side of a command
| | 04:31 |
telling it what to do.
So the beginning and ending bookends that
| | 04:34 |
have the question mark and the less than,
greater than signs, those are the PHP
| | 04:38 |
tags telling it, hey, PHP is inside here.
And then the command in the middle is the
| | 04:42 |
PHP info function that's being called.
So now we go to our browser, and we
| | 04:47 |
request Apache to load up this PHP page.
It's going to see that it ends in .php.
| | 04:51 |
It's going to see the PHP tags.
And it's going to process this command
| | 04:57 |
that's here in the middle.
And we'll see the results of what that
| | 04:59 |
function will output.
So let's do Ctrl + X to exit, Y to save
| | 05:04 |
our changes, and Return to keep that file
name.
| | 05:07 |
We can see that it is here in my site's
directory.
| | 05:11 |
Let's go to our browser, let's try it
out.
| | 05:14 |
Localhost, remember, we need to put our
username in front of this.
| | 05:18 |
And then my_phpinfo.php.
So there it is.
| | 05:25 |
It loaded it up, that PHP.
And it processed it.
| | 05:27 |
And the results to the output of HTML
decribing the entire configuration of PHP
| | 05:33 |
on this system.
It's extremely useful command.
| | 05:36 |
First of all, because it tells you all
the configuration, so you could review
| | 05:40 |
that and see how things are configured,
see if there are any problems.
| | 05:42 |
But also, just because it is the simplest
test that you can do to prove to yourself
| | 05:47 |
that PHP is working on this system.
If the phpinfo page works, then Apache is
| | 05:53 |
able to handle serving up PHP, the
problem's gotta be something else.
| | 05:57 |
So that's it.
We've now enabled PHP.
| | 05:59 |
And we could go ahead and we could start
working with this and you can go through
| | 06:02 |
this tutorial.
And almost everything that I'm going to
| | 06:04 |
show you in the tutorial is going to work
just fine with version 5.3 of PHP.
| | 06:10 |
However, I want us to see how we can
upgrade to a newer version.
| | 06:14 |
And we're going to do that next.
| | 06:15 |
| | Collapse this transcript |
| Upgrading PHP| 00:00 |
In the last movie we saw how to get php
turned on and working with Apache.
| | 00:03 |
And we saw how to use the PHP info
function in order to ouput our current
| | 00:07 |
configuration information for PHP.
We also see that we have version 5.3
| | 00:12 |
installed by default, we're going to
upgrade that now to the latest version of php.
| | 00:17 |
If we go to the PHP website you'll see
that the latest version at the moment.
| | 00:22 |
This is 5.5 alpha.
That's not a fully released version.
| | 00:25 |
It's an alpha version.
Over here it tells us the current stable
| | 00:28 |
version of PHP 5.4 is 5 4 11.
So that's what I want to install.
| | 00:34 |
Now the odds are very good that by the
time you do this installation, that will
| | 00:37 |
have changed.
It will be a later version.
| | 00:40 |
You want to use whatever the latest
version is.
| | 00:42 |
You don't need to use what I'm using.
Go ahead and install a later version.
| | 00:45 |
If 5.5 is out, go ahead and use 5.5 as
well.
| | 00:49 |
Now, if we look at the manual for PHP and
that can be found from the documentation area.
| | 00:54 |
If you look down, there installation
configuration is one of the options and
| | 00:58 |
installation on Mac OS 10 is an option.
This will give us the latest information
| | 01:03 |
about installing PHP on Mac OS 10.
Using packages is the one that we want to click.
| | 01:08 |
That's by far in a way going to be the
easiest and it gives us some suggestions
| | 01:12 |
on ways to do that.
If you're familiar with any of these, Mac
| | 01:15 |
Ports, Think, or Homebrew, you definitely
want to go with one of those options.
| | 01:19 |
It's going to be very simple.
So if you're familiar with those, you
| | 01:22 |
already have those working, those are a
great choice for keeping you PHP upgraded.
| | 01:26 |
If not though you are going to use
another package to install.
| | 01:31 |
They suggest something called Entropy but
Entropy is no longer supported or
| | 01:34 |
maintained but you can see is that in
this user contributed notes.
| | 01:39 |
Instead, it's being maintained at this
URL here.
| | 01:42 |
So I've already gone to that URL.
Here it is.
| | 01:44 |
It's php-osx.liip.ch.
This is where this PHP package is being maintained.
| | 01:52 |
This is considered a safe and reputable
source for installing PHP.
| | 01:56 |
You can actually browse the code that
they've got, that they're using here, if
| | 02:00 |
you want to make sure that it's safe.
But I think you can trust it.
| | 02:03 |
So if you look down, they give you some
simple one line installations that you
| | 02:07 |
can use.
This is very nice.
| | 02:09 |
If we want PHP 5.4.
Well guess what?
| | 02:12 |
We just have to copy this line right
here.
| | 02:14 |
And I can do that with Cmd + C.
And then we can go over here to our
| | 02:18 |
terminal, and just paste it in, and hit
return.
| | 02:22 |
And it will now install.
This version for us.
| | 02:25 |
So it's getting the package.
It wants our password to install because
| | 02:29 |
it's installing some software.
And then we'll start installing.
| | 02:32 |
So we'll just sit and wait while it
finishes doing its thing.
| | 02:37 |
So now that, that worked, we're ready to
check it out.
| | 02:39 |
Lets go back to our PHP info page.
And lets just hit reload.
| | 02:43 |
Let's reload the page, and you can see
now I'm running 5.4.11, so that's the
| | 02:48 |
version that it installed for me.
Now if you go back and you go to your
| | 02:52 |
command line, I just want to show you if
you do php -v, you actually still get
| | 02:57 |
version 5.3, it still tells you that
you're running 5.3.
| | 03:01 |
That's because both of them are still
installed and the web server is pointing
| | 03:06 |
at one of them While the command line is
looking at a different one.
| | 03:10 |
So don't let that throw you.
This is the authoritative one.
| | 03:12 |
This is the one that tells you what
Apache sees, and what Apache is using,
| | 03:15 |
and since we are not going to be running
PHP from the command line, we're only
| | 03:19 |
going to be running PHP through Apache,
that's what matters most.
| | 03:23 |
Now that we have it upgraded, we have PHP
5.4 installed, now we are ready to do
| | 03:27 |
some configuration of PHP.
| | 03:30 |
| | Collapse this transcript |
| Configuring PHP| 00:00 |
In this movie, we're going to learn how
to configure PHP.
| | 00:03 |
Specifically, there are three things that
we're going to be looking for.
| | 00:06 |
And that's to configure the error
reporting, the output buffering, and the
| | 00:10 |
default timezone.
Let's see how to do those.
| | 00:13 |
So in the last movie, we saw how to use
the phpinfo function in order to bring up
| | 00:17 |
the phpinfo information page.
This shows all of our configuration.
| | 00:22 |
It's a great place to look and see how
things are set at the moment.
| | 00:25 |
The most important part, though, are
these lines right here.
| | 00:28 |
Configuration file for php.ini.
That is where our configuration is going
| | 00:34 |
to be loaded.
Loaded configuration file.
| | 00:36 |
It's going to be inside
usr/local/php5/lib.
| | 00:38 |
And the file itself is called php.ini for
initialize.
| | 00:43 |
That's what the ini stands for.
That's the file.
| | 00:46 |
The php.ini file is going to hold all of
the configuration.
| | 00:49 |
It's all of the things that we see on
this page.
| | 00:52 |
There's one other important thing that I
want you to notice as well, which is the
| | 00:56 |
line below it, scan this directory for
additional ini files.
| | 01:01 |
Now, this is not always the case with all
PHP installations.
| | 01:04 |
But the one that we've downloaded, the
one that I used from Liip, well, that is
| | 01:09 |
going to use that.
It was created so that it will look in
| | 01:13 |
this php.d directory for additional ini
files.
| | 01:17 |
And that's going to be important.
We want to remember that.
| | 01:19 |
So we actually have configuration stored
in two places.
| | 01:22 |
And our main ini file and in these extra
files that are inside this other directory.
| | 01:27 |
So let's start by just copying this path
all the way to the ini file.
| | 01:32 |
Use Cmd+C see to do that.
Switch back to my command line and I'll
| | 01:36 |
use nano to edit that file.
So here it is.
| | 01:40 |
Here's my configuration.
You can scroll through it and you can see
| | 01:43 |
all the different things that you can
configure in PHP.
| | 01:46 |
But let's jump straight to the things
that we're looking for.
| | 01:49 |
I'm looking for display errors, first of
all.
| | 01:52 |
So I'm going to use Ctrl+W to search and
it'll ask me what I want to search for.
| | 01:56 |
Display_errors.
That comes up with first a quick
| | 02:02 |
reference and basic settings.
I don't want that so lets hit Ctrl+W again.
| | 02:07 |
I don't have to type it again.
The default is to search for display
| | 02:10 |
errors again.
I hit Return, and here it is.
| | 02:12 |
This is the real value.
How do I know it's the real value?
| | 02:15 |
Because it doesn't have a semicolon in
front of it.
| | 02:17 |
The semi colon's letting us know that
these lines are comments.
| | 02:21 |
Ones that don't have semicolons are the
real active lines, the actual
| | 02:24 |
configurations going on.
You can see that it actually gives me
| | 02:27 |
some helpful information.
It tells me what the default value is,
| | 02:30 |
the development value, production value
that you'd want to have.
| | 02:34 |
Now, for in development, we want to see
our errors.
| | 02:37 |
We want to know what went wrong so we can
fix it, we want as much information as possible.
| | 02:41 |
But when we actually deploy it to the
real world, we are not going to want to
| | 02:44 |
give all sorts of server information to
people so we are going to turn off the
| | 02:48 |
errors in that case.
But you want to make sure that right now,
| | 02:50 |
display errors is on.
The next one that I want us to check for
| | 02:55 |
is error reporting.
So, Ctrl+W again, its time to search for
| | 02:59 |
error reporting.
And let's do Ctrl+W again, a couple of
| | 03:05 |
times to get to this one.
Error reporting is set to E_ALL.
| | 03:09 |
This is a constant value.
And this constant value actually
| | 03:14 |
represents a number.
So if you look over here in the config
| | 03:17 |
file into the search for error reporting,
you will see error reporting right here
| | 03:23 |
is 32767.
That's what this value represents, right?
| | 03:28 |
So E_ALL gets translated into a number
when it gets stored.
| | 03:32 |
It's a lot easier for us to work with it,
though, being this constant, E_ALL.
| | 03:37 |
So this will report back all errors to
us.
| | 03:40 |
Now, before version 5.4 of PHP, if you
wanted all errors, you also had to put
| | 03:46 |
the upright pipe, followed by E_STRICT.
The strict errors were a separate
| | 03:52 |
category that were not included in all,
and that's counter intuitive.
| | 03:55 |
All should include everything, right?
So they did make that change, and strict
| | 03:59 |
got incorporated into all so it includes
that as well.
| | 04:02 |
If you don't want something in there,
like you don't want these strict ones.
| | 04:06 |
Well then, you use the & and then the
curly squiggle followed by E_STRICT.
| | 04:12 |
So that means all errors except for the
STRICT errors.
| | 04:15 |
You can do the same thing for all the
different kind of errors that there are.
| | 04:18 |
Notices, deprecations that's code that's
going to be going away soon.
| | 04:22 |
And you can see that they tell you, when
you're in production, it's good idea to
| | 04:26 |
not have it raise errors on depracated
code strict usage of things.
| | 04:31 |
But when we're in development, go ahead
and give us a warning about those kinds
| | 04:35 |
of usages.
So you want to make sure that you have
| | 04:37 |
E_ALL or that you have E_ALL followed by
E_STRICT if you happen to have an older
| | 04:45 |
version, that works as well.
Okay, so now we have that.
| | 04:47 |
The last one I want to check for on
errors is HTML errors.
| | 04:51 |
So we'll do our search again, this time
for html_errors, we want to make sure
| | 04:56 |
that this is turned on.
This says that when we do see errors,
| | 04:59 |
let's give them to us in HTML format.
Just looks a little nicer, and actually
| | 05:02 |
has some links that we can click on.
So now we've got our errors set to show
| | 05:05 |
us all errors.
Let's now take a look at output buffering.
| | 05:09 |
So Ctrl+W and let's look for
output_buffering.
| | 05:15 |
Here's the actual directive.
Now, this has a value of 4096, that's how
| | 05:20 |
much data it will buffer before it
outputs.
| | 05:22 |
We'll talk more about output buffering
later.
| | 05:24 |
For now, turn it off.
Now, when I'm developing, I often have it on.
| | 05:29 |
But I want us to have it off so that you
can see what happens when it's off.
| | 05:32 |
It's going to be very instructive to
understand this part of PHP.
| | 05:36 |
So let's turn it off then we'll talk
about what it does, and why we would want it.
| | 05:40 |
And then we can go and turn it back on,
and we'll be able to use output buffering
| | 05:44 |
as a feature because we'll be
knowledgeable about it at that point.
| | 05:47 |
So to start with, we're going to turn it
off.
| | 05:50 |
And then the last one we're going to do
is the timezone.
| | 05:53 |
So let's go Ctrl+W again, let's search
for timezone, and here's the one we want.
| | 05:59 |
It is commented out.
We're going to take away the semicolon at
| | 06:03 |
the beginning and then we're going to put
inside quotes, our timezone.
| | 06:07 |
What timezone do we want to be using?
Now, there's a very specific format that
| | 06:11 |
these timezones need to be in.
And you can actually just do a search for
| | 06:16 |
php timezones and you'll find it, or
php.net/manual/en/timezones.php.
| | 06:19 |
So this is a list of all the supported
timezones.
| | 06:24 |
I'm in America so I'm going to click that
one.
| | 06:26 |
And here you see a list of all the
American timezones.
| | 06:29 |
This is both North and South America...
You can see some American cities that
| | 06:33 |
might be close to you.
So there's Detroit and there's another
| | 06:36 |
one here for Los Angeles.
I'm going to be close to New York, that's
| | 06:40 |
my time zone, so I'm going to putting in
America/New_York.
| | 06:42 |
I'll just go ahead and copy that, Cmd+C.
I'll switch back over here and inside
| | 06:50 |
those quotes, then I'll paste that in.
So it's going to set my timezone to be America/New_York.
| | 06:56 |
Now, you could also set other
information, latitude, longitude, things
| | 06:58 |
like that.
But those aren't important to us at this point.
| | 07:01 |
We just need to make sure we have the
timezone set.
| | 07:02 |
And the reason why we want to take time
to do this is because PHP now includes
| | 07:07 |
some warnings that will pop up while
we're developing.
| | 07:09 |
It'll say hey, you're working with date.
And you don't have a default timezone set.
| | 07:14 |
That's not very advisable.
In order to avoid those warnings, we're
| | 07:17 |
going to go ahead and set a default
timezone for ourselves now.
| | 07:20 |
It's also a good practice to be in.
So let's save that.
| | 07:24 |
Save modified buffer?
We want to say yes, and then hit Return
| | 07:27 |
to keep it in that file name.
We need to restart Apache.
| | 07:33 |
For our changes to take effect.
It wants our password.
| | 07:36 |
That's our password we used to install
software on our Mac.
| | 07:38 |
And then let's switch back to our phpinfo
file.
| | 07:42 |
And let's go up to the very top and we'll
reload the page.
| | 07:46 |
So now let's look for output_buffering,
no value.
| | 07:51 |
So it turned off output_buffering for us.
Where before it said 4096 there.
| | 07:55 |
Let's take a look at time zone.
Now, wait a minute.
| | 07:59 |
This isn't the time zone that I put in.
And here's another one down here.
| | 08:02 |
This isn't the time zone I put in.
So what happened?
| | 08:05 |
Remember that i told you there are
additional config files.
| | 08:08 |
If you go look in this directory, user
local php 5, slash, php.d -- copy that
| | 08:15 |
Let's just go into that directory using
cd.
| | 08:17 |
Then if we take a look at what's in that
directory, you'll see that there are more
| | 08:21 |
ini files.
And the one that really matters is this
| | 08:25 |
one right here.
That's the ini file that contains a
| | 08:29 |
timezone setting that going to cause us
problems.
| | 08:31 |
So let's just edit that real quick.
Nano And we'll open up that file, and
| | 08:35 |
here it is.
You can see that it has set Europe Zurich
| | 08:39 |
as being the the time zone.
We can just comment that out.
| | 08:42 |
And our default one will take place.
I'll comment it out there, latitude and
| | 08:45 |
longitude as well.
Or, we could change it here.
| | 08:48 |
We could change that value to be the same
time zone that we had in the other place.
| | 08:51 |
All the other settings that they've got
here are fine, they're not offensive.
| | 08:55 |
Notice that they did set error reporting
here.
| | 08:58 |
If we wanted ours to take effect, then
you could go ahead and then take that out
| | 09:01 |
as well.
Alright, so once we've done that, let's
| | 09:04 |
do the Ctrl+X, save our modified buffer,
and we'll save that file once again.
| | 09:09 |
We need to restart Apache.
I'm going to use my up arrow a couple of
| | 09:12 |
times to get back to the command and hit
Return.
| | 09:15 |
Now, that Apache's restarted, we'll come
back here and reload our ini page, and
| | 09:19 |
let's search for time zone again.
And here it is.
| | 09:22 |
America/New York.
So it worked.
| | 09:24 |
It did set it to the correct value.
Now we have the basic configuration that
| | 09:27 |
we need to start developing with PHP.
And not only that, we have a little bit
| | 09:31 |
of an understanding of how we go about
configuring PHP.
| | 09:34 |
That we make changes to that php.ini
file, we restart Apache, and then we can
| | 09:39 |
come here and check those values to make
sure they're correct.
| | 09:42 |
There are other things in that file you
can change.
| | 09:44 |
There's maximum execution time, there
request to auto run, there's the maximum
| | 09:48 |
file size that will be uploaded.
There's where you want to store your
| | 09:51 |
error logs.
There's all sorts of things in there that
| | 09:54 |
you can go through and customize and make
PHP work exactly the way that you want.
| | 09:58 |
But this is enough to get you started, so
let's move on and get MySQL installed.
| | 10:02 |
| | Collapse this transcript |
| Installing MySQL| 00:00 |
In this movie, we're going to learn to
install MySQL.
| | 00:02 |
MySQL is a free and open source database
that we're going to be using to connect
| | 00:06 |
to, from PHP.
PHP will connect to the MySQL database
| | 00:10 |
and then either store data in the
database or retrieve data back from the
| | 00:13 |
database to work with in PHP.
Installing is pretty simple.
| | 00:17 |
We just go to the MySQL website, download
it, and then run the installer.
| | 00:20 |
The URL for downloading is going to be
http://dev.mysql.com/downloads/mysql.
| | 00:25 |
Let's go there now.
So, here I am at the MySQL download page.
| | 00:34 |
And the version that we're going to be
using is refered to as the community server.
| | 00:37 |
That's the free and open source version.
There are some of mySQL that have some
| | 00:42 |
additional features, some that are paid.
The one that we want is the community server.
| | 00:47 |
If you scroll down this page, you'll see
that this is the actual download area
| | 00:50 |
right here.
And the newest version, right now, is 5.6.10.
| | 00:55 |
By the time that you watch this tutorial,
that number very well may have changed.
| | 00:58 |
Don't worry about it, go ahead and get
whatever the latest version is.
| | 01:01 |
MySQL has not changed significantly over
many versions.
| | 01:04 |
The basic features are still the same,
and that's what we're going to be using,
| | 01:07 |
is just the basic features.
So, you can feel comfortable using
| | 01:10 |
version 5.7, 5.8, maybe 6.0.
don't worry about it, go ahead and get
| | 01:15 |
the latest version.
You can see also it's preselected Mac OSX
| | 01:18 |
as our platform of choice.
So, we're seeing only Mac OSX versions in
| | 01:22 |
this list.
But you'll notice that the OSX version
| | 01:25 |
that list right now, is 10.7.
That's the newest one.
| | 01:28 |
There are some 10.6 versions down here,
but there's nothing that says, 10.8.
| | 01:33 |
That's okay as well.
Go ahead and download the 10.7 version
| | 01:36 |
for working on 10.8.
It works just fine.
| | 01:39 |
There's also another choice we need to
make which is, what format do we want to
| | 01:41 |
download it in, either compressed TAR
archive, or DMG archive?
| | 01:46 |
This is a disk image, and this is the one
that has the clickable installer in it
| | 01:50 |
that we want.
So, we're going to want to have the DMG
| | 01:52 |
archive version.
And then we have one more choice to make.
| | 01:55 |
Do we want the 32 bit version, or do we
want the 64 bit version?
| | 02:00 |
Chances are, you want the 64 bit version.
All modern Macs use the 64 bit.
| | 02:05 |
Now if you have an older Mac, you may
want to actually check and look on
| | 02:08 |
Apple's site.
Apple offers some information about how
| | 02:11 |
you can determine whether you have a 32
bit or a 64 bit processor.
| | 02:14 |
But almost all Macs from the last couple
of years are going to be 64 bit.
| | 02:19 |
So, this is the one we want.
Mac OSX Version 10.7 with the Intel
| | 02:23 |
processor, x86, 64-bit and in the DMG
Archived format.
| | 02:25 |
So, once we have that, we can click
download.
| | 02:26 |
It'll come up and ask us to either login
or sign-up, but we don't have to do either.
| | 02:35 |
If you scroll down past that, you'll see
that there's a little link here that
| | 02:38 |
says, no thanks, just start my download.
That's the one that we want Firefox is
| | 02:42 |
going to ask me what it wants me to do
with this installer.
| | 02:45 |
We can just save it to our desktop.
For Firefox, I can say Open with Image
| | 02:48 |
Mounter, and that will do it for me.
You can also just save it to desktop, but
| | 02:51 |
then double-click on the resulting file.
So once it's downloaded, Firefox will
| | 02:55 |
open up this window for you.
Or you can double-click on the disk image
| | 02:58 |
to be able to see this window.
And there's three things in here.
| | 03:01 |
There's MySQL.
The MySQL Preference Pane, that's going
| | 03:04 |
to go in your system preferences.
And then, MySQL Startup Item, that's
| | 03:07 |
going to start up MySQL for you.
You're going to want all three of those,
| | 03:10 |
and you're going to do them in this
order.
| | 03:12 |
So, we're going to start with the one
over here on the far left, that's going
| | 03:15 |
to install MySQL itself.
Now by default, Apple has something set
| | 03:18 |
up on your Mac called Gatekeeper that
makes sure that malicious programs aren't run.
| | 03:23 |
And they've gone ahead and said, don't
let it run at all.
| | 03:25 |
Go ahead and refuse to let this program
run because it didn't come from the App
| | 03:29 |
Store or a list of approved developers.
We want to turn that off.
| | 03:33 |
There's a couple of ways to do that, you
can either use the Ctrl key and click on
| | 03:36 |
this and click Open.
Or, we can go ahead and go to system
| | 03:40 |
preferences and just turn off the
feature, which is what I like to do.
| | 03:43 |
You could turn it back on if you feel
uncomfortable with that.
| | 03:45 |
We're going to click the lock to make
changes, under Security and Privacy, and
| | 03:50 |
then we're going to need to put in our
password.
| | 03:53 |
And then, instead of just having Mac
store and identify developers, allow
| | 03:57 |
applications downloaded from anywhere.
Choose from anywhere, and then you can
| | 04:02 |
close that Control panel.
Now, we can launch it and we'll go past
| | 04:06 |
Gatekeeper and we'll be able to run the
installer.
| | 04:08 |
Click Continue, Continue, Continue, we'll
agree to the license and then it says,
| | 04:15 |
where do you want to install?
We'll install on my hard drive here, and
| | 04:19 |
it's going to want my password.
Okay, so now my software has been
| | 04:25 |
successfully installed.
We can click Close.
| | 04:27 |
We now have My SQL.
Before we can go and try that out though,
| | 04:30 |
let's run the Preference pane and install
that.
| | 04:34 |
We'll install it for this user only or
all user of this computer we'll go and
| | 04:37 |
say all users.
It's going to ask for my password again.
| | 04:42 |
And there it was.
Did you see it there?
| | 04:44 |
Let me just go back to Show All.
Here is the Preference pane, so this is
| | 04:47 |
now going to give me a way to start and
stop MySQL.
| | 04:51 |
For now, I'm going to just hold off on
that, we'll come back to that.
| | 04:53 |
I'm going to do and do the MySQL start up
item package, and let's run that one.
| | 05:00 |
Is a very small little program.
And there down that's installed.
| | 05:09 |
So now, let's go back and lets actually
start up MySQL.
| | 05:11 |
And I've got it set to automatically
start MySQL surround startup.
| | 05:14 |
That's an option for you, you don't have
to, but I'm going to go ahead and have it
| | 05:17 |
do that, so it's always running for me.
Doesn't take up a lot of processing
| | 05:20 |
power, so for most cases, you won't even
notice that it's running.
| | 05:23 |
And let's tell it to start up the server.
Says it's now running, so there it is.
| | 05:29 |
And if we want to stop it, we have that
Preference pane right there to stop it as well.
| | 05:33 |
Nice and easy.
So, now that we have MySQL installed, we
| | 05:35 |
can go ahead and close up these windows,
and we can throw this disk image in the trash.
| | 05:40 |
Before we start using MySQL, there are
two small configuration changes we need
| | 05:43 |
to make.
And we'll see how to do those in the next movie.
| | 05:45 |
| | Collapse this transcript |
| Configuring MySQL| 00:00 |
Now that we have MySQL installed, and
this week we're going to look at how we
| | 00:03 |
can configure MySQL.
Specifically, there's two changes I want
| | 00:06 |
us to make.
The first of those is I want us to add
| | 00:08 |
MySQL to our PATH.
That's going to allow us to access MySQL
| | 00:12 |
from the command line inside Unix.
We'll talk about what the PATH is and how
| | 00:16 |
to work with it in just a moment.
The second change that I want us to make
| | 00:19 |
is I want us to set a root password for
MySQL.
| | 00:22 |
By default, there is no password required
to log into MySQL as the root user, that
| | 00:28 |
is the user with the most access
privileges so its wide open by default.
| | 00:32 |
That's not a great security measure to
have.
| | 00:35 |
Even though we're just on our local
machine it's still a good idea to always
| | 00:38 |
go ahead and set a root password.
To make sure that your database is locked
| | 00:42 |
down and only someone with that password
has access to the data that's inside of them.
| | 00:45 |
So we're going to take care of those two
things, and we can do those from the
| | 00:48 |
command line.
So I'm going to open up terminal.
| | 00:51 |
And if you're in a new window, then you
should already be in your user directory.
| | 00:54 |
You'll just want to make sure, pwd will
tell you, and cd with that little tilde
| | 00:59 |
after it, will make sure that you're in
that user directory, that's where we all
| | 01:02 |
want to be.
So the first task is that we want to set
| | 01:05 |
up MySQL so that it's accessible from our
PATH.
| | 01:08 |
So now let's talk about what the path is
and why it's important that we make this change.
| | 01:12 |
The path is a list of directories uses in
order to locate programs on the machine
| | 01:18 |
that it can run.
So for example, we have PHP installed and
| | 01:21 |
I can type which php.
and it'll come back and tell me where
| | 01:24 |
it's located.
It's in the directory user/bin, that's
| | 01:29 |
where it is.
And user bin is in my PATH, it's one of
| | 01:32 |
the directories that Unix looks in to
find programs.
| | 01:35 |
But MySQL is not in one of those, which
mysql comes back if it didn't find it in
| | 01:40 |
any of the preset directories.
So we have to add the directory where
| | 01:44 |
MySQL resides to that list.
We can take a look at that list by just
| | 01:48 |
saying echo, dollar sign, all capital
PATH.
| | 01:52 |
This is a variable that contains a list
of the directories it's going to check in
| | 01:55 |
with colons in between each one of those.
So it's first going to look in user bin,
| | 01:59 |
bin and bin, bin and user s bin, then an
s bin and then user local bin.
| | 02:04 |
And that's the order of priority.
It's going to look in each of those
| | 02:06 |
directories until it finds the thing that
we're looking for.
| | 02:08 |
So that's how it was able to find php.
And it went through all of those and did
| | 02:12 |
not find MySQL, so we need to add our
location for that.
| | 02:17 |
Now, where is MySQL located?
Well the place that it installs it is
| | 02:21 |
going to be inside, I'll just type ls
here, /user/local/mysql/bin.
| | 02:29 |
That's the directory where it's located,
I'm going to do a listing of that
| | 02:32 |
directory, and here it is, MySQL.
There's also a lot of other little
| | 02:35 |
programs that installs along with it, but
that's the program that we're looking
| | 02:38 |
for, MySQL.
So what we need to do, is essentially add
| | 02:42 |
this, to this long list here.
The way we can do that is by editing one
| | 02:47 |
of our bash configuration files.
Bash is the environment that we're using
| | 02:51 |
in Unix.
Don't worry too much about that.
| | 02:53 |
But if we type ls dash la from our user
directory, you will see a list of the
| | 02:59 |
files thatb are used in the directory,
and you'll see that I have one here
| | 03:02 |
called .bash_profile.
You may or may not have this file.
| | 03:06 |
I have it because I've done some
configuration in here before.
| | 03:09 |
Don't worry about whether you have it or
not, this is the file that we want.
| | 03:13 |
If you have one that's called .bashrc,
that would work as an alternative.
| | 03:17 |
Bash profile it's a little simpler, if
you don't know the difference between the
| | 03:20 |
two go ahead and just use bash underscore
profile.
| | 03:23 |
If you have bashrc and know the
difference then you can use that.
| | 03:26 |
Now in order to edit that file, I am just
going to type nano.bash_profile that's
| | 03:30 |
going to work even if you don't have that
file.
| | 03:33 |
Okay, so the nano.bash_profile is
going to open it up in the nano text
| | 03:36 |
editor and you can see I have just got
some basic configuration in here.
| | 03:40 |
What we're going to want to do is add a
new line to it that's going to say, export.
| | 03:44 |
And then, PATH without the dollar sign,
equals, and then in double quotes I'm
| | 03:51 |
going to put dollar sign, PATH.
That's going to echo back the value of
| | 03:55 |
PATH that we just saw.
But before we echo it, we're also going
| | 03:58 |
to append to it, user/local/mysql/bin:.
You see what I did?
| | 04:06 |
So it's basically saying the path now is
going to be equal to this new directory,
| | 04:10 |
followed by whatever was previously set
by the operating system.
| | 04:14 |
I'm going to just add my new PATH to the
front of it, so that's it.
| | 04:17 |
That's all there is to it, and we want to
make sure that we export it, make sure
| | 04:20 |
that you spelled everything right, make
sure you've got your quotes right.
| | 04:22 |
And then Ctrl+X to exit, Y to Save
changes, and Return to accept the name.
| | 04:28 |
You can do cat .bash_profile, that's a
Unix command that will just output it for
| | 04:33 |
us, so that we can see what it looks
like.
| | 04:34 |
We see that it's there Now if we say echo
dollar sign PATH, you'll see that its not
| | 04:40 |
listed there.
That's because it's changes haven't taken effect.
| | 04:43 |
We can either close this terminal window
and reopen it and they would take effect
| | 04:47 |
because it reads that dash profile every
time it starts up.
| | 04:50 |
Or the other way we can do it is just to
say source .bash_profile, and that will
| | 04:57 |
read in that code.
Now we'll do echo path and you'll see
| | 05:01 |
that now it has upended the beginning.
So it's going to look in that place, we
| | 05:04 |
say which mysql, now it has located it,
now it knows where MySQL is.
| | 05:09 |
And that's great because we want to use
another program that's in that same
| | 05:12 |
folder to set the root password, and
that's going to be the MySQL admin program.
| | 05:17 |
If we say which mysqladmin, you can see
that it's located in that same spot.
| | 05:22 |
So this is the program that lets us set
passwords, among other things.
| | 05:26 |
Mysqladmin and the user we want to set it
for is our root user.
| | 05:31 |
Root is the name of the sort of most
powerful user on any Unix system, and
| | 05:35 |
it's true for MySQL as well.
So, the root user, and we want to set a password.
| | 05:40 |
Now, we could put the password in quotes
after it, but an even better practice is
| | 05:44 |
to just hit Return after this line, and
it'll come up and ask me for my password.
| | 05:49 |
Now, there was no password set
previously.
| | 05:51 |
I'm just setting a new password.
It can be anything you want.
| | 05:54 |
I'm going to type secret, SECRET, and it
doesn't show what you're typing.
| | 05:59 |
Don't worry about that, that's a security
measure.
| | 06:01 |
Hit Return, it asks me to type it again.
I'm going to type secret again.
| | 06:05 |
And now it's set, now my password is
there.
| | 06:07 |
If I try and run that command again
you'll see it says oops, sorry you can't
| | 06:11 |
do that because did not provide a
password.
| | 06:14 |
If you want to change your password,
well, then you just need to provide the
| | 06:18 |
dash p option to that.
And it'll come up and say, all right,
| | 06:21 |
give me your current password, SECRET,
and the new password.
| | 06:25 |
I'm going to make my new one John, Paul,
George and Ringo.
| | 06:33 |
Type that again, John, Paul, George,
Ringo.
| | 06:39 |
Okay, so now that's my new password.
All lowercase, all run together.
| | 06:43 |
John Paul George Ringo is my new
password.
| | 06:45 |
Now you can't see that, but you hopefully
put in your own password anyway and not
| | 06:49 |
the same password as me.
Now we can still access MySQL.
| | 06:52 |
We can still run the MySQL program from
here.
| | 06:54 |
But we won't be able to access it
remotely at all.
| | 06:57 |
Only from the local computer.
Only when we're sitting right here can we
| | 07:01 |
still get into the MySQL program.
And it's important to still have access
| | 07:04 |
to MySQL in some way from this computer.
Because that's going to allow us to reset
| | 07:08 |
the password if we ever lose it.
And there are instructions on how to go
| | 07:11 |
about resetting a lost root password on
the MySQL site.
| | 07:15 |
So if you ever lose it you can go to
their site and they'll help you to get it reset.
| | 07:19 |
Okay, now that we've got our password
set, we've completed the configuration of
| | 07:22 |
MySQL and we're ready to use it.
| | 07:23 |
| | Collapse this transcript |
| Text editor| 00:00 |
In this movie we're going to make sure
that you have a good Text Editor installed.
| | 00:02 |
This is what we're going to be using to
write our PHP code.
| | 00:06 |
If you've been coding before, then you
may already have a favorite text editor
| | 00:09 |
and that's great.
You can feel free to use any text editor
| | 00:12 |
that you want.
The only requirement is that it needs to
| | 00:15 |
just generate only text at the end.
That's what a text editor does.
| | 00:19 |
It's very different from a word processor
like Microsoft Word.
| | 00:22 |
That would be a terrible text editor.
And the reason why is because that is
| | 00:25 |
focused on a different purpose.
It's about formatting a document.
| | 00:29 |
So it keeps all sorts of extra
information in the file.
| | 00:31 |
Like where are your margins set?
What are your tabs set at?
| | 00:34 |
What font is being used?
All that information gets stored in there
| | 00:37 |
and we don't want it in there.
We want just our code.
| | 00:40 |
Just the instructions, that the Apache
web server is going to read and process.
| | 00:44 |
In addition, text editors, or code
editors often have lots of nice features
| | 00:48 |
that make it easier to use and allow us
to work faster and make less mistakes.
| | 00:53 |
So let's take a look at some of the
features you want to watch for.
| | 00:55 |
The essential features, you want to look
for code coloring and syntax highlighting.
| | 00:59 |
That is, you'd like the page to actually
have colors to help tell you what is the
| | 01:04 |
HTML, what's the JavaScript, what's the
PHP.
| | 01:08 |
Where does the function start, and where
are the different arguments to the
| | 01:12 |
function located?
All those things can have different
| | 01:14 |
colors to them that help us to be able to
read the code quickly and understand
| | 01:18 |
what's going on.
They also usually need to have a way that
| | 01:20 |
we can navigate a whole project.
So we can open up a project and move
| | 01:25 |
between the files quickly instead of
having to always go back to the finder,
| | 01:29 |
open the file work with it, close it
again.
| | 01:31 |
You want it to have good search and
replace features, especially once you
| | 01:34 |
start working on a large project.
It's really important to be able to
| | 01:37 |
search all your code to look for times
when you've used a certain variable name,
| | 01:41 |
let's say.
Maybe you want to change the variable
| | 01:43 |
name, well, we want to replace it
throughout the entire application.
| | 01:47 |
Make sure that we can find them all.
It's also important to have good
| | 01:49 |
auto-pairing of brackets parentheses and
quotes.
| | 01:52 |
So when we type an open bracket it goes
ahead and creates the closed bracket at
| | 01:56 |
the same time.
So that we don't forget to do that,
| | 01:58 |
they're always closed.
When we're coding that's super important
| | 02:02 |
that these are always paired up.
And then preferred features i would list
| | 02:05 |
off being able to auto indent.
So that when code needs to be indented it
| | 02:09 |
goes ahead and indents it to the right
spot, you don't have to hit tab to indent it.
| | 02:12 |
Code completion so that you can start
typing and then often hit a key like Tab
| | 02:17 |
and it will complete the thought for you.
And customizing the coloring of the
| | 02:21 |
document and the coloring of the code.
This is often referred to as themes.
| | 02:24 |
You can often have a couple of different
themes and switch between them.
| | 02:27 |
So that's what I would say that the
features you want to look for in any text editor.
| | 02:31 |
And, of course, you'll want to look for
things that are good for working with
| | 02:33 |
PHP, specifically, so that it has good
code coloring for PHP.
| | 02:37 |
Most of them will, because PHP is so
popular.
| | 02:39 |
Now, the one that I'm going to recommend
that we use, if you don't already have a
| | 02:42 |
favorite, is TextMate, it's a great text
editor, it has lots of great features.
| | 02:47 |
It's created by Macromates, so
macromates.com is the site to get it at,
| | 02:50 |
and it has a free 30-day trial, it's
about $50 to actually buy the program if
| | 02:54 |
you like it.
There are other ones out there, though,
| | 02:56 |
that I want to just list off that you can
explore and check into.
| | 03:00 |
Sublime Text 2 is extremely popular, a
lot of people have been moving from
| | 03:03 |
TextMate to Sublime.
And then Eclipse, Netbeans, Komodo, Coda,
| | 03:07 |
and BBEdit.
Those are all other popular, either
| | 03:10 |
editors or IDEs, for integrated
development environment.
| | 03:13 |
That's what that stands for.
So give all of these a look.
| | 03:15 |
Maybe TextMate won't be your favorite.
Everybody seems to have their personal
| | 03:18 |
favorite that they love and they just
won't part with.
| | 03:21 |
And if you don't have a favorite, then I
recommend you give TextMate a try to
| | 03:24 |
start with.
Lets see how we can get text mate installed.
| | 03:27 |
So here I am on the macromates.com
website.
| | 03:30 |
For TextMate you can see here is the free
30-day trial.
| | 03:34 |
You'll just click that button and it will
say where do you want that download it to.
| | 03:36 |
I'll just select the default for opening
it.
| | 03:38 |
You could also save it to your desktop
somewhere and then it will download, here
| | 03:42 |
we go.
Once that's done, I can close that up,
| | 03:45 |
and there it is, there's TextMate.
I can throw this file away anymore.
| | 03:48 |
This is the application right here, so
all you need to do is drag that to your
| | 03:51 |
applications folder.
And you can see that I already have my
| | 03:55 |
copy in here, but you can just drag your
copy in, and then you'll have it there.
| | 03:59 |
It's also not a bad idea to drag it down
to the dock as well, so that you have a
| | 04:02 |
copy down there running.
Once you've got it installed you'll just
| | 04:06 |
want to double click on it and it willpop
open with a new document.
| | 04:10 |
It may prompt you for some other
information since yours is not licensed.
| | 04:13 |
My copy already is licensed, I've
purchased it.
| | 04:16 |
I just want to show you can change the
way that this looks by going to TextMate.
| | 04:20 |
Preferences, and then under the Fonts and
Colors you can pick different themes.
| | 04:24 |
I've picked one here called Idle Fingers,
and that's the one I'm going to be using,
| | 04:28 |
it has a black background which I think
is nice and easy for reading code.
| | 04:32 |
Let's open up our existing PHP file we
have that already, php.info, I'll
| | 04:36 |
double-click on that.
You can see that it does code coloring,
| | 04:39 |
it puts it in red, when everything else
is not.
| | 04:42 |
That's one of the configurable options.
You can also notice that it gives me line
| | 04:45 |
numbers here.
That's also a nice useful feature.
| | 04:47 |
So you can read through their online
documentation.
| | 04:50 |
It will teach you a lot of the different
tips and tricks of how to do things faster.
| | 04:53 |
But the main thing that I want to make
sure was that all of us had a good text
| | 04:57 |
editor to start with before we start
creating our php code.
| | 05:00 |
| | Collapse this transcript |
|
|
3. Windows InstallationOverview| 00:00 |
In this chapter, we're going to learn how
to install PHP and MySQL on Windows.
| | 00:04 |
I'd like to start by giving you an
overview of the process before we then
| | 00:07 |
walk through each of the steps together.
There are a number of ways to get
| | 00:10 |
everything we need installed on Windows,
so lets start by looking at the list of
| | 00:14 |
general requirements.
We're going to need to have a web server.
| | 00:17 |
A web server is going to listen for page
requests from the web browser, and then
| | 00:21 |
serve up pages back to web browser in
response.
| | 00:23 |
While processing the PHP that's in those
page at the same time.
| | 00:27 |
Apache's the most widely-used web server,
especially for PHP development.
| | 00:31 |
So that's what we'll be using.
Of course, most importantly, we need to
| | 00:33 |
have PHP.
That doesn't come installed by default on Windows.
| | 00:37 |
We're going to need to add it.
Version 5.4 of PHP is the current version
| | 00:41 |
as I'm recording this, but version 5.5 is
going to be releasing very soon.
| | 00:45 |
Now, version 5.4 requires Windows XP,
Windows 2003, or something newer.
| | 00:50 |
Version 5.5 is going to drop support for
Windows XP and 2003, and therefore, it
| | 00:56 |
will require Windows Vista or something
newer.
| | 00:59 |
I'm going to be installing on Windows 8.
The process for installing on any version
| | 01:02 |
of Windows is going to be very similar.
Now, almost everything that I'm going to
| | 01:06 |
teach you in this tutorial about PHP,
will work with any version of PHP 5, even
| | 01:11 |
future versions.
And any time there is a difference
| | 01:14 |
between versions, I'll be sure to call it
out and highlight it to you.
| | 01:17 |
So if you have an older version of PHP
than what I've got, or if you've got a
| | 01:21 |
newer version that's come out since,
don't worry.
| | 01:23 |
The fundamentals and the essentials of
PHP are going to work in all of these versions.
| | 01:28 |
The next thing we'll need of course is a
MySQL database.
| | 01:31 |
The latest version of the MySQL database
is going to be the best one used.
| | 01:35 |
But any recent version of MySQL will work
just fine.
| | 01:38 |
The basic SQL syntax that we'll be using
hasn't changed in decades.
| | 01:42 |
And then we'll want to have a text
editor.
| | 01:44 |
That's different from a word processor.
We'll talk a little bit more about the differences.
| | 01:48 |
We're going to need to have a way that we
can edit our web pages.
| | 01:51 |
And of course, we'll need a browser.
That is a web browser that can issue
| | 01:54 |
requests to our web server and ask for
web pages back.
| | 01:58 |
So, these are the requirements, now let's
talk about how we're going to get them installed.
| | 02:01 |
For Apache, PHP, and MSQL, we can install
the WampServer.
| | 02:06 |
WampServer is an all in one built in
package that contains all three of these,
| | 02:13 |
so we can just solve all of our problems
right there by installing the WampServer.
| | 02:17 |
It's available for download at
wampserver.com/en.
| | 02:20 |
And we'll take a look at that, and we'll
actually download it together.
| | 02:24 |
That will allow us to have an environment
where we can do our PHP and development.
| | 02:28 |
It makes it really easy.
Now, for a text editor, I'm going to have
| | 02:31 |
us use Notepad++.
It's very popular, you can find that
| | 02:36 |
online at notepad-plus-plus.org.
There are a number of other text editors
| | 02:41 |
that we can choose, and I'll give you a
few of the other options that you might
| | 02:44 |
want to consider.
But then I'll show you how to download Notepad.
| | 02:47 |
And then for the web browser, we're
going to be using Firefox.
| | 02:51 |
Now, any browser will work.
Of course, Windows comes with Internet
| | 02:54 |
Explorer, and you may want to just use
that.
| | 02:56 |
There are sometimes some small
differences in the way that different
| | 02:59 |
browsers display different HTML and CSS.
So, if your HTML and CSS doesn't look
| | 03:05 |
exactly like mine on the screen, it could
be because you're not using the same browser.
| | 03:09 |
I think most of the things we're going to
be doing should be cross-browser
| | 03:12 |
compatible, so it should be okay
regardless of which browser you use.
| | 03:15 |
But Firefox is what we'll be using, and
if you want to make sure that yours looks
| | 03:18 |
like mine, that would be a good choice.
So, now that we understand the steps that
| | 03:22 |
we're going to need to take, let's get
started by downloading and installing the WampServer.
| | 03:27 |
| | Collapse this transcript |
| Installing WampServer| 00:00 |
In this movie, we're going to download
and install the WampServer package.
| | 00:04 |
You'll remember that Wamp is an
abbreviation.
| | 00:06 |
Which stands for Windows, Apache, MySQL,
and PHP.
| | 00:10 |
And together that's what we refer to as
the stack that we'll be running, the
| | 00:14 |
stack of technology that we'll be using.
And Windows of course, we already have.
| | 00:19 |
So what we're really downloading is the
A, M, and the P.
| | 00:22 |
The Apache, MySQL, and PHP as part of the
WampServer package.
| | 00:26 |
And you can download the installer for
that package from the WampServer
| | 00:30 |
websitem, which is
http://wampserver.com/en/.
| | 00:37 |
That final en is there for the English
language version of the site.
| | 00:41 |
If you don't put that then by default
you're on the French language version.
| | 00:45 |
So here I am in Firefox on
wampserver.com/en.
| | 00:49 |
And they may redesign this website from
time to time, don't let that throw you.
| | 00:52 |
What we want to find are the download
links, the the download area.
| | 00:56 |
And right now, there's several different
packages here that we can choose for downloading.
| | 01:01 |
That's referring to the architecture of
your computer's processor.
| | 01:05 |
If you don't know which one you have,
then you can go to your Control Panel and
| | 01:10 |
you can go to System and Security, and
under System, it will tell you.
| | 01:15 |
It says System Type and here it say 64
bit operating system x64 base processor.
| | 01:20 |
So my version of windows is 64 bit and my
processor is 64 bits.
| | 01:24 |
So it's okay for me to choose 64 bit
version which is actually a faster version.
| | 01:28 |
So you want to pick the one that's
compatible for you.
| | 01:31 |
Now out of those I still have a few more
choices.
| | 01:33 |
This on is PHP 5.3 that's an older
version.
| | 01:36 |
This one's 5.4 that's the current version
as I'm recording this.
| | 01:39 |
5.5 is just about to come out.
And then this one over here, it doesn't
| | 01:43 |
say it up here but it's PHP 5.4 still the
difference is that it's a newer version
| | 01:48 |
of Apache.
It's Apache 2.4 instead of Apache 2.2.
| | 01:51 |
You won't notice a difference between the
two versions of Apache, but as a general
| | 01:55 |
principle, always pick the latest
version.
| | 01:57 |
The latest and greatest of all these
parts and components.
| | 02:00 |
So that's what I'm going to pick here.
Now once I click it, it comes up and asks
| | 02:04 |
me a few more things.
And then I have a like that says you can
| | 02:07 |
download it directly and that will start
the actual download.
| | 02:10 |
Now I'm not going to click that link
because I've already downloaded it.
| | 02:13 |
It's here on my desktop.
So you want to locate that ion the place
| | 02:16 |
wherever your browser downloads it, and
then you'll Double click on the installer
| | 02:19 |
to launch it.
We'll get some security warnings that we
| | 02:22 |
have to accept, and then it comes up with
the install wizard which shows us the
| | 02:29 |
versions it's going to install here.
We click Next.
| | 02:32 |
We need to read and accept the license
agreement.
| | 02:35 |
And then it says where do you want to
install WampServer 2?
| | 02:38 |
Now, unless you know you need it
somewhere else, go ahead and let it just
| | 02:41 |
install it in the default, which is
c://wamp.
| | 02:46 |
So that's the root of the C drive in the
directory called Wamp.
| | 02:49 |
That's a good place for it.
Now, here I can add some icons.
| | 02:52 |
I'm going to choose the quick launch
icon.
| | 02:54 |
I'm not going to choose the create a
desktop icon.
| | 02:56 |
And then it says, okay, are you okay with
these?
| | 02:59 |
Are you ready to install?
And we click Install.
| | 03:01 |
And it takes off.
It starts installing all of the files
| | 03:04 |
that it needs.
And then once it's done extracting all
| | 03:07 |
the files, it'll come up and say please
choose your default browser.
| | 03:10 |
This is the browser that WAMP will open
up for you when you ask it to show you
| | 03:14 |
certain pages.
So we want to tell it what our default
| | 03:17 |
choice is.
By default, that'll be Explorer, that's
| | 03:19 |
what it offers down here, and instead,
I'm going to switch it to Firefox, local
| | 03:24 |
host Program files, an then here's
Firefox.
| | 03:28 |
I'll pick Firefox and click Open, and
then we'll finish the installation.
| | 03:33 |
Firewall has blocked some features.
So do we want to allow Apache to
| | 03:38 |
communicate to these other networks?
Private networks, yes.
| | 03:41 |
Public networks, no.
Let's go ahead an just say private
| | 03:44 |
networks for now.
We're going to be developing locally, so
| | 03:46 |
it's going to be okay.
And let's say, allow access.
| | 03:50 |
And then for your server name, leave it
as said as Local Host.
| | 03:53 |
That's what we call by default the local
version of Apache that's running.
| | 03:57 |
Now, if you have an SMTP server and you'd
like your local development to be able to
| | 04:02 |
send mail using the mail function, you
would configure that here.
| | 04:06 |
We're not going to be using the mail
function and I don't have an SMTP server
| | 04:10 |
set up.
So, I'm just going to click next and
| | 04:11 |
leave the defaults in there for now.
That's okay.
| | 04:13 |
And then we're at the end.
So, that's great.
| | 04:15 |
We're all done and I'm going to leave
this box checked that, says launch
| | 04:18 |
WampServer 2 now and I'll click Finish
and that's what it'll do.
| | 04:21 |
It'll launch it for us.
We'll approve the security alert again.
| | 04:26 |
And now you'll notice that there's this
little item down here in the corner,
| | 04:30 |
that's for the WAMP control menu.
Now if you don't see it, it may be hiding
| | 04:34 |
under here.
You can customize that and make sure that
| | 04:37 |
it shows all the time.
Show icons in notifications, and now it
| | 04:42 |
will stay there all the time.
And when I click on it, it gives me the
| | 04:46 |
WAMP server menu, the choices that WAMP
server does.
| | 04:50 |
Now if it's green, that means that it is
successfully online.
| | 04:54 |
That it is working if it's orange it
tried to go online and it failed.
| | 04:58 |
Something blocked it.
If it's red that means turned off line
| | 05:02 |
and you need to start or stop the
different services here.
| | 05:04 |
You can test to make sure that it is in
fact working by going to local host.
| | 05:09 |
And that will open up the default browser
that we just told Wamp about.
| | 05:13 |
And it will give us this information.
If you see this page, then it means that
| | 05:17 |
it was able to connect to Apache and to
bring up this default information.
| | 05:22 |
And there's some other good links here.
One of these is PHP info.
| | 05:25 |
If we click on that, we now know that PHP
is working as well.
| | 05:29 |
So we know that Apache's working.
And we know that PHP is working, because
| | 05:32 |
we're able to see all this information
about PHP on the PHP info page.
| | 05:36 |
It's a good way to check and make sure if
it's green, you can see Apache, and you
| | 05:42 |
can see PHP, then you know you're
probably in good shape on the WAMP side.
| | 05:45 |
| | Collapse this transcript |
| Finding the document root| 00:00 |
In this movie, we're going to talk about
how to go about finding the document root.
| | 00:04 |
It's a very important folder directory
that we need to locate, so that we know
| | 00:07 |
where to put our web files as we work.
We're working with WampServer, assuming
| | 00:12 |
you installed it in the default location,
the document root is going to be inside
| | 00:17 |
local C drive, inside the WAMP folder or
wherever you installed WAMP.
| | 00:22 |
And then from there, it's the www folder,
that is the file that is going to be
| | 00:27 |
served up by Apache.
Apache is looking at this directory and
| | 00:30 |
returning pages from this directory.
So all of our project files need to go here.
| | 00:34 |
Now there is also another way that you
can get there which is from the
| | 00:38 |
WampServer menu over here at the side, it
was www directory.
| | 00:43 |
That is our document root.
It opens up the exact same thing.
| | 00:45 |
And index.php, well, that's this test
page, that's right here for local host.
| | 00:51 |
If you go to local host, you'll open up
Firefox and it loads local host, whhich
| | 00:56 |
is the same thing as local host
index.php.
| | 01:01 |
That's the default page is index.php so
it's implied.
| | 01:05 |
So that is what's loading up here, is
that page.
| | 01:07 |
So that shows you the relationship
between where files are stored on the
| | 01:11 |
hard drive and where we access them in
the browser.
| | 01:13 |
Local host is what we used to get to
Apache, and to get to this document root.
| | 01:19 |
And then we provide whatever else we need
after that to be able to locate the file
| | 01:23 |
inside whatever nested directories might
be there.
| | 01:25 |
So you're going to want to put all of the
PHP project files that we create, in this
| | 01:29 |
www directory.
And then you'll access them from the
| | 01:33 |
browser by typing it this way, not by
double-clicking on the file.
| | 01:38 |
It's very important, we need to tell
Apache that it has to process these files.
| | 01:42 |
So we don't want to just open up PHP on
it's own.
| | 01:44 |
We have to get there by going through
Apache, so that Apache can interpret them
| | 01:48 |
and return good HTML to us.
Now, one other note that I want to make
| | 01:52 |
is that it's different from where I'm
going to be on the Mac.
| | 01:55 |
And I'm going to be demonstrating
everything on the Macintosh.
| | 01:57 |
On the Mac, I'm going to be locating
those files by using localhost/ and then
| | 02:02 |
tilde and then my username.
For on Windows, though, it's just
| | 02:06 |
going to be the localhost part.
So anytime you see me start to type that
| | 02:09 |
longer string on the Mac followed by some
other file names, you'll just take out
| | 02:13 |
that username portion and just use
localhost/ and then whatever comes after
| | 02:17 |
that, because on the Mac, my document
root is in a slightly different place.
| | 02:20 |
But if you keep that one change in mind,
you won't get lost.
| | 02:23 |
| | Collapse this transcript |
| Configuring PHP| 00:00 |
In this movie, we're going to configure
PHP to the settings that we like and we
| | 00:04 |
need for this tutorial.
Three things that we're going to be
| | 00:07 |
looking at, error reporting, output
buffering, and timezone.
| | 00:11 |
Those are the three areas that we want to
address.
| | 00:14 |
In the last movie, I showed you how to
get the PHP info page.
| | 00:17 |
You do that from your WAMPSERVER menu,
and you choose local host and that will
| | 00:21 |
take you to the main WAMPSERVER page, and
then phpinfo is a link down here that
| | 00:27 |
will actually take you to it.
So once you're on that page, this has all
| | 00:30 |
the information about the way in which
php is configured.
| | 00:33 |
So you can always come here if you want
to see what is php running right now.
| | 00:37 |
What are its configuration options that
it's using?
| | 00:40 |
This will tell you.
The most important one of those is this one.
| | 00:43 |
Loaded configuration file.
That tells us where the configuration
| | 00:47 |
file for PHP lives.
It's called php.ini.
| | 00:51 |
Now WAMP also gives you a short cut so
that you don't actually have to go
| | 00:55 |
directly there.
You can just go to the WAMP menu and then
| | 00:58 |
under PHP you'll see there's an option
for php.ini.
| | 01:02 |
Lets open that up now.
Now it opens up in your default text editor.
| | 01:06 |
For me right now, that's just Notepad
that's built into Windows.
| | 01:10 |
And these are all the configuration
options.
| | 01:12 |
Every line with a semi colon in front of
it is a comment line.
| | 01:16 |
That means that it's not actually being
processed.
| | 01:19 |
What's actually being processed are the
lines that don't have semicolon the
| | 01:22 |
directives that are a little further
down.
| | 01:24 |
So here, y2k_compliance equals on for
example.
| | 01:28 |
Precision equals 14 for significant
digits displayed at floating point
| | 01:32 |
numbers, and so on.
Let's find the ones that we're interested in.
| | 01:36 |
I'll just do a search, and I can do that
from the Edit and then I have Find or
| | 01:41 |
Control F you'll see also is what tells
me will do it.
| | 01:44 |
And then the first thing I want to find
is display_ errors.
| | 01:48 |
And find the next occurrence to that.
Now that's a commented outline, I'm not
| | 01:52 |
interested in that.
Let's find the real one, and here it is
| | 01:55 |
down here.
display_errors equals on.
| | 01:59 |
Now it also gives you some information on
how to set this.
| | 02:02 |
And it typically gives you a development
value and a production value.
| | 02:06 |
Development is what we're going to be
doing on this computer locally.
| | 02:10 |
Production is when we actually deploy to
a web server for the whole world to see
| | 02:14 |
and use.
So we want to have different settings for
| | 02:16 |
those two things.
For example with displaying errors, we
| | 02:20 |
want the errors to be on for us, so that
we can see every single thing that goes
| | 02:25 |
wrong while we're building it.
But when we put it in production, we
| | 02:28 |
don't want to to expose all of that data
to our public.
| | 02:31 |
We don't want to to tell them gobs and
gobs of information about our code and
| | 02:34 |
about our set-up.
So instead, we'll turn it off in that case.
| | 02:38 |
So you'll want to make sure that it's
turned on for you and it should be.
| | 02:42 |
Now that's not the only setting related
to errors, there are a couple more.
| | 02:45 |
The next one is error reporting, you can
either search for it if you can't find it.
| | 02:49 |
For me, it's right above it here and
let's just scroll up a little bit.
| | 02:52 |
Find out some of the information about
that, and that tells you what all these
| | 02:56 |
different things are.
And, why you would use them and gives you
| | 02:59 |
some common combinations that you might
use.
| | 03:02 |
Mine is set to report all errors.
Now up until I think PHP 5.4.
| | 03:07 |
This said E_ALL and E_STRICT, which is
what you can see is what it recommends up here.
| | 03:13 |
That E_STRICT is now included in E_ALL,
so you don't have to do that anymore, but
| | 03:18 |
if you do, it's fine, it's just backwards
compatible for older versions of PHP.
| | 03:22 |
Now when we're in production of course,
we want to tell it not to do these and
| | 03:26 |
you put the little twiddle in front of
it, the little tilde.
| | 03:29 |
So E_ALL and not the deprecations.
Deprecations are code that's going to
| | 03:34 |
stop working in future versions.
It works just fine right now, so there's
| | 03:38 |
no reason to bother the production server
with it.
| | 03:40 |
But as a developer, it's very important
to see those deprecation, because it says
| | 03:44 |
hey, you're using something that's about
to go out of date.
| | 03:47 |
You might want to think about a
replacement for this soon, so make sure
| | 03:51 |
that you have either E_ALL or that you
have E_ALL and this upright bar E_STRICT.
| | 03:57 |
Now if you're wondering, what is this
weird E_ALL.
| | 04:00 |
It's actually a constant that represents
a number.
| | 04:02 |
And if we just shoot back over here to
our phpinfo page.
| | 04:05 |
Let's just do a find here real quick, for
error reporting, and there it is.
| | 04:11 |
And this is the number that it comes up
with.
| | 04:14 |
But instead of trying to work with all
the different numbers that correspond to
| | 04:17 |
these different things, those are
settings of different bits.
| | 04:21 |
Instead we just use E_ALL.
It's a lot more convenient.
| | 04:25 |
The next one I want us to look for in
this same document is HTML errors.
| | 04:30 |
HTML errors, and here it is.
You want to make sure that this is turned on.
| | 04:34 |
You would turn it off for production.
But it basically says, when you create an
| | 04:38 |
error, do you want me to just give you
text, or do you want me to put some HTML
| | 04:42 |
around the error as well?
And It'll look better with the HTML
| | 04:44 |
around it, I think it looks a little
nicer.
| | 04:46 |
So, let's go ahead and do that.
It also allows it to include HTML links
| | 04:50 |
in those errors, which can be handy.
So that's it for errors.
| | 04:52 |
Now let's look at output buffering.
That's a concept that we'll talk about
| | 04:56 |
later on.
For now, I'll just try and find output_buffering.
| | 05:02 |
It says it can't find it, but I need to
search up.
| | 05:04 |
It's probably up above.
that's not the one I want.
| | 05:08 |
There it is.
So, let's go up a little bit more.
| | 05:12 |
Now, my default setting is just to have
it turned on.
| | 05:15 |
It says here that you can also provide a
number that's the amount of bytes that it
| | 05:19 |
will allow to buffer, 4,096 is a default
value.
| | 05:23 |
That's what more people use.
So, you could also just have 4,096 in here.
| | 05:27 |
However, we're going to alternate off
right now.
| | 05:30 |
Alpha buffering is a desirable feature.
It's something we're going to talk about,
| | 05:34 |
and we're going to learn why you would
want to use it, but until you know about
| | 05:37 |
it, I want us to have it off.
And then you'll appreciate it and
| | 05:40 |
understand oh that's what it does and
that's why I need to use it.
| | 05:44 |
So, for now let's all make sure that
alpha buffering is turned off.
| | 05:48 |
And then the next thing that we want to
look at is the timezone.
| | 05:51 |
So again, let's do a search.
We'll do search for timezone.
| | 05:54 |
There it is right there, date.timezone
equals and you might have empty quotes
| | 06:02 |
here, it might be commented out, or like
me you might have UTC.
| | 06:06 |
UTC is Greenwich Mean Time.
You can leave it at UTC and it'll work
| | 06:10 |
just fine.
But you do need to set it to something
| | 06:13 |
and that's because if you don't set it
then PHP is going to start giving you
| | 06:17 |
some errors from time to time saying hey,
you're doing something that's related to
| | 06:21 |
date and time but you don't have a
default time zone set.
| | 06:24 |
That's not a good idea.
So let's avoid those warnings by having
| | 06:27 |
some times zones set.
Now I'm not in UTC so I'm going to go
| | 06:31 |
ahead and give it my time zone.
On the php.net website, you can find the
| | 06:36 |
list of those,
php.net/manual/en/timeszones.php.
| | 06:41 |
And then I'm in America, so I'll click on
America and you can scan down and you can
| | 06:45 |
find cities that are close to you.
So maybe you're somewhere close to
| | 06:48 |
Denver, or you're close to Louisville,
I'm actually going to be close to New York.
| | 06:53 |
So here it is for me.
America, New_York.
| | 06:56 |
So that's what I need to put in as my
value.
| | 07:00 |
Has to be exactly like that.
And I'm going to put it in quotes.
| | 07:03 |
It's going to be America/New_York.
So that's going to be my timezone.
| | 07:10 |
And it will then use that as a default
when it's working with dates.
| | 07:13 |
And most importantly, it'll get PHP to
stop giving us warnings about it.
| | 07:16 |
So once you're done, you'll want to go to
File and Save to save your changes, then
| | 07:21 |
you can close it.
And then let's go back to WAMP.
| | 07:24 |
And let's Restart All Services.
So that'll get all of those changes take effect.
| | 07:28 |
It'll turn yellow briefly, red, yellow
and then back to green.
| | 07:33 |
Now it's back online.
Now if we go back here to our phpinfo
| | 07:36 |
page, scroll up to the top and reload the
page.
| | 07:40 |
And then we can look for some of those
changes to see if they took effect.
| | 07:44 |
The one that I'm most interested in is
going to be Output Buffering.
| | 07:47 |
So let's see just see if we find that.
Output and it has no value right now for
| | 07:51 |
it, which means that it's turned off.
Let me look for my timezone, timezone.
| | 07:57 |
And you can see my default timezone now
says America and New York.
| | 08:01 |
So those are the three key configurations
that you want to make sure that you set
| | 08:04 |
up before you start using PHP.
There are plenty of other ones in there
| | 08:07 |
that you could set for yourself, but
those are the ones that we really need to
| | 08:10 |
have before we move forward.
| | 08:11 |
| | Collapse this transcript |
| Configuring MySQL| 00:00 |
In this video, we're going to do some
configuration to MySQL.
| | 00:03 |
Specifically, I want us to set a root
password, MySQL, because by default,
| | 00:08 |
MySQL comes with no default password for
the root user.
| | 00:13 |
Root user being the most powerful user on
the system.
| | 00:15 |
The one with the most privileges inside
MySQL.
| | 00:18 |
And it's just a good security practice to
always have a default password set, so
| | 00:23 |
that only someone who knows that password
can get access to the data that's in the database.
| | 00:28 |
Now you may think, well, I'm not going to
be sharing this data with anyone.
| | 00:31 |
It's only on my personal laptop.
I'm the only one who uses it.
| | 00:34 |
But it is a good practice, because you
may end up importing some data from a
| | 00:39 |
production database, at some point, onto
your development machine.
| | 00:42 |
And if someone were to come and find your
laptop, or if you were to lose it, they
| | 00:45 |
could get access to all of the data that
was on there.
| | 00:48 |
So I want us to go ahead and follow this
best practice and set a root password for MySQL.
| | 00:52 |
Now the tool that we need in order to
update the MySQL password is MySQL Admin,
| | 00:57 |
the tool that's installed with MySQL.
WAMP gives you lots of nice tools here
| | 01:02 |
from this directory, but they don't give
you one for MySQL Admin.
| | 01:06 |
They do give you a way to go directly
into MySQL here, MySQL console.
| | 01:10 |
That's kind of nice.
Comes up and says enter password, and
| | 01:13 |
since there is no password right now, I
can just hit Return and it puts me
| | 01:17 |
directly into MySQL.
So I've logged in as the root user.
| | 01:20 |
I didn't have to type a password.
I just simply hit Return.
| | 01:23 |
Now I'm in MySQL.
Instead, I like to be able to enter a password.
| | 01:27 |
Now to do that, we're going to have to go
to the console.
| | 01:30 |
The Command Prompt.
So you want to find the Command Prompt
| | 01:33 |
that's going to be inside all of your
programs, inside Windows System.
| | 01:37 |
Command Prompt, you'll find it there.
This is the command line interface to Windows.
| | 01:42 |
Don't be intimidated by it, we're not
going to be here for very long.
| | 01:45 |
What we want to do, is we want to
navigate to the place where MySQL Admin lives.
| | 01:50 |
If we just type mysqladmin on its own, it
comes up and says sorry, I don't know
| | 01:53 |
what that is.
But if we go to cd, change directory, C,
| | 01:58 |
that's a capital C:\wamp\bin\mysql.
And then we type dir, we'll get a listing
| | 02:10 |
here, and this folder name will change
depending on what version of MySQL you have.
| | 02:14 |
Now we want to do another cd mysql, and
you can actually just hit the Tab and it
| | 02:20 |
should auto-complete for you.
If not, go ahead and type that full
| | 02:23 |
number out there, and that will change
directories one more time and then put
| | 02:27 |
/bin after it.
So now we're inside a place where all of
| | 02:30 |
the programs live for MySQL.
We could actually click the dir, we can
| | 02:34 |
see what all those are.
The one that we want is MySQL Admin.
| | 02:41 |
Then after it put dash u space root for
the root user, and then we want to type
| | 02:47 |
password because that's what we want to
change.
| | 02:49 |
So all lower case password.
We'll hit Return, and it will then come
| | 02:53 |
up and ask us for the new password that
we want to set.
| | 02:56 |
So I'm going to set mine just to be
secret for now, secret and it doesn't
| | 03:01 |
show it.
It just has the bullets there instead,
| | 03:03 |
hit Return, confirm it, and I'll type it
again, and now it's set.
| | 03:07 |
That's my new password that I have to
type to get in.
| | 03:10 |
When I hit the Up arrow, and it will
bring that line back up again.
| | 03:13 |
If we want to change it again to
something different, it'll come up and
| | 03:17 |
it'll say oops, sorry, I needed some
kind of indication that you wanted to
| | 03:21 |
give a password.
So what we need to do is actually just go
| | 03:23 |
back and make a small edit to this line
and put dash p, which means I will be
| | 03:28 |
providing a password.
So it will ask me for the password, it's
| | 03:31 |
going to prompt me for it.
But I'm just letting it know, look, it's
| | 03:34 |
not okay to just go with no password and
then when it says enter password here.
| | 03:37 |
It wants my old password, which is
secret.
| | 03:42 |
And now it wants the new password.
But I'm going to change it.
| | 03:45 |
I'm going to make my password
johnpaulgeorgeringo.
| | 03:51 |
And then it will ask me to confirm it.
johnpaulgeorgeringo.
| | 03:58 |
And now it's set.
So now that's my new password for my root user.
| | 04:02 |
And we can confirm that by coming back
over here and going into MySQL to the console.
| | 04:08 |
And if we just type return this time, it
says nope, sorry, no good and it closes
| | 04:13 |
on us.
If we try it again, MySQL console.
| | 04:17 |
And this time I type johnpaulgeorgeringo,
there I am, now I'm inside My SQL and
| | 04:25 |
just typing exit will get us out of
there.
| | 04:28 |
Now there is one other thing that I want
to mention to you about this.
| | 04:30 |
Which is that with WAMP is also included
an application allied PHP My Admin.
| | 04:35 |
It's a very popular way to look at your
MySQL databases and to manage them by
| | 04:39 |
using a PHP web interface.
Now we're not going to be using that as
| | 04:43 |
training, we're going to be using MySQL
directly, using the MySQL console.
| | 04:47 |
But I just want to mention that we did
just break phpMyAdmin because it's not
| | 04:51 |
setup to use a password.
If you want it to use a password, you
| | 04:55 |
need to go back to your command prompt,
navigating to C:\wamp.
| | 05:02 |
And then in there if you look, you'll see
there's a directory called Apps so we
| | 05:06 |
will also change directory into Apps.
And then inside here is PHP MyAdmin with
| | 05:12 |
a version number after it.
So we'll just use Tab to auto complete
| | 05:16 |
that for us.
And then inside here, you'll find that
| | 05:19 |
there's a file all the way at the top
called config.inc.php and that is where
| | 05:27 |
it gets its configuration.
So you would edit this file and you would
| | 05:31 |
find the location where it has password
and change that to be your new root password.
| | 05:37 |
So I just wanted to mention that to you.
Even though we won't be using it in this
| | 05:40 |
training, I didn't want to leave you with
some software that we broke without
| | 05:43 |
giving you some direction on how to fix
it.
| | 05:44 |
| | Collapse this transcript |
| Text editor| 00:00 |
In this movie, we're going to make sure
that you have a good text editor installed.
| | 00:03 |
This is what we'll use to write our PHP
code.
| | 00:06 |
Now if you've coded before, you may
already have a favorite text editor.
| | 00:09 |
And if so, that's great.
You can feel free to use any text editor
| | 00:12 |
that you want.
The only requirement is that any
| | 00:15 |
application you use should generate only
text at the end.
| | 00:18 |
That's what a text editor does.
It's very different from a word
| | 00:21 |
processor, something like Microsoft Word.
Word would make a terrible choice for a
| | 00:25 |
text editor.
And that's because it's built to serve a
| | 00:27 |
different purpose, which is to format the
text in a document.
| | 00:31 |
So, Word documents are not just text,
they include lots of formatting
| | 00:34 |
information as well.
Like what font you're using, or where
| | 00:36 |
your margins are set, and we don't want
all that formatting information.
| | 00:40 |
We just want the code, the PHP
instructions that Apache will interpret
| | 00:44 |
to return web pages.
In addition, text editors or code
| | 00:48 |
editors, as they're sometimes called,
often include lots of other nice features
| | 00:52 |
that allow us to code faster and to make
less mistakes.
| | 00:55 |
So, let's take a look at some of the key
features to watch for.
| | 00:57 |
The features that I'd list as being
essential features are code coloring, or
| | 01:01 |
syntax highlighting.
That is, so that different parts of the
| | 01:04 |
PHP language will be colored in different
ways.
| | 01:07 |
It helps you to quickly be able to read
your code, and identify different parts
| | 01:10 |
of it.
To see what's a function, to see what's a variable.
| | 01:13 |
And you'll be able to read your code a
lot faster.
| | 01:16 |
The ability to navigate a whole project
at one time so that you're not constantly
| | 01:20 |
opening files and closing files again.
But that you can open them all up and
| | 01:24 |
quickly switch between them.
A really good search and replace is a
| | 01:27 |
pretty essential feature, especially if
you can search across the entire project.
| | 01:31 |
So that you can look for all occurrences
where you've used a certain function.
| | 01:34 |
It can be really useful.
And then auto-pairing of brackets,
| | 01:37 |
parentheses and quotes.
The idea here is that it's really easy
| | 01:40 |
when you're coding to type an opening
parentheses.
| | 01:43 |
And then, to forget to type the closing
parentheses that you need on the other side.
| | 01:47 |
And your code will break if you don't
have both.
| | 01:49 |
So, a lot of these text editors will
auto-pair them.
| | 01:52 |
As soon as you type the opening
parentheses, it automatically adds the
| | 01:56 |
closing parentheses right after it.
And you keep typing right between those
| | 02:00 |
parentheses, but both the beginning and
the end are both right there.
| | 02:03 |
A lot of times, once you roll over a
parentheses or a bracket or something.
| | 02:06 |
It'll highlight the matching one on the
other side so that you can quickly
| | 02:10 |
identify the pairs.
Now, what I'd say are preferred features
| | 02:13 |
would be auto-indent, so that when you go
down to the next line in your code.
| | 02:18 |
It automatically puts your cursor
indented to the place where it ought to be.
| | 02:21 |
Instead of having to hit Tab a few times,
code completion can be a nice feature.
| | 02:25 |
The ability to start typing a function
name.
| | 02:27 |
And then, hit some kind of a hot key to
get it to finish completing the rest of
| | 02:31 |
that name for you, so you don't have to
type it all out.
| | 02:33 |
And then, customize document and code
coloring themes.
| | 02:37 |
The ability to not just be stuck working
with black text on a white background,
| | 02:41 |
but to be able to change it up.
And to work with some colors that suit
| | 02:44 |
your eyes better.
And to even code your PHP how you like it.
| | 02:47 |
So, if you want your function names to
appear in blue, they can be blue.
| | 02:50 |
If you want them to be green, you can
change them to be green.
| | 02:53 |
Now, the text editor that we're going to
be using is going to be a very popular
| | 02:57 |
one called Notepad++.
And you can find that at notepad-plus-plus.org.
| | 03:03 |
Notice that it's .org at the end.
If you go to .com, you won't get there.
| | 03:07 |
And one of the great things about
Notepad++ is, it includes lots of great
| | 03:10 |
text editor features.
And it's completely free.
| | 03:13 |
That's one of the reasons it's become so
popular, because it's absolutely free to use.
| | 03:17 |
It has everything that you're going to
need.
| | 03:19 |
Now, there are a lot of other text
editors and IDE's out there.
| | 03:23 |
IDE stands for Integrated Development
Environment.
| | 03:26 |
And so, I want to mention these to you
because you might want to look into them.
| | 03:28 |
And see if some of these suit your
purposes even better than the default one
| | 03:32 |
that I'm giving you.
And Sublime Text 2 is extremely popular.
| | 03:36 |
E Text Editor is meant to be a similar
version to Text Mate, but on the Windows side.
| | 03:42 |
Sort of a Windows companion to Text Mate,
but it's kind of disappeared recently.
| | 03:45 |
So, it might be a little bit hard to find
online, I don't recommend it as default
| | 03:49 |
we are going to go with Notepad instead.
And then Eclipse Netbeans and Komodo are
| | 03:54 |
all popular IDEs that people use for a
lot of languages.
| | 03:57 |
Not just PHP.
Or for working with Java C++ and things
| | 04:01 |
like that, too.
So, let's download and install Notepad++.
| | 04:04 |
So, you want to start by going to
Notepad-plus-plus.org.
| | 04:08 |
And once you're here, you'll see that
there's a link here for download.
| | 04:12 |
And that will take us to the download
page.
| | 04:14 |
Now, this is the download link that will
download the latest version for you.
| | 04:18 |
Over here, this is an ad, and you may
have several of them over here.
| | 04:21 |
Ignore all of these download links there.
The one that you want is the one that's
| | 04:25 |
right here actually in the website.
So, we'll click Download here and it will
| | 04:30 |
say, alright you want to save it or do
you download it.
| | 04:32 |
So you say Yes, I've already done that so
I'm not going to do that step again.
| | 04:36 |
Once you download it, then you'll have
this installer and we'll just
| | 04:39 |
double-click it.
We'll give it permission to do things it
| | 04:42 |
needs to do.
Select our language and here's our
| | 04:45 |
Wizard, click Next, we'll agree to the
license agreement.
| | 04:49 |
Where do we want to install it, we'll let
it install in the default location.
| | 04:52 |
Now, check the components that you want
to install.
| | 04:55 |
I'm going to go ahead and select the
default set here.
| | 04:58 |
I'm not going to make any changes to
that.
| | 05:00 |
And then, it offers a couple of other
options here.
| | 05:02 |
We don't need to worry about most of
them.
| | 05:04 |
Create shortcut on the desktop though is
going to be a good one because that will
| | 05:06 |
give us a good way to get to the program
in the end.
| | 05:09 |
And then we'll click Install.
It just takes a quick second while it
| | 05:12 |
installs the files.
Now it's done.
| | 05:16 |
And then Run Notepad is the option at the
end.
| | 05:19 |
So, I'll leave that checked.
I'll click Finish.
| | 05:21 |
It'll actually launch the program for us.
Gives us a change log of what's different
| | 05:25 |
about it.
So, you can surf around and explore and
| | 05:27 |
see the different options that are here.
But it has the basic file options up at
| | 05:30 |
the top.
We have the ability to search, we have
| | 05:33 |
the ability to set different languages.
So we have, for example, PHP that we can
| | 05:37 |
set as a language for our file.
And then, it will use code coloring on
| | 05:41 |
the file as PHP.
And it should pick up that from the .php
| | 05:44 |
ending of the file name.
And that's it.
| | 05:46 |
We now have Notepad++ installed and ready
to use as our text editor.
| | 05:50 |
| | Collapse this transcript |
|
|
4. First StepsEmbedding PHP code on a page| 00:00 |
Now that we have PHP installed.
Were ready to start learning how to use it.
| | 00:03 |
Were going to start by learning how to
embed PHP code in a page.
| | 00:06 |
Because there's some basic rules that we
need to know about.
| | 00:08 |
WWe already did this in the setup
chapter.
| | 00:11 |
We created our first file which was
my_PHPinfo.PHP.
| | 00:16 |
And we populate with just this one little
snippet of code.
| | 00:19 |
And you may not have completely
understood what it was at the time, you
| | 00:22 |
may have just followed along.
That's okay were now going to talk about
| | 00:24 |
it, and talk about how this works.
Notice that there's the beginning, and
| | 00:28 |
ending to this line with a bit of code in
the middle.
| | 00:32 |
I'll drop out the code in the middle, so
you can really see the difference.
| | 00:34 |
This is the beginning of opening a PHP
tag, and the end, closing PHP.
| | 00:40 |
What we're doing is essentially saying to
the Apache server, hey, as you're
| | 00:44 |
processing this document, turn on PHP.
Start reading the next little bit as
| | 00:50 |
being PHP code, and then when it gets to
the end and it sees that question mark
| | 00:53 |
with the greater than, The tag is over.
Now we're done with PHP.
| | 00:58 |
You can go back to doing your regular
html rendering.
| | 01:02 |
This allows us to embed PHP into the
html.
| | 01:05 |
Remember, in the early introduction I
talked about taht that's one of the
| | 01:08 |
features of PHP.
Is that we can just embed in the html code.
| | 01:11 |
So we're essentially telling Apache to
turn on and turn off PHP filtering as
| | 01:16 |
it's going through the document.
And then in between, we can put whatever
| | 01:19 |
PHP code that we want.
So if you look at somebody else's PHP
| | 01:22 |
code,you're going to see these open and
closing tags throughout the document, as
| | 01:27 |
it's turned on and off.
This isn't the only way that you can
| | 01:30 |
write these PHP tags.
There's also something called Short-Open Tags.
| | 01:34 |
These, I would say, are considered bad
form.
| | 01:37 |
This is using the same tag, but omitting
the letters PHP from it.
| | 01:42 |
There's also a form that uses the
question mark followed by an equal sign
| | 01:46 |
which outputs the results directly.
normally evaluating the PHP doesn't
| | 01:50 |
output results on its own, you have to
tell it specifically I want to output
| | 01:54 |
something here, otherwise you could just
do some processing, you could add one
| | 01:58 |
plus one...
and not out put the results and do a
| | 02:00 |
calculation in there for the second for
Mr.
| | 02:03 |
this has become more popular especially
to a lot of experienced PHP developers
| | 02:07 |
has started to prefer having the shortcut
but it's still a bad idea and we will
| | 02:11 |
talk about why just a second The other
way that you could do it is to use ASP
| | 02:15 |
style tags.
I think this is considered very poor
| | 02:18 |
form, instead of using the question mark,
we use the percent sign.
| | 02:22 |
ASP is Microsoft's version of PHP.
It's what became asp.net.
| | 02:28 |
ASP was something that actually existed
before asp.net.
| | 02:31 |
And this is the way that the tags look.
They have the percents on either side.
| | 02:34 |
And again we have that equals there.
Both of these options are something you
| | 02:38 |
can turn on and configure in your PHP INI
file to allow them.
| | 02:43 |
otherwise you need to stick with the
regular default PHP text.
| | 02:46 |
So why am I saying that this is a bade
idea to create these kinds of texts.
| | 02:50 |
Well, it's specifically because they're
enabled in the PHP INI file.
| | 02:54 |
one of the wonderful things about PHP is
that the code is portable.
| | 02:57 |
A version of PHP on Windows and MAC and
Linux is all exactly the same.
| | 03:01 |
My code can just run on one, it can be
moved to the other one and run their just
| | 03:05 |
as easily.
But, if I make a choice to use a
| | 03:07 |
different style of tag and I require that
the php.ini file be configured a certain
| | 03:12 |
way, then my code stops being portable.
Now, it will no longer run on this other
| | 03:17 |
system because it's not configured
correctly and I think that's a bad thing.
| | 03:21 |
If we just stick to the default standard
tags, then our code will stay portable.
| | 03:24 |
And that's going to be especially true if
you start writing code that's going to be
| | 03:28 |
a plug in for existing application like
Wordpress or Drupal or something.
| | 03:32 |
You want to be able to distribute that
and have everyone be able to use it.
| | 03:35 |
So you want to be in the habit of using
good, widely available, widely supported
| | 03:39 |
PHP tags.
Otherwise, I think you're shooting
| | 03:41 |
yourself in the foot before you've even
started.
| | 03:43 |
There are a couple of other points that I
want to make about your php code.
| | 03:47 |
If we open up our myphpinfo.php file, I
want to show you that the white space
| | 03:53 |
doesn't matter.
White space inside the PHP code doesn't matter.
| | 03:57 |
This is just as valid as what we had
before.
| | 03:59 |
Same here, if we put more returns in
here, tabs, spaces, those are all
| | 04:03 |
considered white space.
And PHP doesn't care about those.
| | 04:07 |
It completely ignores it.
It just executes the code as it goes through.
| | 04:10 |
And that's great, because it allows us,
then, to use white space to help us to
| | 04:14 |
create readable code.
We can indent things, we can group things
| | 04:18 |
together so that it's really nice and
easy for us to read and follow.
| | 04:21 |
We don't have to worry how the white
space is going to be interpreted.
| | 04:24 |
Not all languages work that way, and
that's one of the nice things about PHP.
| | 04:27 |
The other thing I want to point out to
you is notice that the command ends with
| | 04:30 |
a semicolon.
That's part of the way that PHP helps to
| | 04:33 |
know when one command is over and another
one is starting.
| | 04:36 |
Since white space doesn't matter.
It needs to have some reliable way to
| | 04:39 |
know this command is finished and I'm
ready to move on to the next command.
| | 04:43 |
So every line is going to need to end in
a semicolon.
| | 04:45 |
So get used to it.
It's a habit you're going to have to get
| | 04:48 |
in, when working with PHP is to always
put semicolons at the end of your lines.
| | 04:52 |
And there's one last thing I want to show
you before we move on.
| | 04:54 |
Let's just close this up, we'll save it
and close it.
| | 04:57 |
And inside our index.html file that we
created earlier, if you don't have it,
| | 05:01 |
you can just create one, a nice, simple
file inside here, index.html.
| | 05:05 |
Let's open that up, and let's just drop a
little bit of PHP in there.
| | 05:08 |
So open our PHP tags.
You see that it auto closed it for me as
| | 05:12 |
well at the same time, that's nice.
Phpinfo with my parentheses and then the
| | 05:17 |
semicolon, right?
So that's the same little bit of PHP code
| | 05:20 |
that I had.
Let's save it.
| | 05:22 |
We'll close that up.
And let's now go into our browser and
| | 05:25 |
let's load that file up.
Localhost, Kevin Scopeland.
| | 05:31 |
Index.html, that's the file I'm loading
up.
| | 05:35 |
I get hello from my user directory, I did
not get the result of that php function,
| | 05:39 |
it has nothing to do with the fact that
there was other text in there at all.
| | 05:43 |
In fact, if we view Source under web
developer, I can choose, Page Source.
| | 05:49 |
And you can see that it actually just
output the code itself.
| | 05:53 |
It didn't actually do any PHP processing
on it.
| | 05:56 |
So why is that?
I gave it my starting PHP tag and my
| | 05:58 |
ending PHP tag.
Why didn't it process it?
| | 06:02 |
While that's because the file ends in
.HTML.
| | 06:06 |
It's important that we have both of those
things.
| | 06:08 |
The file needs to end in .PHP.
That tells Apache, hey, be on the lookout
| | 06:13 |
for PHP tags.
There may be PHP tags in this document.
| | 06:17 |
And then when it sees a PHP tag It turns
on it's PHP module.
| | 06:21 |
Starts processing the PHP until we close
the PHP section.
| | 06:24 |
So it's important that we have both of
these, right?
| | 06:26 |
If we start naming our files ending in
something besides PHP, it's not going to
| | 06:31 |
see it as being a PHP file and try and do
any processing on it.
| | 06:34 |
Now that we have the fundamentals of how
to use PHP tags to embed PHP code into
| | 06:38 |
our document.
Lets start trying to use that with
| | 06:41 |
dynamic text.
| | 06:42 |
| | Collapse this transcript |
| Outputting dynamic text| 00:00 |
If you remember back to the introduction
where I talked about why you would
| | 00:03 |
want to use PHP.
The real power of PHP comes from the
| | 00:05 |
ability to Output Dynamic Text.
That's what makes it different than just
| | 00:09 |
an HTML website.
If, for example, we have an auction
| | 00:12 |
website and a new item has been posted
for sale, it gets stored in the database.
| | 00:16 |
PHP goes and grabs it out of the
database, and now the HTML page that the
| | 00:20 |
user sees is different than what it was a
few minutes ago.
| | 00:23 |
This new item has been dynamically added
to the page.
| | 00:26 |
So we're going to start looking at how we
can output dynamic text and we're going
| | 00:29 |
to start really simple by just outputting
a few words.
| | 00:32 |
And then we'll build up to being able to
pull things from the database and to
| | 00:35 |
actually output more elaborate stuff.
To start with, we're going to learn the
| | 00:38 |
most important PHP that you need to know,
which is the echo command, or function.
| | 00:44 |
Echo is just simply going to turn
whatever we say, back to the user, it's
| | 00:48 |
like an echo.
We say something, that's what gets echoed
| | 00:51 |
back to the user so that they see it.
You can think of it like printing it to
| | 00:54 |
the users browser and we'll use it inside
our PHP tags with whatever we want to
| | 00:59 |
echo back.
So for example open php tags echo space
| | 01:03 |
and then inside quotes, Hello world
semicolon At the end and then close our
| | 01:07 |
PHP text.
That will send to the users browser embed
| | 01:11 |
on the page Hello world no quotes just
the characters from the H to the D Let's
| | 01:18 |
create a page where we can try this out.
Now, instead of doing it on one of these
| | 01:21 |
other existing pages I'm going to go into
my text editor, TextMate, and I'm
| | 01:25 |
going to choose New From Template.
Now that's little different than what we
| | 01:28 |
did before, instead of just creating a
new file I'm going to create an HTML
| | 01:31 |
file, and I'm going to pick the
transitional version.
| | 01:34 |
That's just going to give me some basic
HTML at the top.
| | 01:37 |
I'm going to remove some of these bits.
Make it a little simpler and do some
| | 01:41 |
indentation on it.
And I'm going to save this file in my
| | 01:44 |
site's directory.
I'm just going to save it as basic.html
| | 01:48 |
and that will be an HTML file that I can
pull up any time I want to work with and
| | 01:52 |
to make a copy from.
Now notice it's .html so we can't put PHP
| | 01:55 |
in it.
But now I'm going to do Save As on that document.
| | 01:59 |
And I'll make a new one from it, and I'll
call it helloworld.php.
| | 02:04 |
Okay, so that's in the same folder, I now
I have both of those.
| | 02:07 |
This one I'll be coming back to often
when I really want to get some HTML
| | 02:11 |
again, and then this is the one we're
working with now, .dot php.
| | 02:13 |
Ready for PHP to be imbedded.
Put Hello World at the top, and let's
| | 02:18 |
embed our php code here.
Let's open up our tags, php echo, open
| | 02:23 |
the quotes, Hello World, exclamation
point then the semicolon, and then close
| | 02:30 |
our php tags.
So there we go, nice and simple.
| | 02:33 |
We'll save it, always important that you
must save it, because Apache is going to
| | 02:37 |
read whatever is saved on the hard drive.
Whatever's open in the text editor and
| | 02:40 |
not saved, that's only on our screen.
Let's go into Firefox and let's open up
| | 02:44 |
localhost and
kevinskoglund/helloworld.php, and there
| | 02:52 |
it is.
Now lets do View Source on that.
| | 02:55 |
Under Tools for web developer, I have the
ability to do Page Source, and you can
| | 03:00 |
see that it output all of the HTML.
And then right here, it just dropped in
| | 03:05 |
that text.
No quotes around it, just dropped that
| | 03:08 |
text right into the body.
So you see how we can embed PHP.
| | 03:12 |
That's what we would have expected to
happen, it output it right there.
| | 03:15 |
Now if we take away the echo, if we just
have the string by itself, and we reload
| | 03:19 |
the page, then it doesn't output it.
We still get the HTML, we can still view
| | 03:23 |
our source.
And see it's there, but it didn't output anything.
| | 03:27 |
The echo is a very important part of
outputting that to us.
| | 03:30 |
So let's try another one.
We're going to learn how to concatenate
| | 03:33 |
two parts together.
So we have Hello World, but this time
| | 03:37 |
let's put quotes here so that it's two
different quoted strings.
| | 03:42 |
Hello inside quotes and World inside
quotes, and I've got an extra space in
| | 03:46 |
here right before World.
This period is going to concatenate them
| | 03:51 |
together, that is smash them together
dynamically generating one new thing they
| | 03:56 |
get echoed back.
This is a very common technique just to
| | 03:59 |
echo things and also to concatenate
things together.
| | 04:02 |
If we go here and we take a look, you see
we get Hello World two times.
| | 04:06 |
Now, it's not on a new line.
Right, there's nothing here to tell us to
| | 04:09 |
go to a new line, it just echoes one,
then the white space.
| | 04:13 |
This is just HTML white space, it just
says put one space in there.
| | 04:16 |
And then followed by our next command.
If we want to actually have a line
| | 04:20 |
return, well then we need to format it
with our HTML.
| | 04:23 |
We need to have like a br tag.
So, it's a very important point just
| | 04:27 |
because we see a new line here between
our PHP tags doesn't mean it generates a
| | 04:32 |
new line.
We still have to write the HTML to make
| | 04:35 |
that happen.
Alright, next I just want to show you
| | 04:37 |
that we can also generate some numbers.
Let's try doing 2 plus 3 and we'll just
| | 04:44 |
return that back.
Let's first take a look, let's put
| | 04:46 |
another br tag.
Let's take a look at it this way, and
| | 04:49 |
notice that it didn't output anything.
Well, it's because I didn't echo it.
| | 04:53 |
I always need to echo it, it did the
calculation, it did actually add 2 plus
| | 04:59 |
3, but then it just threw away that
value.
| | 05:00 |
It didn't do anything with it, because I
didn't tell it to print it to the screen
| | 05:04 |
using echo.
Now, the one last point that I want to
| | 05:06 |
make here is that the semicolon is a
separator that I've told you, you always need.
| | 05:11 |
It's a good habit to always have in your
PHP.
| | 05:13 |
In this case when it's a one line you
don't actually need it.
| | 05:16 |
Apache is able to tell when it's parsing
the PHP that this is the end of the PHP
| | 05:20 |
because we have an ending tag here.
And it will keep it separate from this
| | 05:24 |
one because it's in a separate tag.
So we don't strictly speaking have to
| | 05:28 |
have the semicolons.
However, I think it's a really good
| | 05:31 |
practice to just always, always, always
use the semicolons.
| | 05:35 |
An that way you won't be tripped up by
the sometimes that you use it and
| | 05:37 |
sometimes you don't.
Just get in the habit of always using a
| | 05:40 |
semicolon, and you'll never run into
problems.
| | 05:43 |
Now that we're able to output dynamic
text, I want us to pause for a second,
| | 05:46 |
and take a look at how the web server
processes these PHP pages.
| | 05:50 |
Because I think that process is going to
be instructive.
| | 05:53 |
We'll do that in the next movie.
| | 05:54 |
| | Collapse this transcript |
| The operational trail| 00:00 |
In this movie, we're going to take a look
at the request-response cycle in PHP.
| | 00:04 |
This cycle describes the way that a
browser and a web server communicate to
| | 00:08 |
process user requests.
Especially if you haven't done web
| | 00:11 |
development before, it can be useful to
see this cycle laid out for you.
| | 00:14 |
To start with, we have our browser, and
then we have the web server.
| | 00:18 |
Our browser makes a request to the web
server.
| | 00:21 |
That's when you type in the domain of the
site.
| | 00:24 |
So let's say that you type lynda.com, it
sends a request to the lynda.com web server.
| | 00:28 |
In our case, that's going to be local
host though.
| | 00:31 |
And on that web server software needs to
be there to intercept that request, and
| | 00:35 |
we have a HTTP Daemon called Apache
that's there.
| | 00:38 |
And Apache see's the request and says oh,
okay, I'm going to see if I have a file
| | 00:43 |
that will help me to respond to that
request.
| | 00:46 |
It then goes to the file system and looks
for a file.
| | 00:49 |
Let's call that file hello.php.
Apache grabs that file and then says,
| | 00:53 |
okay, I see that it has .php at the end.
There may be PHP here that needs processing.
| | 01:00 |
No problem.
I will do that.
| | 01:01 |
So it goes to process the PHP.
In the course of doing that, it may need
| | 01:05 |
to go back and forth to the database a
couple of times.
| | 01:07 |
It may need to request some information
from the database, store information in
| | 01:11 |
the database.
All sorts of things can happen as it goes
| | 01:13 |
back and forth between them.
But, once it's done processing the PHP,
| | 01:18 |
there's one final step where it assembles
the HTML that's going to be returned and
| | 01:23 |
then ships that back to the browser.
And that's the response that the user seeks.
| | 01:27 |
So it's very important that you see this
cyclical process from the browser to the
| | 01:31 |
web server where goes to the file system,
processes the file, goes to the database
| | 01:35 |
as many times it needs during that
process.
| | 01:38 |
And then, finally, generates HTML that's
sent back to the browser.
| | 01:41 |
There's no chance to process PHP after
the HTML has been completely generated.
| | 01:46 |
There's no chance to do it once it's
gotten to the user's browser or anything
| | 01:49 |
like that.
It happens, only in this one spot.
| | 01:52 |
Where we process the PHP, after Apache
goes and finds the file.
| | 01:56 |
This is often also referred to as the
request-response cycle because we're
| | 02:00 |
making a request to the server and it's
responding with something back to us.
| | 02:04 |
So, it's a request-response cycle.
Now, while this may seem simple, I think
| | 02:09 |
it's very important for you to keep this
in your head as a mental picture as we
| | 02:12 |
continue developing PHP.
Because a lot of things we're going to
| | 02:15 |
learn, like header redirection, alpha
buffering, setting cookies, all those
| | 02:20 |
kinds of things, are really going to be
affected by this cycle.
| | 02:23 |
So it's important to have a good firm
understanding of this, right from the start.
| | 02:26 |
| | Collapse this transcript |
| Inserting code comments| 00:00 |
In this movie, we're going to learn how
to insert code comments into our PHP.
| | 00:04 |
We don't want to become simply capable
developers, we want to become good developers.
| | 00:08 |
And in order to do that, we need to learn
about code comments, something that's
| | 00:12 |
very important in any programming
language.
| | 00:14 |
You want to add comments to your code so
that you or someone else coming to look
| | 00:18 |
at the code later.
Can quickly understand what your code is
| | 00:21 |
trying to accomplish, and understand the
approach that you took.
| | 00:24 |
Comments take additional time to write,
but they pay off and save you time later.
| | 00:28 |
In PHP, there are a couple of ways we can
write comments.
| | 00:31 |
We can make single line comments by
simply opening PHP tags, and then putting
| | 00:37 |
in // followed by our comment, single
Line comments are like this.
| | 00:45 |
Or we could use a pound sign.
Or like this but that is less common.
| | 00:50 |
Mostly you're going to see people using
the slash slash in front of it and that's
| | 00:54 |
what I'll be using throughout this title.
Now lets just save that document real
| | 00:57 |
quick and lets go over here and lets
reload this hello world page in our browser.
| | 01:03 |
So there's the page, let's do view source
on it, web developer, and page source.
| | 01:08 |
And you can see the comments do not show
up, right, just the things that I echoed
| | 01:12 |
out, just the output.
So these comments are not output into our HTML.
| | 01:16 |
And they are not processed as PHP,
they're just simply ignored.
| | 01:20 |
They're there for our benefit only.
And I want you to note two things about them.
| | 01:24 |
First of all, this only works inside PHP
tags.
| | 01:27 |
right?
If we did it up here, then that's just
| | 01:29 |
html that we're writing, that's just text
that will be output, so we can't put them there.
| | 01:34 |
And also this technique doesn't work for
double-line.
| | 01:36 |
So for example, if we have double-line
comments that keep going, oh wait a minute.
| | 01:43 |
Notice that TextMate actually helpfully
colored it a different color for me.
| | 01:46 |
The brown text color indicates that it's
a comment.
| | 01:50 |
The blue is indicating that it's going to
be, try and process this as some kind of PHP.
| | 01:54 |
And I'm going to get an error.
Now, we could just put more // in front
| | 01:58 |
of it.
That's certainly valid to do your
| | 02:00 |
comments that way.
But I also want to show you that you can
| | 02:02 |
do double-line comments by putting A
slash with an asterisk after it.
| | 02:08 |
So double line comments are written like
this, so that you can keep typing.
| | 02:17 |
I'll just indent a little bit and typing.
And you can do that all the way until you
| | 02:22 |
type another asterisk, and a forward
slash.
| | 02:25 |
So everything in between these, is all
going to commented out.
| | 02:28 |
It's a little bit like turning on and off
the PHP tags for PHP processing.
| | 02:32 |
We're saying this whole thing is a
comment until you get to the ending
| | 02:36 |
comment marker just assume that we're
still in comment mode.
| | 02:39 |
And this is great because it allows us to
type entire paragraphs describing what
| | 02:43 |
our code does.
And of course one thing I want to a
| | 02:46 |
caution you about is you don't want to
try to put a multi line comment inside
| | 02:49 |
another comment because as soon as you
get to the first ending slash, alright,
| | 02:54 |
even if we have another starting slash,
test.
| | 02:57 |
Right it's going to close it and turn off
commenting mode, they're not nested, so
| | 03:02 |
two turn ons does not equal two turns
offs, right, so as soon as we get to an
| | 03:05 |
off that's it, it's out of comment mode,
and let's save that and then let's just
| | 03:10 |
comeback over here and reload the page
one more time and take a look.
| | 03:14 |
View Source.
And you can see that it doesn't show up
| | 03:18 |
there either.
Comments are super important to writing
| | 03:21 |
good code.
They can be hard to be disciplined about it.
| | 03:25 |
You get caught up in writing the code,
and often you forget to leave yourself comments.
| | 03:29 |
But once you start programming a lot
you'll realize that comments are going to
| | 03:32 |
save you a lot of time, not now, but 10
weeks from now.
| | 03:35 |
When you come back to the code.
You can't remember what you were trying
| | 03:38 |
to do or why you chose a certain
approach.
| | 03:40 |
It's one of those things where investing
a little bit of time in the beginning is
| | 03:43 |
going to save you lots of time later on.
And comments are especially friendly if
| | 03:47 |
there's ever someone else who's going to
be working with your code, because they
| | 03:51 |
don't know what you were thinking.
They don't know The reasoning that you
| | 03:54 |
went through to arrive at a solution to a
certain problem, but your comments can
| | 03:58 |
make it clear to them.
So I'd like you to make a promise to
| | 04:01 |
yourself right now that you're going to
try and put as many comments in your code
| | 04:06 |
as you can.
Especially while you're still learning PHP.
| | 04:07 |
| | Collapse this transcript |
|
|
5. Exploring Data TypesVariables| 00:00 |
Our exploration in the PHP programming
language is going to begin with an
| | 00:03 |
exploration of the different structures
or types that we can use while writing PHP.
| | 00:07 |
And the first of those that we're
going to look at are variables.
| | 00:10 |
Now, if you've done programming before,
then you're familiar with what a variable is.
| | 00:13 |
But if you haven't, then let me start by
explaining that a variable is a symbolic
| | 00:17 |
representation of a value.
You can think of it as a symbol that
| | 00:21 |
refers to something and that's going to
make a lot more sense once we actually
| | 00:25 |
start using them.
But as it's name suggests, it can change
| | 00:28 |
over time or vary.
It has a variable value because it can
| | 00:32 |
point to different values.
Now, in PHP, there's some rules about the
| | 00:37 |
kinds of names that we can give to
variables.
| | 00:39 |
They need to start with a dollar sign,
that needs to be followed by either
| | 00:42 |
letter or an underscore, they can contain
letters, numbers, underscores or dashes.
| | 00:47 |
They cannot contain any spaces and they
are case sensitive.
| | 00:51 |
It makes a difference whether we use an
upper case letter or a lowercase letter.
| | 00:54 |
So let me give you some examples of some
variable names and then we can talk about them.
| | 00:58 |
So I could have item, which is just
$item, and that could be a symbol that
| | 01:04 |
then points to an item.
So maybe it's just number five, right?
| | 01:08 |
Item points to number five.
I could also have Item with an uppercase I.
| | 01:12 |
Either one is perfectly fine, but they
are different.
| | 01:15 |
If I have two of them, if I'm using both
of them, and I have item lower case
| | 01:19 |
pointing to the number 3, and Item upper
case pointing to the number 5.
| | 01:23 |
Then depending on whether I use upper
case or lower case will determine which
| | 01:26 |
one I get back.
So you want to make sure that you're
| | 01:27 |
consistent with your capitalization.
We can also have myVariable with a
| | 01:32 |
capital V.
That is often referred to as camelCase,
| | 01:35 |
because those upper case letters in the
middle are like the humps in a camel.
| | 01:38 |
So, some developers like that format.
Then there's the underscore between
| | 01:42 |
words, so this_variable.
There's also a dash, this-variable.
| | 01:47 |
That's perfectly valid.
You can put numbers in it.
| | 01:49 |
So product3, that's perfectly valid to
have a third product named that way.
| | 01:54 |
You can put underscores in front of it.
Remember, the first character has to
| | 01:57 |
either be a letter or an underscore, so
you could have _book.
| | 02:00 |
You could even have multiple underscores,
$__bookPage.
| | 02:05 |
Now, all of these are valid.
Any of these will absolutely work.
| | 02:08 |
But I want to steer you towards some and
away from others.
| | 02:11 |
The first one is I think we should not
use the hyphenated version of this, and
| | 02:16 |
the main reason why is that, that hyphen
looks like a minus sign, and it looks
| | 02:20 |
like we're subtracting this minus
variable.
| | 02:24 |
And when we start working with variables
and we start working with addition and
| | 02:26 |
subtraction, that could be confusing for
us.
| | 02:29 |
So let's avoid confusion and stay away
from that.
| | 02:31 |
The second one is this multiple
underscores.
| | 02:33 |
Stay away from that as well, because it
makes it hard to tell whether you've got
| | 02:37 |
one or two underscores.
I once worked on a project where another
| | 02:41 |
programmer had written their variable
names using underscores.
| | 02:44 |
And sometimes they had one underscore,
sometimes they had two, sometimes they
| | 02:47 |
had three, and it had significance to
them.
| | 02:49 |
There was a reason why they were using
one, two, or three, but it was really
| | 02:52 |
kind of lost on me as to what the meaning
was behind these, and it was hard to read
| | 02:56 |
to tell whether it was one or two or
three.
| | 02:59 |
The last one is not quite as evil, but I
want to steer you away from it and that
| | 03:02 |
is this single underscore at the
beginning.
| | 03:05 |
The reason it's not evil is that PHP is
going to use it itself.
| | 03:08 |
And we're going to see that.
We're going to see that PHP has some
| | 03:10 |
special variables.
They're named with this underscore at the
| | 03:13 |
beginning, and some developers use this
underscore for special cases.
| | 03:17 |
They want to denote the fact that a
variable has certain access privileges,
| | 03:21 |
that certain people can or can't access
things by putting that underscore in
| | 03:25 |
front of it.
Because it has this special meaning both
| | 03:27 |
to PHP and to other developers.
Let's stay away from it for general use.
| | 03:31 |
Let's go with the other ones.
, ow the one that I'm going to use most
| | 03:34 |
often and the one that I prefer is the
fourth one in this list, this_variable.
| | 03:38 |
And that's what you're going to see me
using throughout this title.
| | 03:41 |
But you can use whichever one you feel
most comfortable with.
| | 03:44 |
Now, you can't just use any single name
that you want.
| | 03:47 |
PHP actually has some words that are
reserved, words that you're not allowed
| | 03:51 |
to use for different things, and it's a
good idea to take a review of this list,
| | 03:55 |
and then stay away from those words as
much as possible.
| | 03:58 |
Sometimes, it's not a problem to use it
for a variable name, but it might be a
| | 04:01 |
problem to use it in other contexts.
It's basically just names with special
| | 04:04 |
meaning to PHP that we don't want to use.
Now, let's try using some variables.
| | 04:09 |
Before we do that though, let's open up
our site's folder.
| | 04:11 |
Let's get all the way inside that site's
folder and let's create a new directory
| | 04:15 |
inside here.
So File > New Folder, and I'm going to
| | 04:18 |
call this sandbox.
I find that it's very helpful to have a
| | 04:21 |
sandbox where we can put all of our
miscellaneous files, like the ones that
| | 04:25 |
we've been working with here.
So I'm going to grab basic.html,
| | 04:28 |
helloworld, my_phpinfo.
I'm going to put all of those inside my sandbox.
| | 04:32 |
This index value, HTML file, I don't
actually need anymore, and I can just
| | 04:36 |
throw that away.
So, all of my files now are inside my
| | 04:39 |
sandbox and this is going to be a little
place where I can try experiments out,
| | 04:42 |
certainly, while I'm learning PHP.
But even after I'm actually developing, I
| | 04:46 |
can just jump back to my sandbox, try
some things out, see if things work the
| | 04:50 |
way I expect, and then switch back to my
application to apply what I learned.
| | 04:53 |
I do this all the time.
So it's really helpful to have this kind
| | 04:56 |
of PHP Sandbox.
Let's now open up our basic.html file,
| | 05:00 |
and let's just do a Save As on that.
And let's save that in our sandbox, as variables.php.
| | 05:06 |
Just make sure that ended up in our
sandbox.
| | 05:10 |
You see there it is.
And notice that it ends in .php.
| | 05:13 |
So now, we're ready to put in some PHP
code.
| | 05:17 |
Just give myself a little room here and
we'll change the name to variables.
| | 05:21 |
Okay, so we're ready to write some
variables inside our PHP code and to
| | 05:25 |
start out with, let's just do a simple
assignment, variable1.
| | 05:29 |
That's the name of our variable, var1 and
then we're just going to say it's equal
| | 05:33 |
to the number 10.
That's it.
| | 05:35 |
We've now made that symbolic pointing
from the variable to the number 10.
| | 05:40 |
Variable 1 now points to 10, and if we
talk about variable 1, we're talking
| | 05:44 |
about 10.
So if we then, in the next line say echo
| | 05:48 |
var1, it's going to echo back 10, right?
Let's save it and just try it.
| | 05:53 |
Go back to Firefox, and instead of
HelloWorld, now, we want, variables.
| | 06:00 |
Nope, I forgot I have to put sandbox in
front of that, because I've now moved
| | 06:04 |
everything to the sandbox.
There we are.
| | 06:06 |
So there's the number 10.
See how that works?
| | 06:08 |
It didn't output the actual string, var1.
var1 is a variable that points to 10, and
| | 06:14 |
of course, this value can vary.
We're just going to take these two lines,
| | 06:18 |
and let's paste it.
And now, let's say it's equal to 100, right?
| | 06:22 |
Let's Save it, and before I output it,
I'm just going to also echo a br tag in
| | 06:27 |
between them.
I can echo HTML just like I can anything
| | 06:30 |
else and it will output that br tag in
between the two of those.
| | 06:34 |
Let's try reloading the page.
Now, we got 10 and 100, so var1 the first
| | 06:39 |
time points to 1 thing.
The second time it points to something
| | 06:42 |
else, because I changed its value.
It varied.
| | 06:44 |
Notice, also, that it's case sensitive.
If I had said that this was equal to, I
| | 06:49 |
was going to echo back var 1 with a
capital V.
| | 06:52 |
I save it.
It comes up and it says oops, we have a
| | 06:56 |
problem here, I don't see that Var1 with
a capital V has been initialized, and I
| | 07:00 |
get a warning message.
This is what a notice looks like in PHP.
| | 07:04 |
So var1 here is different from Var1 with
a capital V there.
| | 07:09 |
Now, variables don't only point to
number, they can point to other things as well.
| | 07:14 |
Also, for example, we could have var2 is
going to be equal to hello world.
| | 07:20 |
And then echo var2.
Save that.
| | 07:27 |
Come back here, reload the page, and you
can see that now the outputs Hello world.
| | 07:30 |
So variables can point to anything
really.
| | 07:32 |
It's just a symbolic representation of
something larger, and it gives you a sort
| | 07:36 |
of handle, an easy way to work with this
larger thing.
| | 07:40 |
And just refer to it by this symbolic
name.
| | 07:42 |
We'll continue working with variables and
text like Hello world in the next movie,
| | 07:47 |
when we talk about the data type called
Strings .
| | 07:49 |
| | Collapse this transcript |
| Strings| 00:00 |
In this movie, were going to be learning
about the data type called Strings.
| | 00:03 |
Strings are a very common feature of most
programming languages.
| | 00:07 |
And we've already been using Strings
without knowing it.
| | 00:10 |
A string is a set of characters.
Those characters can be letters, numbers, symbols.
| | 00:14 |
And their going to be defined inside of
quotation marks, either single quotes or
| | 00:18 |
double quotes.
In non-programming terms, you can think
| | 00:21 |
of a string as being text.
In the last movie, we're working with
| | 00:24 |
hello world.
Hello world is an example of a string, so
| | 00:27 |
is the br tag.
That is a string as well it is defined
| | 00:31 |
inside those quote.
It is a string of text that we can then
| | 00:34 |
work with.
let's take our variable.php page and do a
| | 00:37 |
Save As on it, so that we can create a
new one for strings.
| | 00:40 |
Strings.php saved in our sandbox, we'll
call it strings and I'm going to take all
| | 00:46 |
of the PHP that's in here and let's just
take it all out and we can start fresh
| | 00:51 |
with our strings.
Let's try it with just a simple echo of a
| | 00:54 |
string, right?
Hello World, we saw that before, nice and simple.
| | 00:58 |
We can also put HTML inside of it, right?
That's perfectly valid, that's a string.
| | 01:02 |
You can output it, and it will output the
text, hello world, followed by the HTML
| | 01:07 |
tag br.
We can also do the same thing, but with
| | 01:10 |
single quotes.
I'm just Copy and Paste it, and I'll
| | 01:12 |
replace the double quotes with single
quotes.
| | 01:15 |
In general, I'd like you to start getting
used to using double quotes, though,
| | 01:18 |
instead of single quotes.
And in just a minute, we're going to see why.
| | 01:21 |
Now, we also saw that we can assign
strings to variables, so for example, if
| | 01:25 |
we have greeting equals hello, and let's
have target equals and we'll have world.
| | 01:34 |
And then we can actually put those
together phrase equals the greeting.
| | 01:39 |
And then we learn how to cancatnate with
that dot.
| | 01:41 |
And I'll put another string that has a
single blank space in it.
| | 01:44 |
That's still a string even though it only
has a blank space, and then target.
| | 01:50 |
And last of all lets echo that phrase.
Alright, now that we've got a few of
| | 01:55 |
these, let's bring them up in the browser
and try them out.
| | 01:57 |
Let's go to Firefox, and instead of
variables, we're going to be going to strings.
| | 02:03 |
So there they are.
They all came back just fine.
| | 02:05 |
Hello world, hello world, hello world.
Now, we can also do variable replacement
| | 02:10 |
inside a string.
Let me show you how to do that.
| | 02:13 |
Let's drop down here, I'll do a br tag.
Let's open up a new set of PHP tags, and
| | 02:20 |
let's do that variable replacement down
here.
| | 02:22 |
So, let's say that I have echo and this
time I still have phrase define, right?
| | 02:27 |
The fact that I defined it in a different
block of PHP doesn't matter, this
| | 02:30 |
variable is still set, and inside double
quotes I'm going to tell it to echo
| | 02:34 |
phrase, space again.
Let's go ahead and put br tag at the end.
| | 02:39 |
So, I've now put the variable inside of
the double quotes.
| | 02:45 |
Because it has the dollar sign in front
of it, PHP is going to see it as being a
| | 02:50 |
variable, and it's going to pull up the
value that was here.
| | 02:52 |
Let's try that.
See, hello world again.
| | 02:56 |
So, it dropped in that phrase followed by
again.
| | 02:59 |
Now, this only works with double quotes,
that's why I think it's a good habit for
| | 03:03 |
you to get in using double quotes instead
of single quotes.
| | 03:06 |
Let's just change this real quick, to
single quotes and you'll see what happens.
| | 03:12 |
It just comes back with the literal
phrase again, it doesn't do the substitution.
| | 03:15 |
That only happens with the double quotes.
Now, there's a problem with this, right?
| | 03:19 |
Let's imagine that we have our phrase
here, hello again, but what if we didn't
| | 03:23 |
have a space?
What if we were trying to use a variable?
| | 03:26 |
And then right after, wanted another bit
of text.
| | 03:29 |
Well, if we run this PHP is going to see
that as being one variable.
| | 03:32 |
It's going to look for phrase again as a
variable.
| | 03:34 |
That doesn't exist.
Well, the best way to do these in line
| | 03:38 |
replacements, I'm going to select it all,
and then I'll put curly braces on either
| | 03:42 |
side of it, then I'm going to clearly
differentiate what is the variable from
| | 03:45 |
the non-variable.
When we're inside the double quotes, they
| | 03:48 |
have the special meaning and they allow
us to put variables inside of them, to do
| | 03:52 |
this in place substitution.
You can put slashes in front of it if you
| | 03:55 |
wanted to have the literal curly quotes.
Same thing if you wanted to have literal
| | 03:59 |
double quotes inside of your double
quotes.
| | 04:01 |
You could use a backslash double quote in
front of it.
| | 04:04 |
Backslash escapes the value and let's us
use it.
| | 04:07 |
So let's see what this looks like.
Let's just save it switch back over,
| | 04:09 |
re-load it and see it works here.
Alright, now I was able to still find it
| | 04:13 |
and still do in place substitution.
So, I think if you are going to do in
| | 04:15 |
place substitution, this one is your best
bet.
| | 04:18 |
This one is the one that allows you to
make it really clear what you are doing,
| | 04:22 |
this is a variable that's being dropped
into the string.
| | 04:24 |
So try and use those curly braces.
Of course, it is still just as valid to
| | 04:27 |
do it by concatenating the two together
instead.
| | 04:30 |
And you'll find yourself using both.
Now, that we understand the basics of
| | 04:33 |
strings, in the next movie lets take a
look at some string functions that are
| | 04:36 |
going to be really useful.
| | 04:37 |
| | Collapse this transcript |
| String functions| 00:00 |
We've seen the basics of working with
strings.
| | 00:02 |
Now I want us to take a look at some
functions that we can use with strings.
| | 00:05 |
We haven't looked at a lot of functions
yet.
| | 00:07 |
So far we've really just looked at the
PHP info function and Echo.
| | 00:10 |
Those are really the two main functions
that we've seen.
| | 00:13 |
So we're going to start diving into the
world of functions.
| | 00:15 |
And before we do, I just want to remind
you that the php.net website has some
| | 00:19 |
excellent documentation for functions.
And it will tell you all of the different
| | 00:23 |
functions that are predefined in PHP,
it'll tell you how to use them, has good
| | 00:27 |
user submitted tips for you, all of
that's there.
| | 00:30 |
And you can browse through those if
you're looking for something, you're not
| | 00:33 |
quite sure what it is.
Or if you know the name of the function
| | 00:36 |
you can just type it into the search bar
at the top and it will return the
| | 00:39 |
documentation for it directly.
Alright.
| | 00:41 |
Let's take a look at a few.
So lets start by opening up our
| | 00:43 |
strings.php that we were just working on
before.
| | 00:46 |
And we'll just do save as on that real
quick.
| | 00:49 |
And make this string_functions.php.
That's what we're going to call it string
| | 00:52 |
function and lets take everything out
between all these PHP tags here.
| | 01:02 |
There we go.
Now I have a nice space to to drop in my code.
| | 01:06 |
I'm going to start by just pasting in
some text, so you don't have to watch me
| | 01:09 |
create it.
You can pause the movie if you want to
| | 01:11 |
copy some of this down.
I've got a string that I'm assigning to
| | 01:14 |
the variable first.
The string is, The quick brown fox.
| | 01:17 |
And I've got another string, jumped over
the lazy dog with a space at the
| | 01:20 |
beginning, that I'm assigning to second.
So we've seen how we assigned variables
| | 01:24 |
to strings, like we're doing there.
And we've also seen concatenation.
| | 01:28 |
We've seen how we can just concatenate
these before.
| | 01:30 |
I want to show you another way to do
concatenation real quick.
| | 01:33 |
We're going to do concatenation and
assignment at the same time.
| | 01:36 |
So let's say we have third, it's going to
be another variable.
| | 01:39 |
And third is going to be equal to first.
And then, on a new line it's going to be
| | 01:44 |
third is equal to concatenated equal to
second.
| | 01:51 |
Alright, do you see what it's doing
there?
| | 01:52 |
So it says, alright, first of all third
points to the same thing as first does.
| | 01:56 |
You're going to get that same string and
bring it into third.
| | 01:59 |
Now in the next slide, I'm going to tell
third that it should add on the second
| | 02:04 |
string to the end, because you can
concatenate it to what's already there.
| | 02:07 |
It's like appending it to the end.
The values of first and second haven't changed.
| | 02:11 |
Those values are exactly the same still.
But third now has the complete phrase in it.
| | 02:16 |
This is going to be a very handy way to
write your code if you're trying to build
| | 02:19 |
up a string over time.
We'll definitely use it when we start
| | 02:22 |
working with MySQL later on.
And, of course, I could then say echo $third.
| | 02:27 |
Alright.
So now that we have our third string,
| | 02:30 |
let's take a look at some functions.
I'm just going to drop down here a little bit.
| | 02:34 |
Let's put in a br tag and instead of
opening PHP tags again I'm going to paste
| | 02:39 |
in more text here.
This includes some HTML as well as PHP
| | 02:42 |
tags calling some functions.
So these are the string functions that
| | 02:47 |
we're going to be learning.
The first function that I want us to look
| | 02:52 |
at is stringtolower.
So strtolower, that's the name of the function.
| | 02:56 |
And functions often take something as an
argument.
| | 03:00 |
They take something as input into the
function, and then they return output to you.
| | 03:04 |
So the input in this case, is going to be
inside the parentheses, and it's going to
| | 03:09 |
be our variable third, which is equal to
The quick brown fox jumped over the lazy dog.
| | 03:13 |
So that whole phrase is being sent in to
string to lower.
| | 03:16 |
And you can guess what string to lower
does, it lower-cases it.
| | 03:20 |
So it then returns a lowercase value, and
then, echo, another function, echoes it back.
| | 03:26 |
So we're calling one function, on the
results of another function.
| | 03:29 |
The argument to echo is the results of
string to lower.
| | 03:32 |
Now we have the same thing here with
string to upper, raising it to uppercase.
| | 03:36 |
ucfirst is going to make the first letter
uppercase, and ucwords will make the
| | 03:40 |
words all uppercased.
So let's just try that out, so we can see
| | 03:43 |
what it does.
Let's save it.
| | 03:45 |
Let's go back to Firefox.
And instead of strings, we're now
| | 03:49 |
going to be loading up string_functions.
(SOUND) So there it is.
| | 03:53 |
So you see here's the original phrase
that we echoed the first time.
| | 03:56 |
Here's the lower case version, the upper
case, the upper case first only.
| | 04:00 |
Everything else stayed lower cased and
upper case words for each one gets upper cased.
| | 04:05 |
So, make sure that you understand that
idea before we go on.
| | 04:08 |
Let's just do a few more here.
I'll just put a br tag and we're going to
| | 04:12 |
do length string, len, strlen is going to
tell us the length of the string.
| | 04:18 |
Trim, I'm going to use a different phrase
here, just to illustrate it.
| | 04:21 |
What trim does is it removes the leading
or trailing white space.
| | 04:25 |
Right?
That's spaces.
| | 04:26 |
No matter how many there are, it gets
removed.
| | 04:28 |
So I've got A being concatenated with a
trimmed version of B C D, followed by E.
| | 04:35 |
Alright?
So there should normally, if there was no
| | 04:38 |
trim here, it would be A space B space C
space D space E.
| | 04:42 |
But the trim is going to take out those
spaces.
| | 04:44 |
We'll see that in a second.
Find, which uses strstr.
| | 04:48 |
That's the function name.
The way you can remember that is that
| | 04:51 |
you're finding a string within a string.
That's why it's called strstr.
| | 04:54 |
So inside the string that $third points
to, we're going to look for Brown.
| | 04:59 |
We'll see what that returns.
And then replace by string is going to
| | 05:03 |
replace quick with super-fast inside
third.
| | 05:08 |
So it takes three arguments, right?
So we now have some that takes two arguments.
| | 05:12 |
Another one that takes three arguments.
The order of those arguments is important
| | 05:16 |
and it's something that you have to look
up or know already from the way the
| | 05:19 |
function works.
And you can look those up on the PHP.net
| | 05:21 |
website if you forget them.
The way that string replace works is it
| | 05:25 |
wants the thing you're looking for, the
thing you're replacing it with, and then
| | 05:29 |
the item we're searching inside of.
They call it the haystack often.
| | 05:34 |
We're searching for a needle in a
haystack, well the haystack is third and
| | 05:37 |
the needle we're looking for is quick.
We're looking for quick inside third and
| | 05:40 |
we're going to replace it with
super-fast.
| | 05:42 |
So let's try those real quick and see
what those look like.
| | 05:44 |
Go back, just hit Reload.
So the length of it is 45 characters
| | 05:49 |
long, that's how many letters and spaces
there are in there.
| | 05:52 |
Notice here that the spaces got trimmed
out of b c d, so that there is no space
| | 05:58 |
now between a b and d e.
Find, did find the word brown.
| | 06:03 |
Notice what it returned to us.
It returned everything after that in the string.
| | 06:08 |
So, it found it and the result, what it
returned back, was not just the word
| | 06:12 |
itself, it was everything that follows.
That's the behavior of find.
| | 06:16 |
Again, the documentation will tell us
that that's the way it behaves.
| | 06:20 |
And we might want to slightly modify that
or account for that in our code to handle
| | 06:24 |
it, if we were looking for something like
we want the word that follows brown, well
| | 06:27 |
we then have to apply a few more
functions to it, so that we could get the
| | 06:31 |
word fox from that.
Replace by string, you can see that it
| | 06:34 |
put the super-fast brown fox, so it did
exactly what we would've expected.
| | 06:38 |
Okay.
Let's look at one last set, br.
| | 06:40 |
(SOUND) And we're going to look at
repeating str underscore repeat, we'll
| | 06:45 |
repeat a string.
It's going to repeat third two times,
| | 06:49 |
echo substring substr, that's going to
make a substring from third, starting at
| | 06:55 |
the fifth position to the tenth position
of the string.
| | 06:58 |
And string position is going to tell us
the position of brown, tells us where
| | 07:03 |
brown is located.
Or we can find a character, where is the
| | 07:06 |
character z located, it will return a
number to us telling us where that's located.
| | 07:10 |
These positions can be really helpful for
trying to parse out, we're trying to find
| | 07:13 |
where something is and then make a
substring from it.
| | 07:17 |
Then you can see how we can use a
combination of tell me what the position
| | 07:20 |
is or find it for me and then make a
substring starting at this position going
| | 07:26 |
until whatever position is, you know, at
the ending position.
| | 07:29 |
Let's try those out real quick.
Let's go back, and you see repeat, the
| | 07:31 |
quick brown fox jumped over the lazy dog,
the quick brown fox jumped over the lazy
| | 07:36 |
dog, make substring, you can see where it
grabbed, it grabbed from the fifth
| | 07:40 |
character going forward.
Notice that the fifth character is 1, 2,
| | 07:45 |
3, 4, 5, right?
So that's where it started from the UIC.
| | 07:52 |
And then, find position of brown as in
the tenth position, and then find character.
| | 07:57 |
We were looking for z.
It works the same way that find did, right?
| | 08:01 |
When we were up here, it found brown and
returned everything after it, returning z
| | 08:05 |
returns everything from z going forward
from there.
| | 08:08 |
So these are your first set of functions.
This is the way that PHP is going to work.
| | 08:12 |
We're going to be able to manipulate all
sorts of things by using different
| | 08:15 |
functions, and these are the string
functions.
| | 08:17 |
These are not the only ones.
There are a lot more and I don't expect
| | 08:21 |
that you will have memorized all of
these.
| | 08:23 |
You're going to be looking them up for a
while, so they become second nature to you.
| | 08:26 |
You're going to have to make yourself
some notes.
| | 08:28 |
Keep yourself a little chart, maybe jot
down the ones, you know, that you use
| | 08:32 |
most often.
So that there is a handy reference or
| | 08:35 |
just keep the PHP.net website open, so
that you can quickly go and search and
| | 08:39 |
look up, how you use each of these.
I've been using PHP a long time and I'm
| | 08:43 |
still constantly looking up, the usage of
these and the order of the arguments and
| | 08:47 |
that kind of thing.
Now we've explored strings and string functions.
| | 08:50 |
We're ready to move on and look at
integers.
| | 08:53 |
| | Collapse this transcript |
| Numbers part one: Integers| 00:00 |
Over the course of the next two movies,
we're going to be talking about numbers.
| | 00:03 |
And we're going to start out by first
talking about Integers.
| | 00:05 |
Integers are whole numbers, so that's
numbers like 1, 2, 3, 4, 5, as well as
| | 00:10 |
negative 1, negative 2, negative 3, and
so on.
| | 00:12 |
I think what a number is, is pretty
intuitive.
| | 00:14 |
We all kind of have an idea of that by
now.
| | 00:17 |
But we do still need to see how we work
with them in PHP.
| | 00:19 |
Let's create a new document we can work
with.
| | 00:22 |
I'm going to open up basic.HTML and I'm
going to do just Save As.
| | 00:26 |
I'm going to change this one to be
integers.PHP.
| | 00:29 |
Make sure you've got PHP at the end.
Integers.
| | 00:37 |
Now, we've seen how we could just have
our basic PHP tags and assign a variable.
| | 00:42 |
var1 equals 3, and var2 equals 4.
And we saw how we can add those together
| | 00:48 |
and we can echo back the result.
Let me go ahead and just show you a bit
| | 00:51 |
more math.
We're going to be echoing the result back
| | 00:54 |
from adding 1 plus 2 plus the value of
var1.
| | 00:57 |
That's 3, and then multiplying that by
var2.
| | 01:00 |
This asterisk is what we use for
multiplying when we're programming.
| | 01:04 |
And then, the result of all of that in
parentheses is going to be divided by,
| | 01:09 |
that's what the forward slash here means,
divided by 2 minus 5.
| | 01:14 |
Now, the parentheses, the order of
operations between the multiplication and
| | 01:18 |
division and all that, it's going to
still apply.
| | 01:21 |
It's just basic math that follows the
basic math rules.
| | 01:23 |
So, let's try that out.
Let's open that up in a browser.
| | 01:27 |
Instead of string functions, we're going
to be looking for integers.
| | 01:31 |
And there it is, basic math, the answer
is 7.
| | 01:34 |
So, it added all of those together and
did a calculation for us.
| | 01:36 |
Let me show you a few other math things
that we can do, and that's to use some functions.
| | 01:41 |
So, here's if you want to find absolute
value, you use the abs function.
| | 01:45 |
So, 0 minus 300, that will, of course,
return the absolute value, which would be
| | 01:49 |
300 instead of negative 300.
Exponential, raising something to the
| | 01:53 |
power of something.
The function we're going to use is pow.
| | 01:56 |
So, you see the arguments are 2 and 8, so
that's 2 to the 8th power, and then we
| | 02:00 |
have the square root, sqrt.
The square root of 100, and then fmod is
| | 02:05 |
for modulo.
Now, if you've never worked for modulo
| | 02:08 |
before, what it's going to do is it's
going to take 20 and divide it by 7.
| | 02:12 |
And return just the remainder to me.
What's left over from that, things don't
| | 02:17 |
divide evenly, it will tell us.
That can be very handy for finding out
| | 02:20 |
whether one number divides evenly into
another number.
| | 02:23 |
And then rand, of course, will return a
random number to us.
| | 02:27 |
And rand with a minimum and maximum value
will return a random number within that range.
| | 02:31 |
Let's try all of those out.
Save it.
| | 02:33 |
Back, and here you go.
And you can see the random number it gave
| | 02:37 |
me was a really large number.
Whereas the random min max it gave me was
| | 02:41 |
between 1 and 10.
If we reload that page, you'll see that
| | 02:44 |
it gives me different values for each of
those this time around.
| | 02:47 |
And you can see what I was also talking
about with modulo.
| | 02:49 |
We were dividing 20 by 7, 20 by 7, will
divide into it twice, with 6 left over.
| | 02:56 |
So, the modulo is the remainder that's
left after that.
| | 02:59 |
So, we could know that it does not divide
evenly.
| | 03:01 |
In addition to these functions, I want to
also show you how to increment and
| | 03:05 |
decrement the numbers a little bit.
Let's do a new row here and let's, first,
| | 03:10 |
I want to show you how to do plus equals.
So, we'll do some PHP, and we're going to
| | 03:17 |
say var2 plus equals 4.
Now, what that's going to do is it's
| | 03:20 |
going to update variable 2 in place by
adding 4 to it.
| | 03:24 |
It's the same way we were doing that
concatenation in assignment at the same
| | 03:27 |
time when we were working with strings.
And if the exact same thing as if we had
| | 03:31 |
actually typed out var2 equals var2 plus
4, it just saves us some typing to write
| | 03:37 |
it this way.
Let's echo that value back just so we can
| | 03:40 |
see what it is, var2, we'll put a br tag
at the end.
| | 03:44 |
Now, not only is there the plus version
of this, but there's a minus version, a
| | 03:50 |
multiplication version and a division
version.
| | 03:52 |
So ,we can do all of those as well.
And just so they aren't all exactly the
| | 03:56 |
same, I'm going to change this four to a
three.
| | 03:58 |
So, that'll multiply it by 3 and then
divide it by 4.
| | 04:01 |
Let's try this out real quick.
Save the document.
| | 04:03 |
Switch back.
We'll reload the page.
| | 04:05 |
And you can see they took the last value.
It starts out with a value of four It
| | 04:10 |
adds four to it to get 8, which subtracts
four to get 4.
| | 04:13 |
Multiplies by 3 to get 12, and then
divides by 4 to get 3.
| | 04:17 |
Now, incrementing and decrementing by one
is super common, especially when we start
| | 04:23 |
working with loops.
I'll just show you how that works, so
| | 04:26 |
we're going to call this increments.
And if we want to increment plus equals
| | 04:32 |
1, we could just do it like that.
There is nothing wrong with that and it
| | 04:34 |
might actually be very clear what we're
doing.
| | 04:36 |
But it's so common that we could just do
it as var2++.
| | 04:41 |
That just mean adds one to it.
Increment it.
| | 04:44 |
This actually comes from the world of C.
C uses this when we're incrementing loops.
| | 04:48 |
It's just a nice handy short cut.
We can do the same thing with
| | 04:50 |
decrementing with minus, minus.
So, if you're going through a loop and
| | 04:54 |
you want to keep a variable incremented.
Every time you go through the loop to
| | 04:58 |
count your iterations through the loop,
you can do that by using this plus, plus
| | 05:01 |
or minus, minus.
And we could try it out real quick.
| | 05:04 |
As you would expect, it comes up and
tells us that it went from 3 to 4 and 4
| | 05:07 |
back down to 3.
The last thing that I want to show you
| | 05:10 |
about integers before we move on to
floating point numbers.
| | 05:12 |
Is I just want to make sure that it's
clear to you that there is a difference
| | 05:16 |
between the number 1 and the string 1,
right?
| | 05:20 |
Those are not the same thing.
This is just a character.
| | 05:23 |
It could might as well be x or y or z.
It's just a character on the screen, it's
| | 05:27 |
not a number that's ready to be added.
Now, in truth, PHP will do its best job
| | 05:32 |
to add it together.
If we did something like this, PHP will
| | 05:36 |
say, all right, I realize this is not a
number.
| | 05:39 |
So, it's not suitable for adding, but I
wonder if I can convert it.
| | 05:42 |
I wonder if I can change it into an
integer so that I can complete the operation.
| | 05:46 |
And it will do that.
Let's just see that real quick, and you
| | 05:50 |
see it came up with two.
But if I said this is 1 plus 2 houses,
| | 05:55 |
usually it comes up with 3.
The word houses just disappears because
| | 05:58 |
it converts it.
And the best way it can convert it to an
| | 06:00 |
integer is to pick the number that it see
out of it.
| | 06:03 |
And throw the rest of it away.
And that's the only way that it knows how to.
| | 06:06 |
In general, you should not rely on PHP to
convert strings into integers for you.
| | 06:12 |
You should be working with one or the
other, and you should intentionally
| | 06:15 |
switch from one to the other if you
really need to.
| | 06:18 |
And we'll talk about how to do that later
on.
| | 06:20 |
But, it's considered sloppy programming
to combine types like this without
| | 06:23 |
explicitly converting them to the right
type.
| | 06:26 |
| | Collapse this transcript |
| Numbers part two: Floating points| 00:00 |
Now we've taken a look at integers, I'm
going to to take a look at another type
| | 00:03 |
of number which are floating point
numbers also simply called Floats, for short.
| | 00:08 |
You may know them more commonly as
decimal numbers.
| | 00:10 |
That is numbers that have a decimals in
them followed by a number of significant
| | 00:14 |
digits 2.75 is an example of a floating
point number.
| | 00:18 |
Now, it may seem arbitrary to you if you
haven't done a lot of programming before
| | 00:21 |
that we divide numbers into these two
types.
| | 00:23 |
Integers and floating point, and the
reason why is because computers store
| | 00:28 |
integers and floating points in different
ways in memory.
| | 00:30 |
Mostly because of those significant
digits that we have to keep track of with
| | 00:34 |
floating points.
Because of that, almost every single
| | 00:36 |
programming language has two different
types to work with integers and floating
| | 00:40 |
point numbers.
So let's look at how PHP works with floats.
| | 00:43 |
So, I'm going to start by opening up
basic.html, and then I'll just do a Save
| | 00:47 |
As on that.
And let's just change this to be
| | 00:50 |
floats.php and save it in our sandbox.
We'll call this floating point numbers.
| | 00:57 |
And let's create our first floating point
number here.
| | 00:59 |
So, inside the PHP tags, I'm going to
have echo $float equals 3.14.
| | 01:08 |
That is an example of a floating point
number.
| | 01:10 |
And floating points interact just fine
with our integers.
| | 01:13 |
So for example, if we have our $float
plus 7, we'll echo that back Let's put
| | 01:21 |
some br tags here just so they get them
each on new lines, and we can also divide
| | 01:27 |
two integers together in order to get
floating point numbers.
| | 01:29 |
So 4 divided by 3 doesn't divide evenly,
so as a result we end up with a floating
| | 01:34 |
point number.
Let's try those out.
| | 01:36 |
So I'll switch back over to browser and
we'll go to floats, so there we go, you
| | 01:41 |
see that it echoes the first value, 3.14,
it adds seven, an integer to it, and
| | 01:46 |
gives you a result which is also a
floating point number.
| | 01:49 |
Doesn't matter that we have two different
types, it handles them, they're both
| | 01:52 |
numeric, and so it knows that they can be
added together.
| | 01:55 |
And the result might be an integer or the
result might be a float.
| | 01:58 |
One important point that I want to make
for you, is just that we can't divide by zero.
| | 02:02 |
Just be aware of that.
It does not give us any kind of a
| | 02:05 |
floating point number or anything.
It's an illegal operation, and we'll get
| | 02:08 |
a warning if we try and divide four by
zero.
| | 02:10 |
Let's just try that real quick so you can
see.
| | 02:12 |
Comes up and it gives us a warning, says
oops, there's a warning, can't divide by zero.
| | 02:16 |
Once we learn how to do conditional
statements, we'll be able to check and
| | 02:20 |
see, and have one behavior if something
is zero.
| | 02:23 |
And another behavior if it turns out not
to be zero.
| | 02:25 |
There are some functions that we can look
at with floats that are specific to
| | 02:30 |
floats, and that is basically regarding
rounding.
| | 02:32 |
What do we do about rounding them to a
certain number?
| | 02:34 |
Round, the function round takes a number
that says how many significant digits do
| | 02:39 |
you want there to be?
So, float, 1 will take my float number at
| | 02:43 |
the top, 3.14.
And round it to one decimal place.
| | 02:47 |
Ceiling and floor are also a kind of
rounding.
| | 02:50 |
Except that ceiling always rounds up, and
floor always round down.
| | 02:55 |
It doesn't follow the normal rules of
rounding.
| | 02:57 |
Notice that ceiling is actually ceil,
that that's the name of the function
| | 03:02 |
name, ceil And Floor is the other one.
Let's try both of those out.
| | 03:07 |
Save that document, switch back to
Firefox and reload, and you can see that
| | 03:11 |
it rounded 3.14 to be just 3.1.
It rounded it up to 4 when I used
| | 03:16 |
Ceiling, and it rounded it down to 3 when
I used Floor.
| | 03:19 |
Those are handy tools to have, especially
when something doesn't divide evenly you
| | 03:23 |
can decide whether you want to round up
or round down With the result.
| | 03:28 |
Now with both integers and floating point
numbers, we're able to ask PHP whether or
| | 03:33 |
not something is an integer or whether it
is a float.
| | 03:38 |
We can do that like this.
So for example, I can have PHP, we'll set
| | 03:43 |
integer equal to just 3.
And then I'll just paste in a bit of code
| | 03:47 |
down here.
And it says now, is integer.
| | 03:50 |
An integer is float an integer and then
I'm going to use this method over here,
| | 03:54 |
which is, is underscore int to tell me
true or false, is it an integer or not?
| | 04:00 |
We can do the same thing for float.
Is it a float?
| | 04:07 |
And the method there is of course, is
underscore float.
| | 04:11 |
So it will return true false for each one
of these is an integer or a float.
| | 04:16 |
And there's one more, which is we can use
numeric on both of them, and we'll see
| | 04:21 |
what that gives us.
Numeric is numeric.
| | 04:27 |
Alright, now notice that this is is_int,
is_integer also works, that's an alias
| | 04:30 |
for it but is int is much more common,
that's the one you'll see most people using.
| | 04:36 |
Let's try those out, we'll go over to
firefox, and we'll reload the page.
| | 04:41 |
Is three an integer?
Yes, it responded with true and true when
| | 04:45 |
it's output is a string gets converted
into a one that something will talk to
| | 04:49 |
talk about true and false later on.
Is 3.14 an integer?
| | 04:53 |
No, it's false so it gets converted to
nothing.
| | 04:56 |
So that we see no result there.
Is three afloat?
| | 04:59 |
No, it's not.
Is 3.1 afloat?
| | 05:01 |
Yes, it is both of them though, are
considered numeric.
| | 05:04 |
They both belong to this larger type, of
being numeric.
| | 05:07 |
So this gives us the tools to be able to
know whether something is afloat, whether
| | 05:11 |
it's an integer.
Or even just when it's a number,
| | 05:13 |
regardless of which one of the two types
it is.
| | 05:15 |
These kinds of tests can come in handy.
Now that we understand how both integers
| | 05:19 |
and floating point numbers work in PHP
let's take a look at another type, which
| | 05:23 |
is a raise.
| | 05:24 |
| | Collapse this transcript |
| Arrays| 00:00 |
Arrays are a common feature in many
programming languages.
| | 00:03 |
Arrays are going to be extremely useful
for helping us to keep information organized.
| | 00:07 |
So, what is an array?
An array is an ordered, integer-indexed
| | 00:11 |
collection of objects.
That's a fancy way of saying that we can
| | 00:14 |
take objects, like strings and integers
and put them into a group and then keep
| | 00:19 |
their position in that group in the same
order, so that we can refer to those
| | 00:22 |
objects by their positions.
We can tell the array, give me back the
| | 00:26 |
first object, give me the fifth object,
and so on, because the objects are going
| | 00:30 |
to be indexed according to what position
they hold in the array.
| | 00:34 |
One good way to think about arrays is
that they're like those expanding file
| | 00:36 |
folders that you can buy to put your
bills in.
| | 00:38 |
You might put your electric bills in the
first pocket and your phone bills in the
| | 00:41 |
second pocket, your mortgage payments in
the third pocket, and so on.
| | 00:45 |
You have a preset order and you can put
things in pockets and take them out again.
| | 00:48 |
Put this paper into the third pocket.
Show me the paper that's in the fifth pocket.
| | 00:53 |
That's the way that arrays work.
Now there's no limits to how many pockets
| | 00:56 |
your array can have.
At least none that you're going to need
| | 00:58 |
to worry about.
And a pocket contained anything that a
| | 01:01 |
variable can contain.
A string, a number, even another array.
| | 01:05 |
In fact, a pocket can be just plain empty
and an array can be assigned to a
| | 01:09 |
variable, which makes it easy for us to
reference a large collection of
| | 01:12 |
information by using a single, simple
variable name.
| | 01:15 |
Let's try and example.
Let's create a new page for ourselves.
| | 01:18 |
We'll open up basic.html, do Save As, and
let's call this arrays.php.
| | 01:26 |
I'll change the title to be Arrays and
let's create our first PHP tags here.
| | 01:33 |
Now, the first array that I'm going to
create, I'm going to assign to the
| | 01:35 |
variable: numbers.
Now the way that you define an array is
| | 01:39 |
array() and that's it.
We now have defined an empty array and
| | 01:46 |
assigned it to numbers.
There's no objects inside this array,
| | 01:49 |
it's just empty.
We can put a series of objects in our
| | 01:51 |
array and separate them by using commas.
For example, I'll put in some numbers.
| | 01:55 |
4,8,15,16,23,42.
So now, I have an array that consists of numbers.
| | 02:04 |
And those numbers are always going to
stay in the order that I've put them there.
| | 02:08 |
They're going to stay in that exact
order.
| | 02:10 |
And that way, I can refer to them by
position, when I want to get them back out.
| | 02:14 |
So let's try that.
Let's see how we retrieve something from
| | 02:17 |
an array.
We'll use echo.
| | 02:19 |
We're going to reference the variable
numbers because that's what's pointing to
| | 02:22 |
our array.
It references it.
| | 02:24 |
And then we're going to use square
brackets.
| | 02:27 |
And inside those square brackets, we're
going to provide the index that we want
| | 02:31 |
it to return.
In this case, the index will be the
| | 02:33 |
position that's returned what's in
position one.
| | 02:37 |
Put our semicolon at the end, and let's
bring that up in our browser an take a look.
| | 02:41 |
Go back to Firefox and instead of floats,
let's bring up arrays.
| | 02:46 |
So there it is.
It returned an object to us, and the
| | 02:48 |
object that it returned was 8.
Now, wait a minute.
| | 02:51 |
If you go back and you look, 8 is what we
would've expected was in the second pocket.
| | 02:56 |
And that brings up a very important point
about arrays that we must always keep in mind.
| | 03:00 |
Arrays are numbered starting from zero.
The first pocket is indexed as zero.
| | 03:06 |
The second pocket is indexed as one.
It'll take some getting used to if you
| | 03:10 |
haven't worked with arrays before but
that's how arrays work in almost every
| | 03:13 |
single programming language, not just in
PHP.
| | 03:16 |
So if we change our index to be zero and
then we go back and we reload our page.
| | 03:21 |
We'll get back the first item, which is
4.
| | 03:24 |
So, the first item is indexed with 0.
You really want to make sure you drill
| | 03:29 |
this idea in your head.
First pocket in array, the first item in
| | 03:32 |
array, the first value, is always going
to be referred to by using the index 0.
| | 03:37 |
And as I said, an array can contain lots
of different types of objects.
| | 03:42 |
So, for example, we can have a mixed type
object.
| | 03:44 |
We'll call it mixed, and inside there,
we'll have an array.
| | 03:50 |
And in that array, let's put the number
6, the word fox, followed by the word
| | 03:56 |
dog, followed by another array.
Then in that array we'll put x and y and z.
| | 04:03 |
So you see how that works?
It doesn't matter what kind of objects we
| | 04:11 |
put in there.
It can be integers, it can be strings, it
| | 04:12 |
can even be other arrays.
Any valid type in PHP can go inside an array.
| | 04:16 |
So lets now echo back one of those
values.
| | 04:19 |
Lets do echo from mixed.
And let's ask it for what's in number 2.
| | 04:26 |
So let's put some BR tags in here just to
clean this up a bit; put one up here as well.
| | 04:31 |
And let's also ask it, before we go back
and reload the page, for what is in
| | 04:36 |
pocket number 3.
Now, we know what number 2 should be, right?
| | 04:40 |
because we know that they start counting
at 0, so you can go down the numbers and
| | 04:43 |
say, 0, 1, 2, and figure out what's
going to be in this second position, the
| | 04:48 |
second index.
Let's bring that up in a browser and take
| | 04:50 |
a look and see if you were right.
And it returned dog to us.
| | 04:54 |
Now, notice here, we got a notice, array
to string conversion is what's happening,
| | 04:58 |
and it returned to us Array.
So when we ask for the third item, they
| | 05:02 |
came back and said it's an array.
That's what I see there.
| | 05:06 |
I see an array.
And it used array to string conversion to
| | 05:10 |
try and give us an output for that,
rather than ouput the entire array.
| | 05:13 |
Now, if we wanted to see the entire
array, we can do that.
| | 05:16 |
Let's just try this real quick.
Echo, let's look at the array itself.
| | 05:21 |
See what that gives us?
That comes back and gives us array as well.
| | 05:25 |
(SOUND) Instead, it's really helpful when
you're working with arrays to use print_r.
| | 05:34 |
That stands for print-readable.
And that's a nice way for debugging to
| | 05:40 |
see what's inside of our arrays.
Now, it's not something we would ever
| | 05:42 |
want to show to our users.
It's really just for our purposes.
| | 05:46 |
With, with our array, hopefully, we've
pulled the data out of the array that we want.
| | 05:50 |
And we're not going to run into this
problem.
| | 05:51 |
But let's just do print r on mixed, and
see what that gives us back.
| | 05:55 |
You see?
That's gives us something that's readable now.
| | 05:57 |
It shows us this is an array.
And here's what that array contains.
| | 06:01 |
And you can see the indexes in front of
each one.
| | 06:03 |
This is index 0, this is index 1, and so
on.
| | 06:06 |
Even nicer is if you put the pre tags
around it for the HTML.
| | 06:12 |
(SOUND) And now, look at that.
Now, they're indented and everything.
| | 06:17 |
It makes it nice and easy for us to
follow and see what's inside that array.
| | 06:22 |
Now, what if we wanted to get a value out
of this array?
| | 06:25 |
Right?
So, we're basically looking inside one
| | 06:27 |
array, and then from that, pulling
another value out of that array.
| | 06:30 |
We can do that.
Let's just go up here and copy this line.
| | 06:35 |
We'll bring it down here.
And after we get back, the nested array
| | 06:39 |
which is in position 3, then we'll just
going to ask for what's in position 1
| | 06:43 |
right after it.
Square bracket followed by square bracket.
| | 06:47 |
Let's go take a look.
There it is, it returned Y to us.
| | 06:50 |
See how that works.
I'm just going to get rid of these
| | 06:53 |
warnings up here by just commenting these
lines out.
| | 06:57 |
So we don't hit those anymore.
Now what about adding things to our array.
| | 07:00 |
Alright now we were able to pull things
out of the array but we want to be able
| | 07:04 |
to assign values into it.
We don't want to have to redefine the
| | 07:07 |
whole array each time.
We can put things into pockets by simply
| | 07:11 |
saying mixed, and we'll say at position
two equals cat.
| | 07:16 |
That's it, that's all there is to it.
You just say alright, at position 2, set
| | 07:20 |
this value, put that in there.
It's just a simple assignment operator.
| | 07:24 |
Let's do another one.
I'll just copy this line and let's say
| | 07:27 |
that at position 4, we're going to put
mouse.
| | 07:31 |
Now wait a minute, there is no position
4.
| | 07:33 |
Position 3 was this item here.
So position 4, it's okay to refer to it.
| | 07:38 |
It's going to put an item in that fourth
position for us.
| | 07:41 |
So let's go ahead and take a look and
while we're at it, let's move our print
| | 07:47 |
the array down to the bottom.
And that will allow us to just see
| | 07:51 |
everything that's in there.
So here it is.
| | 07:53 |
See cat up here?
And mouse, down here in the fourth position?
| | 07:58 |
There's also another way.
If you don't know how long something is
| | 08:01 |
and you just want it to go to the end,
you can just make it be blank.
| | 08:04 |
Let's do horse.
And that will put it at the end.
| | 08:08 |
It's appending it to the end.
Whatever the last index is, this is going
| | 08:12 |
to add it to the end of it.
Take a look at that, and so there it is,
| | 08:15 |
it put horse in the fifth position.
So the power of arrays, is that a set of
| | 08:20 |
information can be referenced by a single
variable.
| | 08:23 |
Imagine if we have one thousand email
addresses.
| | 08:25 |
We wouldn't want to create one thousand
variables for those, instead we can
| | 08:28 |
assign all of them to an array, and then
use one easy to reference variable to
| | 08:33 |
pull up each email address by its index.
The other thing that's powerful about
| | 08:37 |
arrays is that they keep their
information in the same order unless we
| | 08:40 |
change it.
Whatever we put in the first pocket stays
| | 08:43 |
in the first pocket.
So, arrays are good for keeping ordered lists.
| | 08:46 |
We can sort those 1000 email addresses
and then they'll be kept in that order
| | 08:50 |
for us by the array.
You can imagine how arrays are going to
| | 08:53 |
stop helping us when we start pulling
records and data out of our database.
| | 08:57 |
We can retrieve 50 customer records
sorted alphabetically by last name and
| | 09:01 |
store them in the array.
And they'll stay sorted in that array by
| | 09:04 |
last name.
The one last thing that I want to mention
| | 09:07 |
to you, is that PHP 5.4, which most of
you are probably running, 5.4 or later,
| | 09:13 |
has a short array syntax.
Instead of writing out the word array, we
| | 09:17 |
can just use square brackets to define
it.
| | 09:20 |
So an array can be equal to 1,2,3.
This doesn't work in 5.3 or earlier.
| | 09:28 |
So because of that, I'm going to stick
with the long notation, so that anyone
| | 09:32 |
taking this training who happens to be
using an earlier version won't get stuck.
| | 09:37 |
But if I were writing PHP for myself,
well, I would definitely use this shorter notation.
| | 09:41 |
So it's up to you which one you want to
use.
| | 09:42 |
Array with the parentheses around it is
probably going to start becoming rarer
| | 09:46 |
over time.
But right now the square bracket is a
| | 09:49 |
little bit new.
So you aren't going to see it in a lot of
| | 09:51 |
older code, especially if they still need
to support older versions of PHP.
| | 09:55 |
But you should be able to see it in both
forms and recognize it as an array.
| | 09:59 |
| | Collapse this transcript |
| Associative arrays| 00:00 |
PHP has another type of array, called an
associative array.
| | 00:03 |
And it's important for us to learn how to
use both types, and to understand the
| | 00:07 |
difference between them.
An associative array is an object-indexed
| | 00:10 |
collection of objects and it's very
similar to what we saw for the definition
| | 00:14 |
of a regular array.
But notice that it doesn't say that it's
| | 00:16 |
ordered anymore.
And instead of being integer-indexed it
| | 00:20 |
is object-indexed, that is, they're
going to be indexed by a label of some sort.
| | 00:25 |
Let's remember the expanding file folder
metaphor that I gave you when we were
| | 00:27 |
talking about arrays.
Associative arrays are similar, but they
| | 00:31 |
work more like hanging file folders.
They're not in any certain order and they
| | 00:35 |
can be rearranged.
Each of those file folders is going to
| | 00:38 |
have a label on it.
And that's how we're going to find information.
| | 00:41 |
Since we won't know what order the
information is in, instead we will thumb
| | 00:44 |
through those file folders until we find
label that matches what we're looking for.
| | 00:48 |
Instead of saying, our phone bill is in
pocket 1, and our electric bill is in
| | 00:52 |
pocket 2, instead we'll say, find the
pocket labeled phone bill and pull out
| | 00:57 |
its contents.
We call the label on each of these
| | 00:59 |
pockets the key, and the contents of
what's inside the pocket the value.
| | 01:03 |
And the combination of the label and the
value is referred to as the key value pair.
| | 01:08 |
So we'll always have a series of keys and
values that make up our associative array.
| | 01:14 |
Both an array and associative array have
their uses.
| | 01:17 |
When the order matters to us we want to
use an array.
| | 01:20 |
When the order is not important, we want
to have the convenience of having a label
| | 01:24 |
that we can refer to, well then we want
to use an associative array.
| | 01:28 |
So if we have 100 customers, we would
want to store them in a ray.
| | 01:31 |
The order of those customers is what
matters.
| | 01:33 |
But then the information about each
customer, I would want to store in an
| | 01:37 |
associate of array.
I don't care that the first item in the
| | 01:39 |
array is their first name.
In fact, I don't want to have to remember
| | 01:42 |
the order in which the data is stored.
Instead I want to be able to ask the
| | 01:46 |
array, give me back the information
that's labelled first name, and retrieve
| | 01:50 |
the data that I'm looking for.
So both of them are going to have their place.
| | 01:54 |
Let's try one out.
So for associative arrays, let's start by
| | 01:57 |
opening up basic.html, we'll do Save As.
We'll call this assoc_arrays.
| | 02:05 |
Assoc is what I'm going to use for short
for association.
| | 02:08 |
Then make sure you put .php at the end.
And here, associative arrays.
| | 02:14 |
Alright.
Now, the function that we're going to use
| | 02:17 |
to create these is going to be exactly
the same as what we had for our regular array.
| | 02:22 |
We're just going to use array with the
parenthesis around it.
| | 02:27 |
That is an empty associative array, as
well as it is a regular array.
| | 02:30 |
There's really not a big difference.
What matters though, is what we put
| | 02:33 |
inside of it.
So, if for example, inside this array, I
| | 02:37 |
put first name as a label, that's a
string, an object, it's object-indexed.
| | 02:43 |
And I point that and I do that using the
equal sign and the greater than sign.
| | 02:48 |
So it looks like an arrow pointing to the
next data, which is the value.
| | 02:53 |
So the key is first name, the value is
Kevin.
| | 02:56 |
And we can have more than one of those.
We can have comma and then last name.
| | 03:01 |
Again, the arrow pointing to Skoglund,
and you can put your own name in there
| | 03:05 |
instead of mine.
There we go.
| | 03:08 |
That is what an associative array looks
like.
| | 03:11 |
Instead of being indexed by the position,
it's going to be indexed by the key.
| | 03:16 |
So if I wanted to get back the first
name, (SOUND) echo, then I would do it by
| | 03:21 |
saying variable assoc square brackets
still.
| | 03:25 |
Still asking for the value using square
brackets, and now I need to put the index
| | 03:29 |
in here.
Still an index.
| | 03:31 |
Before it was an index that was a
position, now it's an index that's an
| | 03:34 |
object, and the object is the string
first name.
| | 03:38 |
Has to match exactly because that's the
label.
| | 03:40 |
I'm looking for first name with that
exact capitalization with the underscore
| | 03:44 |
in between them.
Let's just put a br tag at the end of that.
| | 03:47 |
And let's bring that up in a browser.
Instead of just arrays let's put assoc
| | 03:53 |
underscore in front of it.
And there we go.
| | 03:55 |
It brought back the first name for us.
It looked it up in our array based on its label.
| | 03:59 |
That's what it was indexed by.
And of course, if we wanted to bring up
| | 04:03 |
both first name and last name, we know
how to do that using a pen.
| | 04:07 |
I'll put a space, and then I'll just copy
this, paste it here and turn this into
| | 04:14 |
last name.
Bring that up, and you'll see that it
| | 04:17 |
brings back both values.
Now assignment works the exact same way
| | 04:21 |
that it did when we were working with
regular arrays.
| | 04:23 |
We have assoc and then first name equals
Larry.
| | 04:31 |
That's going to assign Larry to the thing
that's labeled, that's index by first
| | 04:37 |
name, and then let's just echo it again,
so that we can see the results.
| | 04:42 |
You can see now it's changed it and the
value is equal to Larry in there.
| | 04:46 |
Now numbers don't work anymore.
So for example, if we were to come down
| | 04:49 |
here and we were to ask it for what is in
position 0, and let's bring that up.
| | 04:57 |
You say it comes back and says oops,
undefined offset.
| | 05:00 |
Right, basically I don't have an index
called zero.
| | 05:02 |
I'm looking for zero and I don't see it
there.
| | 05:05 |
So that's not going to work.
That is unless you indexed it by using numbers.
| | 05:10 |
Right?
Any number can be there, it doesn't have
| | 05:11 |
to be a string.
We could use a number.
| | 05:13 |
So, in fact, what you'll realize is that
this array that we had numbers, 4, 8, 15,
| | 05:18 |
16, 23, and 42 is exactly the same as if
we had this associative array written out.
| | 05:25 |
0 points to 4, 1 points to 8, and so on.
It's the exact same thing.
| | 05:31 |
Let's just bring that up in a browser.
And you can see that it brought up 4 for us.
| | 05:36 |
It returned this value because it's
indexed with this.
| | 05:38 |
So, in fact, there's not a lot of
difference between an array and an
| | 05:43 |
associative array, except that we have
the convenience of not putting each one
| | 05:49 |
of those numbers in there, when we're
just concerned about the order.
| | 05:52 |
So, you can see that, in fact, an array,
an associative array, actually have a lot
| | 05:56 |
in common.
They're really built the same way inside PHP.
| | 06:00 |
It's just that when we write it in this
form, we aren't providing the indexes,
| | 06:03 |
we're assuming that it can be derived
from the position of the items.
| | 06:07 |
Whereas here, we're actually specifying
what each one is.
| | 06:11 |
And if we wanted to make this one into
20, that's perfectly valid.
| | 06:15 |
We can do that.
We can say that the index of it would be 20.
| | 06:17 |
It's not two anymore, it's 20.
So arrays are going to play a big part in programming.
| | 06:22 |
So it's important that you make sure that
you understand them.
| | 06:24 |
If this didn't make sense at first, then
watch these two movies again and make
| | 06:28 |
sure that you have a good foundation on
what they are and how they work before we
| | 06:31 |
move on.
After that, we'll go to the next movie
| | 06:34 |
where we'll look at some functions that
work on arrays.
| | 06:36 |
| | Collapse this transcript |
| Array functions| 00:00 |
In this movie I want us to talk about
array functions.
| | 00:03 |
An there are a lot of functions for
working with arrays, because arrays are
| | 00:06 |
super useful an we're going to use them
in a lot a different context.
| | 00:09 |
And there's lots a things that we might
want to do with them, and ways we want to
| | 00:11 |
manipulate that information.
So we're going to look at a few of the
| | 00:14 |
most common ones, and then I'm going to
show you how you can look up the rest of them.
| | 00:17 |
Let's start with let's create ourselves a
new file for this.
| | 00:21 |
And let's make our basic .HTML file, Save
As.
| | 00:25 |
We'll call this array_functions.php.
Here we go, ten box/g, array functions.
| | 00:34 |
So, to start with, I'm going to Paste in
a numbers array, which is the same
| | 00:38 |
numbers that we were working with in the
last two videos.
| | 00:41 |
But the difference is that I've jumbled
them all around, they're not in the same
| | 00:44 |
order anymore.
That's because one of the things that
| | 00:46 |
we're going to be looking at is how we
can sort those numbers.
| | 00:49 |
Before we do that, want to look at a
couple of other ones.
| | 00:51 |
The simpliest one of course is just that
we can count those values.
| | 00:55 |
We can use count to tell us how many
items are in the array.
| | 00:59 |
What's the size, what are the number of
items it contains.
| | 01:02 |
There's max which will tell us the
maximum value, and min to tell us the
| | 01:07 |
minimum value.
That's very handy especially if we're
| | 01:09 |
working with max.
And want to make sure that we take
| | 01:12 |
something that's always the maximum of
several choices or the minimum of several choices.
| | 01:16 |
Let's bring those up in a browser and
take a look.
| | 01:18 |
Let's Save that and go back to Firefox,
and, instead of associative arrays, we're
| | 01:22 |
now going to be looking at array
functions.
| | 01:28 |
So, 6 values in there, the maximum value
is 42, the minimum value is 4.
| | 01:33 |
As I said, we can also sort those values.
Let me make this window a little bigger here.
| | 01:38 |
So I'm going to sort them and then right
after I sort them, we'll use print_r to
| | 01:42 |
output it so we can see the result.
So sort and then output it.
| | 01:45 |
And then rsort is for reverse sort, and
we'll output the result of that.
| | 01:49 |
Take a look at what each of those give
us.
| | 01:51 |
So reload this page, so Sort and then
Reverse sort.
| | 01:56 |
If you'd like you can put pre tags around
those to make it easier to read, let's do
| | 02:01 |
that real quick, there we go.
You can see that sort return them in
| | 02:07 |
ascending order while reverse sort return
them descending order by value.
| | 02:11 |
Now there are rules if these aren't
numbers as to how they get sorted.
| | 02:15 |
Obviously if you are sorting a bunch of
strings, you sort them alphabetically by
| | 02:18 |
the first letter, then the second letter
and so on.
| | 02:21 |
If you were sorting an array that had a
mixture of things, it would sort them for you.
| | 02:24 |
But the sort order might not be what
you'd expect, because whether it sorts a
| | 02:28 |
letter before a number or a number before
a letter is a little bit arbitrary.
| | 02:32 |
And you'd have to look that up in PHP to
see which one it was going to do.
| | 02:35 |
So you typically wouldn't do it in that
case, but it would go ahead and handle it
| | 02:38 |
for you.
Notice one thing about sort, which is
| | 02:41 |
that it actually sorted the array in
place.
| | 02:43 |
We didn't do any kind of assignment, we
didn't have to say numbers equals a
| | 02:47 |
sorted version of that, right?
It changed it in place, it's a
| | 02:50 |
destructive function.
So, our old version, our old, unsorted
| | 02:55 |
version, doesn't exist anymore, right?
It sorted them in place.
| | 02:58 |
Another handy function is how we can turn
an array, into a string.
| | 03:02 |
In other words, combine values together
to get a string, and we can do that with implode.
| | 03:07 |
So what we do is we say what array we
want is the second argument.
| | 03:10 |
The first argument is what the separator
is between each of those elements.
| | 03:15 |
So here, I've said it's a space and an
asterisk and a space.
| | 03:18 |
I'll save that, and you'll see what it
looks like.
| | 03:20 |
Let's just do an echo on that so we can
see it.
| | 03:22 |
And you'll see that it imploded it, now
it's 41 star 23 star 16 star, right?
| | 03:30 |
It's not an array anymore, this is a
string.
| | 03:32 |
We could do get type on that and we would
see that it was a string now.
| | 03:36 |
The opposite of Implode is Explode.
So explode is going to take a string like
| | 03:43 |
num_string and every time that it finds
this string, it's going to use it as a
| | 03:48 |
divider between the values.
So every time it sees space astrik space,
| | 03:52 |
it's going to say alright.
That's where I need to split this into a
| | 03:55 |
new object in the array.
So we can see what it does here.
| | 03:59 |
Let's come back, Return, and you can see
that here's the array.
| | 04:03 |
It divided it back up into each of the
values.
| | 04:06 |
It took this string, it said alright 42
is the first value.
| | 04:09 |
This is a separator, twenty three is the
next value, this is a separator and so on.
| | 04:13 |
This is extremely useful when you're
working with something like a comma
| | 04:16 |
separated list, right?
So you have first name comma last name
| | 04:20 |
comma address comma city comma state
comma zip and that sort of thing.
| | 04:24 |
Well you can break that up into an array
by telling it to explode it, based on
| | 04:28 |
where the commas are.
Another useful thing to be able to do, is
| | 04:33 |
to be able to tell whether or not
something is in an array or not, does an
| | 04:37 |
array contain a value?
So 15 in an array, echo, does the array
| | 04:42 |
contain the number 15 inside numbers, and
it's going to return true or false.
| | 04:46 |
Is 19 in the array?
And it's going to return either true or
| | 04:50 |
false for whether 19 is inside the array
numbers.
| | 04:53 |
So in_array is what allows us to do that,
let's Save that.
| | 04:57 |
Try that out.
So,15 in an array?
| | 04:59 |
Yes it is.
True is represented by 1 here.
| | 05:02 |
19 in an array?
No, false.
| | 05:04 |
False is represented by having nothing
there.
| | 05:06 |
Now, as I said, there are many, many
functions for working with arrays.
| | 05:09 |
If you go to the php.net website.
So, php.net/manual, and then your
| | 05:15 |
language, en/ref.array.php.
Here's array functions, and here's a
| | 05:20 |
whole long list of them.
You can see, I can just scroll down the
| | 05:23 |
list here.
There are few that I think are
| | 05:25 |
esspecially interesting.
There is array_keys, that returns just
| | 05:28 |
another array of only the keys from an
accosiative array.
| | 05:31 |
And then there's the same thing for array
values down here.
| | 05:34 |
Returns just the values.
So, if you have a key value pairs, you
| | 05:38 |
can just grab all the keys out of it, or
all the values out of it.
| | 05:42 |
Array_push is an interesting one that
allows you to push an element onto the
| | 05:46 |
end of an array.
Array_pop does the opposite.
| | 05:48 |
It takes an element off the end of the
array.
| | 05:50 |
Array-shift is what you would use to put
an element onto the front of an array,
| | 05:54 |
and array_unshift is what you would use
to take it off of the beginning of an array.
| | 05:59 |
Array_unique will make sure that all of
the items are unique in the array, it
| | 06:04 |
takes out any duplicates.
There's array_search up here, that
| | 06:07 |
searches the array for a given value and
returns the corresponding key if it finds it.
| | 06:12 |
So it's a value search through your
array.
| | 06:15 |
And then array_random is also nice,
array_rand, underscore rand.
| | 06:19 |
This can take one or more random entries
out of an array.
| | 06:22 |
So each one of those you can click
through, you can look for the
| | 06:25 |
documentation on it, you can surf around
in these.
| | 06:27 |
If you're working with an array, and
you're not sure how to accomplish what
| | 06:29 |
you want, this page may provide the
answers that you're looking for.
| | 06:32 |
| | Collapse this transcript |
| Booleans| 00:00 |
In this movie, we're going to look at
Booleans.
| | 00:02 |
A Boolean is a programming type that can
either be true or false.
| | 00:06 |
That's it, one of those two values.
And true is not the string true or even
| | 00:10 |
the number one.
It's just simply the value true.
| | 00:13 |
Booleans are very useful in programming
because we can use them when performing tests.
| | 00:17 |
For example, in the last movie, we used
the in array function to test whether an
| | 00:22 |
integer was inside of an array.
The result was that the test was a
| | 00:25 |
Boolean, true if the integer was present,
false if it was not.
| | 00:30 |
Let's start by creating ourselves a new
workspace for this.
| | 00:32 |
I'll open up basic .html, I'll do Save
As, and we're going to call this Booleans.
| | 00:38 |
Notice how that's spelled Booleans, and
I'll also call it Booleans here.
| | 00:41 |
And let's create our PHP tags.
Now, a Boolean is simply the value true
| | 00:50 |
or the value false.
Notice that there are no quotes around that.
| | 00:54 |
It's not a string, so we could have for
example, result1 equals true.
| | 00:59 |
And then down here, we'll have result2
equals false.
| | 01:04 |
Now what we're doing is assigning a
Boolean to a variable.
| | 01:06 |
We can assign it just like we do anything
else.
| | 01:09 |
And we can take a look at what these
values look like when we echo them back.
| | 01:12 |
I'll just save that, result1 and result2
being echoed.
| | 01:15 |
So, we'll just go to our browser.
And instead of array functions, let's now
| | 01:19 |
load up booleans.php.
Notice that result one comes back and
| | 01:24 |
says one, result2 doesn't have anything.
We've seen this before.
| | 01:27 |
One is what happens when PHP converts a
Boolean true into a string, it turns it
| | 01:32 |
into one.
When it's converting a Boolean false into
| | 01:35 |
a string it has nothing.
It outputs absolutely nothing, not a zero.
| | 01:39 |
But actually no string at all.
Now we can assure ourselves that result
| | 01:43 |
two is in fact a Boolean by calling
another function here called is_bool.
| | 01:47 |
That will tell us whether or not
something is a Boolean.
| | 01:50 |
It's just like we had is numeric earlier,
is float.
| | 01:53 |
Well, we have is bool to find out if
something is a Boolean.
| | 01:56 |
And what is it going to return to us?
A Boolean, but the difference is that if
| | 02:00 |
result2 is a Boolean, it's going to
return true which we'll be able to see
| | 02:03 |
because that'll come back as being a 1.
And notice that I have true and false all
| | 02:09 |
lower case here.
In PHP, you'll often see it written all
| | 02:13 |
upper case TRUE and FALSE, or you'll see
it lower case.
| | 02:16 |
And you can waste a lot of time arguing
with people about the right way of doing
| | 02:20 |
it in PHP.
Everyone has their belief about whether
| | 02:22 |
they should be upper case or lower case.
But in truth, in PHP, they're case
| | 02:25 |
insensitive and you can use which ever
one you prefer.
| | 02:28 |
They're absolutely equal.
I prefer to use the lower case version.
| | 02:32 |
Now, when Booleans are going to be most
useful to us is when we're working with
| | 02:35 |
conditionals which we haven't seen yet.
But the basic idea is in our code we want
| | 02:39 |
to say if one thing is true.
Then, do this bit of code if something
| | 02:43 |
else is true then do this other bit of
code.
| | 02:46 |
Maybe a little sneak peak of conditionals
even though we're not quite ready to dive
| | 02:50 |
into them yet I'll give you a preview.
Let's say that we have number equal to
| | 02:56 |
3.14, and we're going to write an if
statement.
| | 03:01 |
Don't worry too much about the format of
the if statement.
| | 03:04 |
We'll be covering this later.
But, I'm going to check to see whether
| | 03:08 |
number is a float, right?
We saw that is_float earlier, and it
| | 03:11 |
returns a boolean as a result.
So if it returns true, then echo it is a float.
| | 03:19 |
There we go.
Let's just try that out.
| | 03:21 |
And there it is.
You see that it is a float.
| | 03:24 |
And if we change it so it's not a float,
make it a three.
| | 03:29 |
We reload the page, you'll see that it
doesn't execute that code.
| | 03:32 |
So, that's where Booleans becomes really
useful.
| | 03:34 |
We're working with conditionals and we're
going to do a lot of that when we're programming.
| | 03:37 |
So, we'll see more of that when we come
to the conditional section.
| | 03:40 |
For now, just make sure that you
understand the basics of what a Boolean
| | 03:43 |
is and that a Boolean is not a string or
an integer It is its own type.
| | 03:47 |
And it's always either true or false.
| | 03:49 |
| | Collapse this transcript |
| NULL and empty| 00:00 |
In this video, we're going to be talking
about NULL.
| | 00:02 |
NULL is a fancy term for nothing, for not
having a value.
| | 00:05 |
It's not zero.
It's not an empty string.
| | 00:07 |
It's the actual lack of a value.
And if we can set a value into a
| | 00:12 |
variable, we also have to have some way
to talk about the fact that variable
| | 00:15 |
might not have a value at all.
And null allows us to do that.
| | 00:19 |
Let's take a look at it by first creating
a page where we can work with null.
| | 00:23 |
And I'm going to do that by opening up
basic.html, and then choosing Save As.
| | 00:28 |
We're going to save this as null.php.
So, null now, like true and false, the
| | 00:38 |
booleans that we just talked about.
Null can either be written in lower case,
| | 00:42 |
or in all upper case.
And you'll waste a lot of time arguing
| | 00:45 |
with people about the right way to do it
in PHP.
| | 00:48 |
And whether one is better than the other,
or one has better performance than the other.
| | 00:52 |
The truth is that they're case
insensitive and that you can use
| | 00:55 |
absolutely whichever one you prefer.
There is no difference.
| | 00:58 |
I'm going to use the lower case version.
And let's set a variable equal to var1
| | 01:05 |
equal to null.
And just for contrast, let's set var2
| | 01:08 |
equal to an empty string.
Just so that we can see the difference
| | 01:12 |
between those two.
Now, like a lot of the other types that
| | 01:16 |
we've looked at, there are functions that
allow us to test whether it is of a
| | 01:21 |
certain type.
So, we can test whether this is null or not.
| | 01:24 |
is_null will tell us if the variable is
null, and we'll do that to both of these.
| | 01:29 |
Let's also do it to a third one as well.
Make this var3, then we'll test var3.
| | 01:34 |
Now, var3 isn't set, right?
It hasn't been set to anything, so we'll
| | 01:38 |
see what that gives us back.
So we return to Firefox, and set up Booleans.
| | 01:42 |
We'll load it up null.php.
So, it came back and said is variable 1 null?
| | 01:47 |
Yes, it is.
Variable 2, is that null?
| | 01:49 |
No, it's not.
That was my empty string.
| | 01:51 |
So, that's not considered null.
var3, is it null?
| | 01:55 |
I get a notice here that's not an error.
That's not a warning.
| | 01:59 |
It's a notice just saying hey, level
heads up here, you're in the middle of developing.
| | 02:02 |
And I noticed that you're making a
reference to an undefined variable so I
| | 02:06 |
want to call your attention to it here.
Note that these are different than, than
| | 02:09 |
warnings in PHP.
But it goes ahead after that notice and
| | 02:12 |
gives us the result, which is the yes in
fact, var3 is null.
| | 02:16 |
The fact that it hasn't been set means
that it has no value.
| | 02:20 |
So, the answer is true, it is null.
So, notice the difference between that.
| | 02:24 |
Notice what happened with var1 and what
happened with var3.
| | 02:26 |
By setting it specifically to null, I
avoid this notice that pops up.
| | 02:31 |
And we can contrast that with what
happens if we have is set.
| | 02:34 |
Alright, so let me just paste in a few
others here.
| | 02:37 |
var1 is set, var2 is set, or var3 is set.
Is set doesn't have a, a space here, no
| | 02:44 |
underscore between is set, it's all run
together.
| | 02:46 |
And let's just reload the page now.
And you can see that it tells us that
| | 02:50 |
var1 is not set, var3 is not set, and
var2 is set.
| | 02:56 |
Now, not set means that it's null, right?
So being null, we're being told, is the
| | 03:01 |
same thing as being unset.
Now, we know it's not exactly the same thing.
| | 03:05 |
We saw that with is null.
But it's close.
| | 03:08 |
For the purposes of most of our tests, it
will be the same.
| | 03:10 |
So that's what null is, null is just the
lack of having a value.
| | 03:13 |
That's a pretty easy concept.
But I think this is also a good spot for
| | 03:16 |
us to talk about a function in PHP called
Empty.
| | 03:19 |
And as you would expect, empty returns a
boolean, true or false, for whether a
| | 03:23 |
variable is considered to be empty.
What you might not expect though, are the
| | 03:27 |
things that it considers to be empty.
In PHP, empty is going to be an empty
| | 03:34 |
string, null, 0, 0.0.
Meaning, a float in which the value is
| | 03:39 |
zero, a string with a zero inside of it,
false, or an empty array.
| | 03:44 |
All of those things are considered empty.
So, we can provide our test again here,
| | 03:49 |
for whether these things are considered
empty.
| | 03:51 |
And let's define var3 here, just so it
has a value.
| | 03:55 |
var3, it's got a dollar sign in front.
var3 is going to be equal to, let's make
| | 04:01 |
it zero inside the string, like that.
Let's go back over to our browser and
| | 04:06 |
let's try this out.
And let's see what things are empty.
| | 04:08 |
Is var1 empty?
Yes.
| | 04:09 |
var2 empty?
Yes.
| | 04:10 |
var3 is empty also.
So null, an empty string.
| | 04:14 |
And this is a very important one, 0
inside the string are all considered to
| | 04:20 |
be empty.
Now, this broad, catch all approach can
| | 04:23 |
be extremely useful, but you have to use
it with a lot of caution.
| | 04:26 |
For example, imagine that you had a web
form that was submitting a value to your
| | 04:30 |
PHP code.
And imagine that, that field was, how
| | 04:33 |
many children do you have?
You want to check if that field is left
| | 04:36 |
blank by the user.
And if not, you want to say hey, you need
| | 04:38 |
to fill out this.
I need to know how many children you have.
| | 04:40 |
Empty would detect an empty string or no
value being submitted, which is what you want.
| | 04:45 |
But it would also not allow the number of
children you have to be zero as a value.
| | 04:50 |
Which is probably not what you intended,
right?
| | 04:52 |
They would not be able to submit the
value of zero.
| | 04:54 |
Just like we saw here, that would be
considered empty as well.
| | 04:58 |
So most times, you'll need to use further
conditions with empty such as, is it
| | 05:02 |
empty and also not numeric?
Or is it empty but not null?
| | 05:07 |
Now, I'm mentioning this now because
empty is a leading cause of bugs in PHP code.
| | 05:11 |
So, something you're really going to
want to keep an eye out for.
| | 05:14 |
| | Collapse this transcript |
| Type juggling and casting| 00:00 |
During the previous movie we've been
looking at different types in PHPs.
| | 00:04 |
So strings integers floats those are
called types.
| | 00:07 |
And we've seen a few times when PHP
converted a value from one type to
| | 00:11 |
another type for us.
For example we were able to add a string
| | 00:14 |
to an integer and we saw that PHP
converted a Boolean true to be the string
| | 00:19 |
one when it was output to our webpage.
This process is referred to as Type Juggling.
| | 00:24 |
When PHP does it on the fly for us.
We can also explicitly set a type ourselves.
| | 00:30 |
For example, converting a string one into
the integer one.
| | 00:33 |
And when we do it, it's called Type
Casting.
| | 00:36 |
And we can do type casting in two ways.
We can do it using a function, we have
| | 00:41 |
the settype function for that purpose.
And it's just settype and then in
| | 00:44 |
parentheses, two arguments.
The item that's being cast into another
| | 00:49 |
type, comma, and the second argument is
the type that we want to set it to.
| | 00:53 |
So in this case, it's going to either
convert var to be an integer or make sure
| | 00:56 |
that it already is an integer.
Notice the integer is in quotes, it's a
| | 01:00 |
string we're passing in.
The other way that we can type is by
| | 01:03 |
simply writing the name of the desired
type in parentheses before the item being cast.
| | 01:08 |
So for example, parentheses, integer,
closed parentheses, space, and then the
| | 01:13 |
item that we want to cast.
Notice that there's not quotes around
| | 01:16 |
integer in this case.
We're just putting integer right there
| | 01:19 |
between the parentheses.
It's not a string, it is just an
| | 01:22 |
instruction to PHP.
And this is the syntax of how it's done
| | 01:26 |
in the C programming language, that's
where this comes from.
| | 01:28 |
The types that we can use, the values
that we can provide for typecasting, are
| | 01:33 |
the different types that we've seen so
far.
| | 01:35 |
You've got string, integer, or int for
short, float, array, boolean, or bool,
| | 01:41 |
for short.
Now, for some of these, it's going to be
| | 01:43 |
kind of obvious if we're converting an
integer to a float or an integer to a string.
| | 01:48 |
It's pretty obvious the kind of
conversion that it's going to make.
| | 01:51 |
But it's probably less obvious to you if
you're going to convert the number 42
| | 01:55 |
into a boolean or convert an array to a
string.
| | 01:59 |
What that kind of result is going to give
you.
| | 02:00 |
You can try them in an experiment to see,
so you can get a feel for what they do.
| | 02:04 |
Or the php.net website actually has a
list of the rules that explains how it
| | 02:09 |
converts from 1 type to the other.
So you can look those up.
| | 02:12 |
Let's create a page where we can do some
experiements and play with both type
| | 02:15 |
juggling and type casting.
So let's start by opening up basic.html,
| | 02:21 |
and we'll do Save As.
I'm going to call this page
| | 02:24 |
type_casting.php, even though it's
technically going to be for both type
| | 02:30 |
juggling and type casting.
Type casting is probably the more common
| | 02:35 |
term that people use to refer to it,
mostly because that's the thing that we
| | 02:38 |
have control over.
So let's start out by just putting type
| | 02:43 |
Juggling in the html here.
And then, lets put a bit of php in, we'll
| | 02:48 |
define, count is going to be equal to a
string that is 2.
| | 02:52 |
Okay, so, it's equal to a string and we
can find out what type it is.
| | 02:57 |
We can certainly use is string is int,
those kinds of queries that we learned
| | 03:02 |
when we were learning those types.
An even better way to do it is to put
| | 03:06 |
echo gettype.
So we're going to just ask it to get back
| | 03:11 |
the type of count for us.
I'll put a br tag at then end.
| | 03:15 |
So that's going to report back.
Now, we know what the type of that is,
| | 03:17 |
its pretty obvious.
The, the type is a string.
| | 03:20 |
Even though it's a number, it's going to
be a string.
| | 03:22 |
So now, let's do a little bit of PHP.
And let's add count plus equals 3.
| | 03:27 |
We saw this before when we were looking
at integers.
| | 03:29 |
So let's do a gettype again.
And then, let's just bring up that page,
| | 03:32 |
so we can have a look.
Here we are instead of null, I need type_casting.php.
| | 03:38 |
So Type Juggling starts out being a
string.
| | 03:41 |
And then it converts it to an integer.
So count, got converted when we added
| | 03:46 |
plus 3 to it, right?
The type was converted.
| | 03:49 |
It knew, even though you started with a
string, i if I'm going to add these two
| | 03:52 |
things together, I'm going to need to
have integers.
| | 03:55 |
And so it does the conversion for you.
That's type juggling, PHP tries to make
| | 03:59 |
it work out for you.
And we saw that if we had 2 cats for
| | 04:02 |
example, did the same thing.
Reload the page, it still works just fine.
| | 04:07 |
It still goes ahead and adds them
together.
| | 04:09 |
We can output the count and you would see
that it was equal to 5.
| | 04:12 |
And I can also see type juggling going
back the other way.
| | 04:15 |
Let's convert our integer back to a
string by concatenating it with two other strings.
| | 04:20 |
Here I have cats equal to a string,
followed by what is now an integer.
| | 04:24 |
Remember, we ended up with an integer
here, and then another string.
| | 04:28 |
And when we get gettype on that, we'll
see what that returns to us.
| | 04:31 |
Lets bring this up, reload the page, and
it's back to being a string again.
| | 04:34 |
And that's because it said hey, I'm
going to concatenate as string, an
| | 04:38 |
integer, and a string.
Well, if I'm doing concatenation, I need
| | 04:41 |
to turn those all into a string.
And so it juggles back.
| | 04:45 |
To a string for us.
Now I think that this second one, using
| | 04:48 |
an integer to a string is not such a bad
thing to do.
| | 04:51 |
And I think you'll see that happen a lot
in people's PHP code.
| | 04:54 |
Because the conversion from an integer to
a string is super simple, right?
| | 04:59 |
And it's something that we're probably
going to be doing a lot.
| | 05:01 |
However, I think in other cases, like
here, when we're converting a string into
| | 05:06 |
an integer, or, especially if we are
doing more complex conversions.
| | 05:10 |
Maybe you're trying to convert an array
into an integer, right, that seems like a
| | 05:14 |
much more extreme example.
It's sloppy programming to to rely on php
| | 05:18 |
to do the switching for you.
It's much better for you to make the
| | 05:22 |
change yourself.
So, I wouldn't say that you can never do
| | 05:25 |
type juggling.
Because I think you'll find me doing this
| | 05:27 |
kind of thing in my code all the time.
But you won't find me doing this kind of
| | 05:32 |
thing in my code.
I think that's the kind of thing, in my
| | 05:33 |
mind, that would be considered sloppy
programming.
| | 05:36 |
So, how do we make the change ourselves?
Let's give ourselves the br tag here and
| | 05:41 |
then, let's do type casting br.
So we talked about the two ways that we
| | 05:47 |
can do type casting.
One is to use a function so we have the
| | 05:50 |
set type function.
We are going to go ahead and use that
| | 05:53 |
same variable count but this time it
ended up a string here.
| | 05:56 |
Now we are going to change it back into
an integer.
| | 05:58 |
So we can do gettype on that and see what
that is.
| | 06:00 |
So let's just bring that up and try it
real quick.
| | 06:03 |
There you go, cast it back to an integer.
It was a string, here we changed it back
| | 06:07 |
to an integer, the other way that we can
do it is by using that declaration in
| | 06:12 |
front of it, right, string in
parentheses.
| | 06:15 |
This is not a string itself, it's just
the word string inside parentheses, right
| | 06:20 |
before count, and I'm setting count2
equal to that value.
| | 06:23 |
And then we can compare.
We can see what count equals, and what
| | 06:26 |
count2 equals.
So let's bring this up and let's try it.
| | 06:30 |
There we go, now notice count one is
still an integer.
| | 06:33 |
Count 2 is a string.
And this is an important point about the
| | 06:37 |
difference between doing it these two
different ways.
| | 06:39 |
Settype changes it in place.
It actually just effects the variable
| | 06:45 |
right where it is, and changes its type.
It recasts it in place.
| | 06:49 |
This one does not do that and we can if
we take this away, you would see the
| | 06:54 |
difference and you would see it doesn't
recast it.
| | 06:56 |
Let's make that point a little more
explicit.
| | 06:58 |
Let's just make a br tag here and I'll do
php, dollar sign test1 is equal to the
| | 07:05 |
number 3.
And then we'll do that same thing, but
| | 07:08 |
we'll make test2, so that we'll have two
of them that are absolutely identical.
| | 07:12 |
And then, let's set the type both ways.
So let's try the first one, we're
| | 07:17 |
going to use settype and we'll do it to
test1.
| | 07:21 |
And its type is going to be string,
going to explicitly convert it.
| | 07:26 |
Then in the next one, let's just do it by
putting string and then test2, kay?
| | 07:34 |
See what I'm doing?
I'm setting the type both ways here.
| | 07:37 |
Now let's test the type for both of them.
I'll just grab one of these lines up
| | 07:42 |
here, use that as a starting point.
But we're going to call it test1, and
| | 07:47 |
test1 will echo back that value, and then
do the same thing for test2.
| | 07:52 |
Okay, so let's bring that up and let's
take a look.
| | 07:55 |
Reload the page, and you see the first
one was changed to a string.
| | 07:59 |
The second one was not changed.
It's still is an integer.
| | 08:02 |
This change did not last.
It only takes place as it's being used,
| | 08:07 |
in this case up here, as it's being
assigned.
| | 08:10 |
The conversion is happening during
assignment, it's not making a permanent
| | 08:13 |
change to the item itself.
So, so it's important for us, while we're
| | 08:17 |
programming to be aware of what type
something has, is it an integer, or is it
| | 08:22 |
a string?
And then, to use type casting when we
| | 08:25 |
need to convert it to something else.
| | 08:26 |
| | Collapse this transcript |
| Constants| 00:00 |
We started off this chapter by talking
about variables.
| | 00:03 |
So, it's fitting that we're going to end
it by talking about constants.
| | 00:06 |
As you can guess from the name, a
constant is the opposite of a varible.
| | 00:10 |
A vaible can change or vary.
A constant can't change.
| | 00:13 |
It remains constantly set at the same
value.
| | 00:16 |
Constants are going to be recognizable on
PHP because they're always written in all
| | 00:19 |
capital letters and there's no dollar
sign in front of them.
| | 00:22 |
They're also going to differ from
variables in another way.
| | 00:24 |
The only way to set a value for constant
is to use a function.
| | 00:28 |
The define function.
You can't just use the equal sign to
| | 00:31 |
assign a value.
Like you can with variable.
| | 00:33 |
Lets try some experiments.
want to open up basic.html.
| | 00:37 |
And then we'll just do save as.
And we're going to call this constants.php.
| | 00:42 |
Change the title here to constants.
And let's give ourselves a PHP block.
| | 00:50 |
Now, we know how variables work.
Let's say we had max width equals to 980.
| | 00:56 |
That would be a variable set at the
integer 980.
| | 00:59 |
But if was a constant max width would not
have the dollar sign in front of it and
| | 01:03 |
it would be in all capital letters.
So it would be MAX_WIDTH, and if we
| | 01:06 |
wanted to set MAX_WIDTH to 980 we can't
to it by just simply saying 980.
| | 01:13 |
That doesn't work with Constant.
Because we're going to be doing them only
| | 01:17 |
rarely we need to use this special
function to do it.
| | 01:20 |
We're going to use define, and then we're
going to provide the name of the constant.
| | 01:25 |
In quotes, max width.
The second argument then will be the
| | 01:30 |
value that we want to set it to, and
that's going to define max width for us,
| | 01:34 |
now notice that we used the quotes here,
but most of the time when we talk about
| | 01:38 |
the constant we don't use the quotes.
Why don't we use them here?
| | 01:40 |
Well if you think about it...
This is a constant.
| | 01:44 |
But how can we use a constant if it
hasn't been defined yet, right?
| | 01:46 |
So we have to use quotes until it's been
defined.
| | 01:49 |
We're just providing PHP with the string
that we'd like to use for the name of it,
| | 01:53 |
so don't let that trip you up.
Now, once it's been defined, then of
| | 01:56 |
course We can echo back the value and see
that it's been set.
| | 02:00 |
Let's just do that real quick.
I'll save that and we'll switch back over
| | 02:05 |
to Firefox.
And instead of typecasting, we're looking
| | 02:09 |
for constants.
So there it is, 980.
| | 02:11 |
That's the value that's being echoed back
as max width.
| | 02:15 |
Now, we can't change the value.
Let's just see that real quick.
| | 02:18 |
I'll put a br tag.
And then let's open some more PHP.
| | 02:23 |
Let me make a note here.
Can't change the value.
| | 02:27 |
Max width plus equals one.
And then let's echo back max width.
| | 02:35 |
See what we get.
Alright.
| | 02:37 |
Switch back over to our browser.
Reload it.
| | 02:40 |
Oop.
Sorry.
| | 02:41 |
Error.
You can't do that.
| | 02:42 |
It's a syntax error.
We can't do adding to this.
| | 02:46 |
Right.
Max width equals max width plus one.
| | 02:51 |
That doesn't work either.
Let's just switch back and try it.
| | 02:55 |
Nope.
Sorry.
| | 02:56 |
Can't do it.
Let's take both of those out.
| | 02:58 |
Let me show you that you can't even
redefine it.
| | 03:02 |
Alright, so let's just drop down here, we
defined it here.
| | 03:05 |
Let's just change this, and let's say,
can't even redefine it.
| | 03:13 |
So let's say instead of 980, let's make
it now 981.
| | 03:16 |
Let's try and echo that back and see what
we get.
| | 03:18 |
Says oops, sorry.
Constant max width was already defined,
| | 03:23 |
and then what value did it give me back?
980.
| | 03:25 |
It didn't change anything.
It basically just objected to this
| | 03:29 |
completely, said sorry can't do it, and
so max width stayed unchanged.
| | 03:33 |
It stayed constant.
Now constant stays defined for the
| | 03:37 |
duration of the PHP script.
So of course once the And all the values
| | 03:42 |
are unset, then it can be redefined on
the next run of the script.
| | 03:46 |
So think back to that diagram that we had
of the request response cycle.
| | 03:50 |
Right?
When it first comes in and grabs our
| | 03:52 |
script and executes it, it defines the
constant, the constant stays defined
| | 03:56 |
until it returns HTML To the user, but at
that point the constant goes away.
| | 04:01 |
Everything in our PHP goes away at that
point, until the next request response cycle.
| | 04:06 |
So I don't want you to think this value
is forever set in stone.
| | 04:08 |
However, during on execution of your PHP
script, that value is going to be defined
| | 04:13 |
and it is going to be fixed.
Typically, you're only going to use them
| | 04:16 |
for things that don't change, things that
you really want to be hard and fast.
| | 04:20 |
Maybe you're going to define the root
path of the place where you're going to
| | 04:23 |
store images, right?
So you want to have that as a fixed
| | 04:25 |
location on this hard drive.
This is where you can find your image storage.
| | 04:30 |
That's the kind of thing that you would
use a constant for because you're not
| | 04:32 |
going to be varying it.
You want PHP to have a fixed location for those.
| | 04:36 |
So most of the time, you're probably
going to want to use variables.
| | 04:38 |
And you're going to use constants
sparingly.
| | 04:39 |
But they're going to be very useful for
saving data that's fixed.
| | 04:42 |
| | Collapse this transcript |
|
|
6. Control Structures: Logical ExpressionsIf statements| 00:00 |
In this chapter, we're going to be
looking at how we can start to control
| | 00:03 |
the flow of our code.
To have our code make choices about what
| | 00:06 |
should happen based on certain
conditions.
| | 00:08 |
And we're going to do that by using
logical expressions.
| | 00:11 |
The most common of all types of logical
expression, is the if statement,.
| | 00:15 |
This is the basic format of an if
statement in PHP.
| | 00:18 |
We've got if followed by in parentheses
an expression.
| | 00:21 |
And that expression is going to evaluate
to either true or false.
| | 00:25 |
It's going to have a Boolean result.
So if the expression is true, then the
| | 00:29 |
statement that follows it will be
executed.
| | 00:32 |
If it evaluates to false, then the
statement will not be executed.
| | 00:35 |
And the code will skip past it and keep
on evaluating down the rest of the PHP
| | 00:40 |
after it.
So for example, if we have if a is
| | 00:44 |
greater than b, then echo back a is
larger than b.
| | 00:48 |
So if the expression a is greater than b
evaluates to true, then we'll get
| | 00:53 |
something echoed.
If it's not true, then the echo line will
| | 00:57 |
never execute.
PHP will just ignore it and move right on
| | 01:00 |
past it.
Now, this format that I'm showing you
| | 01:02 |
here works if the statement portion
that's going to be executed is just a
| | 01:06 |
single line.
If you have more than one line of code
| | 01:09 |
that you want to execute for the
statement portion, then you'll want to
| | 01:12 |
use curly braces around that statement.
This makes it clear that everything
| | 01:15 |
inside the braces should be executed if
the expression evaluates to true.
| | 01:19 |
It's required for multi line statements
but it's also considered a best practice
| | 01:23 |
to add them around single line statements
too.
| | 01:25 |
It's clearer and I also think it's easier
to develop using one style and just to
| | 01:29 |
stick with it.
So always use curly braces.
| | 01:33 |
Notice also that I've indented the
statement portion.
| | 01:35 |
PHP doesn't care about the indentation.
Remember whitespace doesn't matter to PHP.
| | 01:40 |
But indentation is going to help us a lot
because it's going to improve code readability.
| | 01:44 |
It's also going to help me to make sure
that for every open and curly brace, that
| | 01:48 |
I have a matching closing curly brace.
And we start nesting these inside of each
| | 01:52 |
other, it can sometimes to be hard to
tell, and easy to lose track of them.
| | 01:56 |
So let's start with those two good
programming habits, always using curly
| | 01:59 |
braces, and always indenting our code.
Let's try some if statement for ourselves.
| | 02:04 |
So here I am in my sandbox and I'm just
going to open up basic.html.
| | 02:08 |
I'm going to choose Save As.
Then I'm going to call this logical.php
| | 02:12 |
and save that in my sandbox.
Logical.
| | 02:19 |
So let's just try the example that we
just saw there.
| | 02:21 |
So we have php and in it, we're going to
define a is equal to 4.
| | 02:27 |
And let's have b and have that be equal
to 3.
| | 02:32 |
And then we'll write our statement.
If a is greater than b.
| | 02:37 |
Use my curly braces.
Notice that it indented for me, TextMate
| | 02:42 |
does that helpfully.
Echo a is larger than b.
| | 02:47 |
Okay so that's my statement.
Let's go and try this out and see how it works.
| | 02:52 |
Switch back over to Firefox and I'm going
to be going over to localhost/~kevinskoglund/sandbox/logical.php.
| | 03:07 |
A is larger than b.
Now you can play with those values, and
| | 03:10 |
we can say all right, let's set these in
reverse.
| | 03:13 |
Let's go back, and let's reload the page,
and notice that it doesn't output anything.
| | 03:17 |
It only evaluates what's inside the curly
braces if this statement is true Alright,
| | 03:23 |
simple enough.
Alright, lets try some some, some more
| | 03:25 |
maybe real world examples.
Something a little less experimental.
| | 03:30 |
Lets say that we, have the website and we
want to have a special welcome message,
| | 03:33 |
that displays only to new users of the
website.
| | 03:37 |
So we determine whether someone is a new
user or not, in this case im going to say
| | 03:40 |
that it's true, If that is true, then
we're going to display this welcome
| | 03:45 |
message to them.
So we can experiment with that, you can
| | 03:47 |
try this from true to false and back
again, but let's just go over here, try
| | 03:52 |
it out in our browser, and you'll see
that we get our welcome message, because
| | 03:55 |
it was true.
Another good example, you remember that
| | 03:58 |
we talked about that you can't divide by
0/ That gives you problems?
| | 04:01 |
Well, here's a way to check that.
Our numerator that's 20, the denominator
| | 04:05 |
that's 4 If the denominator is greater
than zero, than it's okay to proceed with
| | 04:10 |
the division.
If it isn't greater than zero, then we
| | 04:13 |
may have a problem here, because this
can't be zero.
| | 04:15 |
Now I realize that this doesn't account
for negative numbers, but you still get
| | 04:18 |
the point, you see what I'm saying, that
it is not letting you divide by zero
| | 04:22 |
before it gives you the result.
Let's just try that one out real quick.
| | 04:25 |
It says no problem, I can go ahead and I
can do that division.
| | 04:29 |
Now, notice that the echo for result is
occurring inside this if statement.
| | 04:34 |
I just want to move that out of there, so
that you see what happens if I do this, alright.
| | 04:38 |
So it doesn't do the division, but it's
going to echo, then, the result here.
| | 04:43 |
Let's go back and see what happens when
we blow that up.
| | 04:45 |
It works just fine.
But if I now make the denominator into
| | 04:48 |
zero, so that it would not execute this
statement, right?
| | 04:52 |
Because now this fails, this is false.
Let's come back over here, and let's
| | 04:57 |
reload the page.
And you'll see, oops, undefined variable.
| | 05:00 |
And the result, no result there.
So notice that the result.
| | 05:04 |
Is being defined in the "if" statement.
That's an important point.
| | 05:08 |
If we're defining variables in the "if"
statement, then we need to make sure that
| | 05:12 |
we account for the possibility that
result is not set, right?
| | 05:16 |
So we're going to start with a result of
0, and then, that way result will still
| | 05:20 |
have a value.
Because this code.
| | 05:23 |
Does not execute at all if that statement
is not true.
| | 05:26 |
So you'll want to watch for that.
Think about the sort of path through your code.
| | 05:30 |
If the if statement doesn't execute, none
of the variables and assignments that you
| | 05:34 |
make in there are going to be available
to you.
| | 05:36 |
Okay, let's go back up here to the top
and you see that we've got this if a is
| | 05:40 |
greater than b.
So if we wanted to account for the
| | 05:42 |
possibility that if a was.
Less than b, we could say a is not larger
| | 05:49 |
than b, right.
And that would work.
| | 05:52 |
However, there's a better way to do that
than to just do a series of if statements
| | 05:56 |
one after the other and we're going to
see how to do that in the next movie.
| | 05:59 |
| | Collapse this transcript |
| Else and elseif statements| 00:00 |
In the last movie, we saw the basics of
an if statement and how we could have a
| | 00:04 |
section of code that only executes if an
expression evaluates to true.
| | 00:08 |
In this movie, we're going to build on
that by talking about else and else if.
| | 00:12 |
That is the ability to have an
alternative that gets executed if
| | 00:16 |
something is not true.
Let's take a look.
| | 00:18 |
So, if we have if a is greater than b, we
want to echo a is larger than b.
| | 00:23 |
We can use else immediately after it to
say if that's not true, then the
| | 00:28 |
alternative version, should be to echo
this section of code.
| | 00:32 |
This statement, echo a is not larger than
b.
| | 00:35 |
So one of these two things will be true.
We've basically divided the path into two
| | 00:40 |
and said, if this is true, do the first
statement, else do the second statement.
| | 00:44 |
Now again, this is without the curly
braces, which is suitable if you have
| | 00:47 |
only a single line of code, but I'm
going to strongly encourage you, even if
| | 00:51 |
you have a single line of code to always
use curly braces.
| | 00:53 |
And if you have multiline code, you'll
definitely want to use them.
| | 00:56 |
I think it's a good habit to be in.
Now I do want you to see that it is a
| | 01:00 |
binary choice between these.
It's going to do one or the other.
| | 01:03 |
Once that first test passes and its
statement is executed, the rest of the
| | 01:08 |
options below that are skipped over.
It jumps to the very end of this whole
| | 01:12 |
statement down to the very final curly
quote, and that's where it picks up and
| | 01:16 |
keeps executing.
So if a is greater than b, echo a is
| | 01:20 |
larger than b, and then, jump down to
that final curly quote and proceed from there.
| | 01:24 |
It doesn't continue checking for
additional options.
| | 01:27 |
Once it finds one, it's done.
It's done with this whole, entire statement.
| | 01:31 |
Now, in addition to just else, we also
have else if, and that lets us string
| | 01:36 |
multiple conditions together.
So we could say, if a is greater than b,
| | 01:41 |
then echo a is larger than b, else if is
a is less than b, echo a is smaller than b.
| | 01:46 |
Notice before I said not larger, because
there's a possibility that the two are equal.
| | 01:50 |
So now I can say else if do another test.
So if the first one matches, then do the
| | 01:55 |
first statement.
If the second one matches, they'll do the
| | 01:57 |
second statement.
If a and b are equal, well then neither
| | 02:00 |
one of these are going to execute.
Right?
| | 02:02 |
Neither one of its conditions were met,
so we'll just keep going from there.
| | 02:05 |
And of course, we can combine all three
of these together, so that if a is
| | 02:09 |
greater than b we echo a is larger than
be, if as is less than b we echo a is
| | 02:14 |
smaller than b, or the final catch-all,
is the possibility that a is equal to b.
| | 02:20 |
It's not greater than, it's not less
than, it must be equal to.
| | 02:24 |
So when we only have two, else is the
alternative version, but if we have
| | 02:28 |
multiple ones, then else is sort of the
default.
| | 02:30 |
It's the final thing, if none of these
other conditions matched up above, this
| | 02:34 |
is the final result that you ought to do.
Let's go back to our page and try those.
| | 02:38 |
So, I'm going to go back to the same page
that I was working on before.
| | 02:41 |
That's the logical.php.
We'll just do it the same page.
| | 02:45 |
Notice before, that I had two if
statements.
| | 02:47 |
If a is greater than b, or if a is less
than be, I can now convert those.
| | 02:53 |
And I could do it by using else and say
else a is not larger than b.
| | 02:57 |
Or, the other way to do it would be just
back up and undo that to change this to
| | 03:02 |
say, else if a is less than b, a is not
larger than b.
| | 03:06 |
Now, that's not quite true, it's a is
smaller b in that case.
| | 03:11 |
And so then, we can add that final
condition, which will echo, a is equal to b.
| | 03:18 |
Now once again, if a is greater than b,
this bit of code gets executed.
| | 03:21 |
After that, it jumps right down to here
and keeps executing our PHP.
| | 03:25 |
If this one's not met, then it goes to
the second one.
| | 03:28 |
If that's not met, then it goes to the
third one.
| | 03:30 |
But after each one of these, after each
determination is made, that block gets
| | 03:34 |
executed and then it jumps down here to
continue with our PHP.
| | 03:38 |
So the combination of these three, the
ability to have if, else if and else,
| | 03:43 |
really gives us lots of control over the
flow of our code.
| | 03:45 |
We can now start to fork our code and say
if the new user is true, well give them a
| | 03:50 |
welcome message.
If they're not a new user, give them a
| | 03:53 |
different message.
If they are a user type of student, give
| | 03:57 |
them one thing, if they're a business
give them something else.
| | 04:00 |
If they're a consumer, give them
something else, and we can start nesting
| | 04:03 |
these inside of each other, as well.
So that if they're a student and they
| | 04:07 |
also are enrolled in a special program,
we give them a different message.
| | 04:10 |
You can really start to see the important
role that logical expressions are
| | 04:13 |
going to play in programming with php.
| | 04:16 |
| | Collapse this transcript |
| Logical operators| 00:00 |
In the last movie, we learned about basic
logical expressions using if else and
| | 00:04 |
else if.
Now, I want us to talk about the
| | 00:06 |
expression part of that, the part that
evaluates to true to false.
| | 00:10 |
And I want us to enhance our ability to
work with that to generate some more
| | 00:13 |
complex expressions.
And to do that, we're going to have to
| | 00:16 |
learn about comparison and logical
operators.
| | 00:18 |
In the previous example, we said if A is
greater than B, or if A is less than B.
| | 00:23 |
Less than and greater than are comparison
operators.
| | 00:26 |
But there are a number of other ones that
we can use as well.
| | 00:29 |
One of the most important ones, is equal.
Are two things equal?
| | 00:32 |
Compare them.
Is A equal to B?
| | 00:34 |
It's made up of two equal signs.
When we're testing if something is equal,
| | 00:38 |
we use it by using two equal signs.
So, when we have one equal sign, we're
| | 00:42 |
doing assignment.
We're assigning a value to something.
| | 00:45 |
When we have two of them, we're comparing
the two.
| | 00:48 |
It's important to keep the two straight.
One is for assignment, two is for comparison.
| | 00:53 |
We also have another version, which is
three.
| | 00:57 |
Together which if for identical.
Now, what's the difference between equal
| | 01:00 |
and identical?
Well, there are some things that are
| | 01:02 |
considered equal because they're roughly
equal.
| | 01:05 |
For example, the number 1, 2, 3 is
considered equal to the string.
| | 01:10 |
One, two, three.
Because if we convert the types, then
| | 01:13 |
they are considered equal but they are
not considered identical.
| | 01:16 |
Identical, they have to be of the same
type as well.
| | 01:19 |
So, it just goes a little bit further in
the check to make sure that they are
| | 01:22 |
absolutely 100% the same.
Most times, you're going to find yourself
| | 01:25 |
just using equal though, with two of
them.
| | 01:28 |
There's also comparison, we saw the
greater than, less than.
| | 01:31 |
There is a greater than or equal to, or
less than or equal to.
| | 01:35 |
Or there's it either less than or greater
than.
| | 01:38 |
And of course, if we're going to do not
equal to, we can also just simply say not
| | 01:42 |
equal using exclamation point equal.
That exclamation point is going to mean
| | 01:47 |
not in PHP.
We're going to see that again later on.
| | 01:50 |
And then, of course, we also have not
identical, which would be not followed by
| | 01:54 |
two equals signs.
Notice that in each case, for equal and
| | 01:58 |
identical, what we're doing is replacing
the first equal sign with that
| | 02:02 |
exclamation point.
Now, in addition to using these to
| | 02:05 |
construct a single expression.
To say is this equal to that or is this
| | 02:10 |
greater than that or this not equal to
that.
| | 02:12 |
We also can combine several of these
together by using some logical operators.
| | 02:17 |
So, for example, for and we can use two
ampersands together.
| | 02:22 |
What we're saying here is if A is greater
than B, and if C is greater than D.
| | 02:27 |
They're testing two different things and
combining them with this &.
| | 02:31 |
Both parts of it have to be true.
If this is true and this is true, then
| | 02:36 |
the whole expression evaluates to true.
We also have or that's made of these two
| | 02:42 |
upright bars, often called Pipes.
Take a second to figure out where that is
| | 02:45 |
on your keyboard.
It's typically right above the Return key.
| | 02:48 |
And this would be a logical or.
So, if a is greater than b, or if c is
| | 02:55 |
greater than d.
And then, of course, as we talked about,
| | 02:57 |
there's not.
So, something is just not true, we can
| | 03:00 |
negate the expression just by putting the
exclamation point in front of it.
| | 03:04 |
It's basically just flips it around to be
the opposite.
| | 03:06 |
Let's try a few example.
Let's start just by opening up
| | 03:09 |
basic.html, and we'll do a save as.
I'm going to call this one operators.php.
| | 03:16 |
And it's going to contain both our
comparison operators and also our logical operators.
| | 03:22 |
Let me just drop down here in the body
and I'll give you the first example.
| | 03:27 |
You can pause them if you need to copy
this down.
| | 03:31 |
I've got a is equal to 4, b is equal to
3.
| | 03:34 |
c is equal to 1 and d is equal to 20.
Notice that those are assignments, using
| | 03:38 |
the single equal sign.
Then, I've got my expression here.
| | 03:41 |
If a is greater than b, notice that I'm
using parentheses around it to group it
| | 03:46 |
together, and if c greater than d.
So, if both of those conditions are met,
| | 03:51 |
this and this, then this entire
expression will evaluate to true.
| | 03:56 |
Because both parts have been met.
So, both trues together mean that the
| | 04:00 |
whole thing is true.
If either one is false, well then it's
| | 04:03 |
not a true statement.
It's not true that both a is greater than
| | 04:06 |
b and c is greater than d.
So, it evaluates to false.
| | 04:10 |
So, let's just try this out.
Let's bring this up in our browser.
| | 04:13 |
And instead of logical, we're going to go
to operators.
| | 04:17 |
And you'll see that nothing is output.
The reason why, a is greater than b.
| | 04:22 |
But c is not greater than d.
So, let's just swap those real quick.
| | 04:25 |
We'll go back and try it again.
There it is, a is larger than b, and c is
| | 04:31 |
larger than d.
So, we can do the same thing with logical
| | 04:34 |
or by just putting in, the or and I'll
just change this as well.
| | 04:39 |
So, now either one is true.
If a is greater than b or c is greater
| | 04:43 |
than d.
If either one of those is true, then it
| | 04:45 |
will work.
Let's just flip this back to what didn't
| | 04:47 |
work before, right?
So now, one of them is true, a is greater
| | 04:51 |
than b.
But c is not greater than d.
| | 04:53 |
Let's go back and reload it.
So a is larger than b, or c is larger
| | 04:59 |
than d.
That's what it output for us.
| | 05:01 |
So, as I said, we can also use some
different comparison operators.
| | 05:03 |
We can say that one thing is equal to
something.
| | 05:05 |
Something is equal to that.
We can feel free to play around with
| | 05:07 |
those, and try different values.
And see what you're going to get back.
| | 05:10 |
Let's try a, a real world example.
I'm going to do one here for if and I'm
| | 05:17 |
going to use the not, right?
The exclamation point, that means not the case.
| | 05:22 |
If isset, and I'll make it e.
If not isset, right?
| | 05:28 |
That's the opposite.
If it's not set, then we are going to set
| | 05:31 |
e equal to 200, then let's just echo back
the value of e.
| | 05:36 |
I'll put a br tag here just so it
separates it out, so let's try this.
| | 05:40 |
Let's see if it inputs something for e.
Switch back 200.
| | 05:45 |
Now, let's experiment here.
Let's set e up here as well.
| | 05:49 |
Let's set e here equal to 100, okay?
Let's go back and try it again.
| | 05:57 |
Now it's equal to 100, because it got
here and it said oh, it's already been set.
| | 06:00 |
So, I don't need to give it a default
value.
| | 06:02 |
This is a good way to set default values.
If something's not set, well then set it.
| | 06:07 |
Let's do another good, real world example
here.
| | 06:09 |
Let's do one with empty.
We talked about empty, and I said you
| | 06:12 |
have to be really careful when you're
using empty.
| | 06:14 |
Let's say that we have a form coming in,
and we don't want to reject zero or 0.0
| | 06:20 |
as being invalid values.
Well, we can say if the quantity entered
| | 06:23 |
is empty and if it is not numeric, then
we say, you must enter a quantity.
| | 06:30 |
But if they've given us empty but it is
numeric, right?
| | 06:34 |
It fails this part of it, then it's
probably a zero or 0.0, and we'll allow
| | 06:39 |
it through.
Let's just try real quick, quantity, and
| | 06:42 |
let's just set that equal to zero.
And we'll add another br tag here just to
| | 06:47 |
separate it out nicely.
There it is.
| | 06:51 |
It didn't respond to it, but if we said
for example that it was a blank string.
| | 06:55 |
Now, we go back rewrote it since you must
enter a quantity.
| | 07:00 |
There's also the powerful combinations
that can be made here.
| | 07:02 |
You can nest different logs.
You want to use parentheses to help you
| | 07:07 |
group those together so that you can keep
them straight.
| | 07:09 |
So, while these comparison operators and
logical operators are really simple in
| | 07:13 |
themselves, they combine to develop some
really powerful expressions.
| | 07:17 |
We can keep stringing them together, we
can nest them inside each other, so that
| | 07:21 |
if all sorts of conditions are met.
If the final evaluation is either true or
| | 07:25 |
false, we'll decide whether or not we
execute some code.
| | 07:28 |
And that's going to be a powerful thing.
| | 07:29 |
| | Collapse this transcript |
| Switch statements| 00:00 |
In this movie, we're going to be looking
at a different kind of logical expression
| | 00:03 |
called Switch Statements.
Switch statements are similar to if statements.
| | 00:07 |
In that they control the flow through our
application.
| | 00:09 |
But they're going to have a different
syntax, some different rules to follow.
| | 00:12 |
And a couple of special gotchas that we
want to make sure that we watch out for.
| | 00:15 |
First, let's look at the basic syntax.
We're going to use the function name,
| | 00:19 |
switch, followed by an argument.
And that argument's going to be a value.
| | 00:23 |
This is the value that we're going to be
testing through each of a series of test cases.
| | 00:27 |
So then in our curly braces we're going
to list off those test cases.
| | 00:31 |
And each one is going to start with case,
and then a test value to that we're going
| | 00:34 |
to test against value to see if their
equal.
| | 00:36 |
And then a colon, and then a statement or
series of statements that we want to execute.
| | 00:41 |
Then following that will be the next case
that we want to test.
| | 00:45 |
So we'll test whether the values equal to
test value two and if it is then we'll
| | 00:48 |
execute those statements.
And then finally we have the option to
| | 00:52 |
provide a default case.
If it doesn't match any of the above
| | 00:55 |
cases we're going to execute the
statements that are defined by default.
| | 00:59 |
So, notice that we're dropping down each
one of those cases, doing a test on each line.
| | 01:03 |
It's very similar if we've written if,
else if, and else.
| | 01:07 |
The big difference here is that we're
testing equality.
| | 01:09 |
We're testing to see, is the value equal
to test value 1, no?
| | 01:13 |
Okay, move down to the next one.
Is value equal to test value 2?
| | 01:18 |
That's a very simple comparison of
equality.
| | 01:20 |
There's no greater than or less than or
anything like that.
| | 01:23 |
And usually, we're not working with
Booleans.
| | 01:25 |
Because a bullion is either true or
false, so there's really not a lot of
| | 01:28 |
test cases.
Where you would see it more is it you had
| | 01:31 |
something like a string that could be
many different things.
| | 01:34 |
For example let's say that our customers
had told us that the way that they wanted
| | 01:39 |
to be contacted.
And they might have said that they wanted
| | 01:41 |
to be contacted by email or by phone or
by some other message.
| | 01:44 |
Text message, Skype, something else, so
we could have a switch statement that
| | 01:49 |
then executed a set of statements.
Based on the way that they told us they
| | 01:52 |
wanted us to contact them.
Now I only provided two case statements
| | 01:56 |
here and a default, but you can have as
many cases as you need.
| | 01:59 |
You could just keep adding different
cases each time.
| | 02:01 |
Now most times you're probably going to
want if else statements.
| | 02:04 |
You are going to use those way more often
than switch statements.
| | 02:07 |
The switch statements are going to be
very useful when you do have this kind of
| | 02:10 |
compact logic.
Where you really are just saying, all
| | 02:13 |
right I want to test a value and that
value can be ten different things.
| | 02:17 |
So I'm going to list off the ten
different things that could happen if
| | 02:20 |
that value is each of those ten cases.
And I'm going to provide code for those.
| | 02:24 |
The parallel structure can be a little
bit easier to read, and to work with.
| | 02:27 |
Okay, now that we know the syntax and the
basic idea, let's try some out.
| | 02:31 |
I'm going to create a new page where we
can work these, I'm going to open up
| | 02:33 |
basic.html and I'll do Save As.
And I'm going to call this switch.php.
| | 02:41 |
Change the title will also be switch, and
let's give ourselves a php area.
| | 02:47 |
Alright, so to start with, let's just
give ourselves a variable.
| | 02:51 |
We'll call it a, and we're going to make
a equal to 3.
| | 02:54 |
And then, let's do a switch statement.
Switch and then send the variable n as
| | 03:01 |
the argument to switch.
So we're going to be testing variable a.
| | 03:04 |
And in the case where it is 0, then we're
going to echo a equals 0, will be our tag
| | 03:12 |
at the end, and semicolon after that.
Okay, now let's take that same thing, and
| | 03:16 |
I'm going to Paste it here.
And let's make our next case, that it's
| | 03:21 |
one, alright?
See what we're doing here, see the
| | 03:24 |
parallel structure?
I'm actually going to Copy both of those now.
| | 03:28 |
Save myself some typing, and we're
going to make this one 2, and this one 3.
| | 03:34 |
I'll change it both here and in the text,
so there we go.
| | 03:37 |
Now we have our cases.
Now remember I could also have a default
| | 03:40 |
case, for now I'm not going to do the
default case.
| | 03:42 |
Let's just do the simple one.
We're going to test whether it's 0, 1, 2,
| | 03:46 |
or 3.
If it's not that, if it's 4, 5 or 6,
| | 03:49 |
well, then none of these will execute.
And we'll just move on right past this
| | 03:52 |
whole statement.
So let's Save it and let's try it out.
| | 03:55 |
Let's go back to Firefox, and instead of
logical, we're going to look for switched.php.
| | 04:01 |
A equals 3, perfect.
See how that worked?
| | 04:04 |
It skipped right past these cases, it
tested them.
| | 04:07 |
Said nope, nope, nope, yes, this one
matches.
| | 04:11 |
So then it executed the statement.
All right let's just try moving a and
| | 04:16 |
making a equal to 0.
Let's see what happens here, Save it.
| | 04:19 |
Let's go back to Firefox and let's reload
the page.
| | 04:22 |
Whoa, wait a minute, a equals 0.
Well that's correct.
| | 04:26 |
It did match that case.
But look what happened after that.
| | 04:28 |
It also came back and told us that it was
equal to 1, 2, and 3.
| | 04:31 |
Which it's obviously not.
And if we put another value in here, like 2.
| | 04:35 |
Go back and reload the page.
You'll see that it comes back and it
| | 04:39 |
executes the first one and every case
that comes after it.
| | 04:42 |
It's an important point about the way
that switch statements work.
| | 04:45 |
They do the statement that matches.
And then they continue to match every one
| | 04:51 |
that comes below it.
Well that's probably not what we wanted.
| | 04:55 |
So if you don't want that behavior, then
you've got to provide a break statement.
| | 05:01 |
So break, break and break, after each
one.
| | 05:04 |
Now when we go back, let's make it equal
to 0 again, Save it, go back to Firefox
| | 05:11 |
and there it is, a equals 0.
That's the behavior that we would expect.
| | 05:15 |
Because we told it, okay, you've done
your statement now break out of the
| | 05:18 |
switch statement.
And that will then drop down here to the
| | 05:21 |
curly brace and keep going from there.
So this break is super, super important
| | 05:25 |
when you're working with switch
statements.
| | 05:27 |
Drill it into your head and you'll save
yourselves a lot of debugging time.
| | 05:31 |
Now as I said, we can have a default case
here as well.
| | 05:33 |
Default and then echo and then a is not
0, 1, 2 or 3.
| | 05:44 |
And we don't have to have a break here,
because it's the last one.
| | 05:47 |
But I'm going to go ahead just to keep
the parallel structure, and also to keep
| | 05:50 |
reminding myself that, that break is very
important to have.
| | 05:52 |
So I'll put it in there, now lets just
try a is equal to 9.
| | 05:56 |
Go back to Firefox, and there it is.
It's not equal to zero, one, two, or three.
| | 06:02 |
So you can see how that works, right?
Now I'm going to set that back to 2, and
| | 06:05 |
let's just drop down here.
And I want to show you another example.
| | 06:09 |
This example is going to output the
Chinese zodiac, the year that it is in
| | 06:14 |
the Chinese zodiac.
So to do that, we're going to do a little
| | 06:16 |
bit of a calculation.
We're going to take the year, we're
| | 06:19 |
going to use 2013, and I'm going to
subtract four from it, and do the modulo
| | 06:24 |
of that, using 12.
That means that I'm finding the remainder
| | 06:28 |
once we divide it by 12.
So after we divide it by 12 evenly, how
| | 06:32 |
many are left over?
So that end result is going to be a
| | 06:36 |
single value.
So even though I've got a calculation in
| | 06:38 |
here, it's a single value that comes
through in the end, and that's what I'm testing.
| | 06:42 |
So whatever that value is, whatever that
remainder is it will check.
| | 06:46 |
If it's 0, then we are in the first year
of the 12 year cycle, so it is the Year
| | 06:51 |
of the Rat.
If it's a one, well then we know we're in
| | 06:54 |
the next year and the year is the Ox and
so on.
| | 06:56 |
What I want to show you here, though, is
that white space doesn't matter.
| | 06:59 |
Do you see the difference here?
Up here, we had each one of these on a
| | 07:03 |
seperate line.
Case 1 echo break, case 2 and so on.
| | 07:08 |
Here I've got them all one after another,
very compact, very easy to read.
| | 07:13 |
I've even added in some extra spaces here
in front of the numbers to keep it all
| | 07:17 |
lined up nice, tight, easy to read.
I've still got my colon.
| | 07:20 |
I've still got my semicolons to separate
the commands and I've still got break,
| | 07:23 |
but the white space doesn't matter.
Alright just so we can see that let's go
| | 07:27 |
ahead and bring that up in a browser.
I'll put a br tag here at the end.
| | 07:31 |
It pops up and it says 2013 is the year
of the snake.
| | 07:35 |
Now there's one last example that I
want to show you, I'm going to Paste in
| | 07:38 |
another example here.
Imagine that we have a user type.
| | 07:40 |
Let's go ahead and set a user type so we
don't get an error.
| | 07:42 |
User type, and the user type is going to
be equal to customer.
| | 07:48 |
Right it could be customer it could be
student.
| | 07:50 |
It could be press it could be something
else.
| | 07:53 |
Well if that user type is student then
we're going to echo back welcome.
| | 07:58 |
If its press then we're going to say
greetings and if its customer then we're
| | 08:02 |
going to say hello to them.
What I want to show you is what if you
| | 08:05 |
had multiple cases that you wanted to
match on something.
| | 08:08 |
What if you wanted to have press and
customer respond to the same set of statements.
| | 08:13 |
I mean we could say Hello and just do it
there but this is a simple example
| | 08:16 |
imagine if this was in fact twenty lines
of code, right?
| | 08:21 |
So, there's twenty lines of code, am I
going to really repeat that twenty lines
| | 08:23 |
of code again?
No, I don't really want to do that.
| | 08:25 |
Now, this is the thing I want to caution
you about.
| | 08:29 |
You may be tempted to do something like
this, and to say case press customer,
| | 08:34 |
with commas between them.
That's how you do it in some other languages.
| | 08:37 |
It's not how you do it in PHP, though.
In PHP, if you want them to have the same
| | 08:42 |
one, well, guess what.
You just put them one after another.
| | 08:45 |
Because what happens when press matches.
It executes the statements that are here
| | 08:49 |
and then all the statements that are
below it until it gets to a break.
| | 08:54 |
So press customer is going to do the same
set of statements because of the way the
| | 08:59 |
switch works.
You can see now why the switch statement
| | 09:01 |
behaves the way it does and why it needs
you to put in that break some of the times.
| | 09:05 |
Because there are cases when you're not
going to use the break when you have
| | 09:08 |
multiple values like this.
So we could have press, we could have
| | 09:11 |
customer, we could have case, when it's
admin, right?
| | 09:15 |
We can just add more cases.
And each one of those is going to do this
| | 09:20 |
block that's below it.
So, let's save it and let's just try it.
| | 09:23 |
Let's go down here, we reload the page
and there it is.
| | 09:27 |
Hello is what we get back as the result.
That is the statement that goes with the
| | 09:30 |
case for customer.
So, as I said, you're going to use if
| | 09:33 |
statements a lot more than your going to
use switch statements.
| | 09:35 |
But if you look up here it's something
like this, where you have the Chinese zodiac.
| | 09:39 |
This is the classic case where you're
going to want to use a switch statement.
| | 09:43 |
Writing if else statements for all of
this just doesn't give you the same
| | 09:46 |
legibility and ease of maintaining the
code as a switch statement does.
| | 09:50 |
| | Collapse this transcript |
|
|
7. Control Structures: LoopsWhile loops| 00:00 |
In the previous chapter we looked at the
first type of control structure, logical
| | 00:03 |
expressions, which let our code make
choices about how to proceed.
| | 00:07 |
In this chapter, we're going to be
looking at the other main type of control
| | 00:09 |
structure, loops.
Loops allow us to write code that will
| | 00:12 |
execute more than once, without having to
write out all of that code over and over again.
| | 00:16 |
The code executes once and then it loops
back to the start to execute again.
| | 00:21 |
And each time the code executes, it
doesn't necessarily do the exact same thing.
| | 00:25 |
The values of variables may be changed
each time it goes through the loop.
| | 00:28 |
So that logical expressions inside the
loop do different things.
| | 00:31 |
And of course, a critical feature of any
loop is knowing when to stop looping.
| | 00:36 |
We're going to take a look at three types
of loops.
| | 00:38 |
While loops, for loops, and foreach
loops.
| | 00:42 |
We're going to begin with while loops.
I think these are the simplest.
| | 00:45 |
The syntax is simply while followed by an
expression, that's the argument to it and
| | 00:50 |
then a statement.
The statement is the loop, the thing
| | 00:52 |
we're going to do over and over again,
while the expression evaluates to true.
| | 00:56 |
Now with a single line statement this
will work.
| | 00:59 |
But most times you're going to have a
multi-line statement and I also think
| | 01:03 |
it's just a good practice to always use
those curly braces.
| | 01:06 |
It makes it very clear what's inside the
loop and what's not.
| | 01:10 |
Then notice how similar the syntax is to
our if statement.
| | 01:12 |
In fact if you swapped out the word while
for if, it'd be exactly the same.
| | 01:16 |
And the expression part works exactly the
same way.
| | 01:19 |
It's going to be a Boolean expression
that's going to evaluate to either true
| | 01:22 |
or false.
So what happens is it says, while
| | 01:25 |
expression is equal to true.
Do this block of code.
| | 01:29 |
Start at the top and do all of these
statements and when you get to the
| | 01:31 |
bottom, come back to the top again and
reevaluate the expression.
| | 01:36 |
Is it still true?
If it is, go through and do all the
| | 01:38 |
statements again 'til you get to the last
curly brace.
| | 01:41 |
Then back to the top.
Evaluate that expression again.
| | 01:43 |
Once it evaluates to false well, now we
don't do it anymore, because we're only
| | 01:47 |
doing it while it's true.
So now, the code jumps back down to the
| | 01:51 |
curly brace, it does not execute that
final time, and we move on with the rest
| | 01:56 |
of our PHP code from there.
Let me give you an example.
| | 01:58 |
So let's say that we have while, the
variable count is less than or equal to 10.
| | 02:04 |
So as long as that's true, as long as
count is less than or equal to 10, we're
| | 02:08 |
going to echo the count and then add 1 to
it.
| | 02:11 |
Then we'll go back to the top, and we'll
check again, is count less than or equal
| | 02:14 |
to 10.
And if it is less than or equal to 10, it
| | 02:17 |
will echo the count and then add 1 to the
count and go back to the top again.
| | 02:22 |
So you can see what's going to happen
here.
| | 02:23 |
Let's say the count started out being 0
well, the first time through, count is
| | 02:27 |
less or equal to 10, so it executes.
This time count is equal to one, still
| | 02:32 |
less or equal to 19, so the loop
executes.
| | 02:35 |
It keeps on going until finally count is
equal to up to 11.
| | 02:38 |
When count is equal to 11, it's no longer
less than or equal to 10.
| | 02:42 |
It's false.
And at that point it stops executing the loop.
| | 02:46 |
Now, two things, about the loop are
critical.
| | 02:48 |
The loop needs to have, a condition, an
expression, that will allow it to execute.
| | 02:53 |
Number two something about the condition
has to change during the course of the loop.
| | 02:59 |
If not you'll end up getting an infinite
loop.
| | 03:02 |
Let me show you what I mean.
Lets say that we didn't have count plus
| | 03:04 |
equal 1 in there.
We just took that line out.
| | 03:07 |
Well now the first time through lets say
count is equal to three.
| | 03:11 |
Three is less than or equal to 10 so
we'll echo three.
| | 03:14 |
Go back to the top of the loop, 3 is less
than or equal to 10, so we'll echo 3.
| | 03:19 |
It'll just keep going and you'll just
keep outputting 3's over and over and
| | 03:22 |
over again.
But we have to have some relationship
| | 03:24 |
between what's happening inside the loop
and the condition that's above it.
| | 03:28 |
Something has to eventually change so
that now the behavior of the loop will be
| | 03:32 |
triggered and we won't just be going
infinitely through it.
| | 03:36 |
Let's try some examples for ourselves.
So to start with, I'm going to open
| | 03:39 |
basic.html, just some simple html that
I've created, and I'll do save as.
| | 03:44 |
And we're going to call this
whileloops.php.
| | 03:50 |
I'll call it loops, while.
So, let's just try our very first loop here.
| | 03:55 |
And let's do the example that we just
had.
| | 03:57 |
Let's do count equals zero, and then
while and then our expression.
| | 04:02 |
You could have the space here or not.
And then while count is less than or
| | 04:07 |
equal to 10.
And then inside our curly braces, what do
| | 04:11 |
we want to do each time?
So until count gets to 10 we're going to
| | 04:15 |
echo count, I'm going to go ahead and put
a comma after it.
| | 04:20 |
Cartenate the comma with a period, and
then I'm going to do count and I'm
| | 04:24 |
going to use plus plus just to show you.
Remember on the slide I did it using
| | 04:28 |
count plus equals 1.
Well count plus plus does the exact same
| | 04:32 |
thing, it's just increments count.
Remember that?
| | 04:35 |
So, I'll just make a note here Increment
by 1 just so we don't forget.
| | 04:41 |
Now let's save it and let's try it out.
Let's come back over here to Firefox and
| | 04:45 |
I'm going to go to local host, and for
me, it's going to be Kevin Skoglund.
| | 04:49 |
And then in my sandbox, and then I just
made this one called whileloops.php.
| | 04:56 |
So, there it is.
See what I got back?
| | 04:58 |
The value started out as 0.
Let's just bring these up together so we
| | 05:01 |
can see them at the same time.
So, the first time through count was 0.
| | 05:07 |
So, it executes, count gets output.
Now, count becomes 1.
| | 05:12 |
Goes back to the top of the loop.
Still meets the condition, still, the
| | 05:16 |
expression evaluates to true.
So it outputs it again, keeps incrementing.
| | 05:20 |
Finally, let's look at the top.
When we get to nine, it's, nine is less
| | 05:24 |
than or equal to 10, so it outputs, it
becomes 10.
| | 05:28 |
10 is less than or equal to 10, so it
outputs the 10, and then it becomes 11.
| | 05:35 |
It goes back to the top.
Now count is equal to 11, which is not
| | 05:40 |
less than or equal to 10.
So, it does not execute this last time,
| | 05:43 |
it drops right down to here.
And just to make sure that you see this,
| | 05:47 |
let's do echo br.
And echo, count colon then let's output
| | 05:57 |
the value of count here.
So, after the loop is completely done,
| | 06:00 |
let's take a peak at what count is at
that last time.
| | 06:03 |
See, there it is, it's equal to 11 after
the loop is done.
| | 06:08 |
Now, as I said, going through a loop
doesn't have to do the same thing each
| | 06:11 |
and every time.
I mean obviously the values did change
| | 06:14 |
and it went from 0 all the way up to 10,
which was nice.
| | 06:17 |
We had one little bit of very simple code
that actually did something different
| | 06:21 |
each time through the loop.
We can do even more than that, let's do
| | 06:23 |
an If statement.
Let's say, if the count is equal to 5,
| | 06:30 |
then I'll put my curly braces.
And I'm just going to go ahead and do else.
| | 06:34 |
And I'm going to copy this and paste it
in here.
| | 06:38 |
Alright.
So, if it's not equal to five.
| | 06:40 |
The else statement is, it's going to do
what it was doing before.
| | 06:43 |
But for, if it's five, let's have it echo
the word, five, with a comma after it.
| | 06:50 |
Save it and let's come back here and
we'll reload and you'll see that it pops
| | 06:54 |
up and it does something different when
it gets to 5.
| | 06:56 |
This is a very simple example, but if you
think for a second, you can see how it
| | 06:59 |
would be applicable.
How you would say, alright if I'm
| | 07:02 |
outputting a set of pictures every fifth
picture, I want to put in a different HTML.
| | 07:08 |
Maybe you want to do a line break after
something, or you want to finish a UL
| | 07:11 |
tage and start a new one.
Now, I just want to point out what I was
| | 07:14 |
talking about with having a relationship
between what happens in the loop and
| | 07:17 |
making sure that it will exit.
If we were to take this count plus plus,
| | 07:21 |
imagine that we moved it up here.
Imagine that it was inside that alt statement.
| | 07:25 |
Do you see what would happen?
It's going to loop infinitely, because
| | 07:27 |
what's going to happen, it's going to get
to five, and then when it gets to five,
| | 07:30 |
it's going to stop incrementing.
It's just going to keep looping, and
| | 07:34 |
looping, and looping, and looping.
So, it's important that we make sure that
| | 07:37 |
regardless of what conditional statements
get executed, that we still end up with a
| | 07:41 |
loop that will eventually break out.
Now, if you were to do this and you
| | 07:45 |
create an infinite loop, PHP will
eventually time out.
| | 07:49 |
The page will say, okay I've tried this
for long enough, this seems to be an
| | 07:52 |
infinite loop and I'm going to give up.
And that's a configurable option in PHP.
| | 07:56 |
Is to how long it will wait before it
gives up.
| | 07:58 |
It is not something you typically need to
modify.
| | 08:01 |
So, on your own,I want you to try to make
a new example.
| | 08:03 |
And in your example, I want you to try
and have a different behavior.
| | 08:07 |
For even numbers and odd numbers, so that
as you go through a loop, and it counts
| | 08:12 |
up, even numbers and odd numbers are
handled differently.
| | 08:16 |
I will give you a helpful hint, though.
It might be useful for you to use the
| | 08:19 |
modulo operator.
That's the percent sign.
| | 08:21 |
And that tells you whether a number
divides evenly by another number or not,
| | 08:26 |
by returning the remainder.
So, it returns the remainder once you do
| | 08:29 |
division, count Modulo two tells us how
many are left over if we divide count by 2.
| | 08:35 |
Give that a shot and make sure that you
have an understanding of how while loops work.
| | 08:38 |
We're going to look at four loops next.
| | 08:40 |
| | Collapse this transcript |
| For loops| 00:00 |
In this movie, we'll look at For Loops.
For loops are very similar to While Loops.
| | 00:04 |
They repeat a section of code, until a
condition is met.
| | 00:07 |
But the syntax used for doing it is very
different.
| | 00:10 |
Why have both, then?
Well, probably because For Loops are the
| | 00:13 |
most classic kind of loop that there is,
and they're used in many other
| | 00:16 |
programming languages.
And some people prefer the compact syntax
| | 00:20 |
of For Loops.
Let's take a look at this syntax now.
| | 00:24 |
A For Loop looks very similar at first,
except for the expression part.
| | 00:29 |
Instead of having one expression in
parenthesis, we're going to have three
| | 00:33 |
separated by semicolons.
And each one's going to perform a
| | 00:36 |
different function.
Expression 1, is going to be executed the
| | 00:39 |
first time only.
It's like an initializing statement
| | 00:43 |
before the loop starts.
Expression 2, is the test expression
| | 00:46 |
that's going to be checked at the start
of each loop.
| | 00:49 |
Just like the expression that we had in
the While Loop.
| | 00:51 |
Expression 3, is going to be executed at
the end of every loop.
| | 00:56 |
Right after the loop finishes, right
before it goes back and evaluates
| | 00:59 |
expression two again.
Perhaps a better way to think of it is as
| | 01:03 |
initial test and each.
There's the initial value, there's the
| | 01:08 |
test that its going to perform each time,
and then each time through the last thing
| | 01:12 |
that its going to do is execute that each
statement.
| | 01:15 |
Now, this syntax will work for a single
line statement, but just like we saw with
| | 01:19 |
the While Loops.
I think it's a good practice to always
| | 01:21 |
put curly braces around it.
And if we have a multi-line statement
| | 01:24 |
those curly braces are required, we have
to have it.
| | 01:27 |
So, I'm going to suggest that it's the
best practice for for you to always use
| | 01:29 |
those those curly braces any time you
have a For Loop.
| | 01:32 |
And then we can tell what's being looped
across.
| | 01:34 |
It's everything inside those curly
braces.
| | 01:36 |
Alright, so I haven't really fully
explained how initial test that each work
| | 01:40 |
can practice.
Let's actually go to some code.
| | 01:42 |
So that we can try it out, and see how
they work.
| | 01:45 |
So to start with, I'm going to open up
basic.html.
| | 01:48 |
And I'll just do save as on this.
And we're going to call this one forloops.php.
| | 01:55 |
We'll call it loops:for.
Right now to begin with, I'm actually
| | 02:02 |
going to paste in a while loop, while
loop example.
| | 02:09 |
And I want you to see this while loop,
because the way that it relates to the
| | 02:13 |
for loops, I think will help for loops
make a lot more sense to you.
| | 02:17 |
So, let's write our for loop now, right
below it and let's try to accomplish the
| | 02:20 |
exact same thing that the while loop
does.
| | 02:23 |
So, we're going to have our PHP tags, and
then let's start with for, and then we
| | 02:28 |
have our initializing statement.
Right, that's the first argument.
| | 02:31 |
The thing that we're going to do at the
beginning is initialize.
| | 02:34 |
So, we did some initialization up here,
look at this.
| | 02:36 |
Count equals 0.
We initialized the value count before we
| | 02:41 |
did the while.
Well, that's exactly what we want to do here.
| | 02:43 |
That's our initialization.
Here it was on a separate line.
| | 02:46 |
Here it's inside the arguments to the
loop.
| | 02:49 |
Now, right after that, we need a
semicolon, so we have a second statement
| | 02:53 |
after that.
In the next statement, is going to be the
| | 02:57 |
expression of what we do each time
through that.
| | 02:58 |
And I told you that was exactly like the
while loop.
| | 03:01 |
So we're just going to copy that, we're
going to put that in there.
| | 03:03 |
And then, the last we'll remember is
something that happens at the end of
| | 03:08 |
every loop, right before it goes back
again.
| | 03:09 |
Well, lets look at our while loop.
What are we doing each and every time?
| | 03:13 |
We're incrementing, and that's the
probably the most common thing it happens
| | 03:17 |
in this third expression.
You can also decrements it can be count
| | 03:20 |
minus minus.
Or it can do something much more complicated.
| | 03:23 |
It could divide by two, or multiply by
four.
| | 03:26 |
All sorts of things can go there.
But typically, the most common one is
| | 03:29 |
just to simply increment.
So, now that we have that, we just need
| | 03:33 |
to put our curly braces, and then what
are we actually going to loop?
| | 03:35 |
What's the business that we want to do
each time we go through this loop?
| | 03:39 |
Well there it is right there.
We're just going to take count and then
| | 03:41 |
output it.
So, some people like this syntax better.
| | 03:45 |
We're doing all the initialization.
We're doing the incrimenting.
| | 03:47 |
We're doing the testing all in one line.
It's all kind of get that business out of
| | 03:51 |
the way, right there at the start.
And then what's inside our loop is a
| | 03:55 |
little bit more isolated.
It has nothing to do with the business of looping.
| | 03:58 |
It really is just the thing that we want
it to do each time through.
| | 04:03 |
So, let's try it, just to make sure that
this works.
| | 04:05 |
Let's give ourselves a br tag here just,
so we can separate that out from the one
| | 04:09 |
before it.
Let's go to Firefox and instead of while
| | 04:13 |
loops, we're now going to open up for
loops.
| | 04:16 |
And there we go, see it did the exact
same thing right underneath it.
| | 04:21 |
Now just like we had with while loops, we
can do all the same things.
| | 04:24 |
You want to watch out for infinite loops
and you can have if statements inside
| | 04:28 |
here that do conditional operations.
So as another example, let's look at the
| | 04:32 |
loop that I gave you as a assignment at
the end of the while loop section.
| | 04:36 |
Something to do on your own.
Let's try those results here.
| | 04:39 |
So we'll do br tag, and then I'm going to
paste in a for loop.
| | 04:44 |
It's going to start out with count equals
to one.
| | 04:46 |
Count is while it's less than 20, it's
going to keep executing.
| | 04:49 |
And each time it's going to increment
count.
| | 04:52 |
Now, just to see the difference.
Let's just try it in reverse.
| | 04:54 |
Let's say, count is equal to 20 to start
with.
| | 04:58 |
And as long as count is greater than
zero, then were gona keep going.
| | 05:04 |
So were going to count down, and then
we're going to count down that I need to
| | 05:07 |
do minus minus here at the end.
You also could do minus equals 1 if you wanted.
| | 05:13 |
That does the exact same thing.
Now, here's the, the heart of my loop.
| | 05:18 |
If count modulo 2 is equal to 0.
That means if I take count and divide it
| | 05:23 |
by 2, if there is no remainder, right?
If it divides evenly, then it will be
| | 05:28 |
equal to zero.
Nothing left over, then I'll know that
| | 05:30 |
it's an even number.
Otherwise it's an odd number.
| | 05:32 |
All right, so let's just try that out.
Let's go over here to Firefox.
| | 05:36 |
Let's reload.
There it goes, 20 is even, 19 is odd.
| | 05:39 |
All the way down until we get to one is
odd.
| | 05:42 |
So, hopefully, you see how that works,
and you see the relationship between a
| | 05:46 |
while loop and a for loop.
How they're basically doing the exact
| | 05:49 |
same thing?
It's just a different syntax and a
| | 05:51 |
different way to write it.
Use whichever one feels most comfortable
| | 05:54 |
to you.
They both accomplish exactly the same
| | 05:56 |
thing, it's just a matter of personal
style and taste.
| | 05:58 |
| | Collapse this transcript |
| Foreach loops| 00:00 |
Foreach loops are a little different from
the previous two types of loops.
| | 00:03 |
Foreach loops are going to take an array
and loop through each item of the array
| | 00:08 |
until it gets to the end.
If you think back, while loops and for
| | 00:11 |
loops know whether to quit or to keep
looping by testing if a condition has
| | 00:16 |
been met.
Foreach loops, on the other hand, are
| | 00:18 |
going to know whether to quit or whether
there are items left in the array to loop over.
| | 00:23 |
The end of the array is what ends the
looping.
| | 00:25 |
There's really just two approaches to
looping.
| | 00:27 |
Let's use a real-world scenario for a
second.
| | 00:29 |
Imagine that I have a classroom with 20
kids in it.
| | 00:32 |
And I have to put name tags on all of
them.
| | 00:34 |
Now, I could just go around the room.
And I could put a name tag on each kid
| | 00:38 |
and keep track of the numbers as I went.
And knowing that there are 20 kids, when
| | 00:43 |
I had gotten to the number 20, I'd know I
was done.
| | 00:45 |
Or, since I know that I have a finite
set, I could just line them all up, start
| | 00:51 |
at the beginning and move down the line.
And when I got to the end of the line, I
| | 00:55 |
would know I was done.
I wouldn't have to keep track of the
| | 00:57 |
count as I was going.
I mean, I could count.
| | 01:00 |
I could certainly know that I was on the
fourth kid, but I wouldn't have to.
| | 01:03 |
I could just start at the first kid, go
to the second kid, and then the third
| | 01:07 |
kid, until I got to the end of the line.
That's how a foreach loop works.
| | 01:12 |
The syntax is also a little bit
different.
| | 01:13 |
We have foreach, and that's foreach all
run together.
| | 01:18 |
No spaces, no underscore or anything like
that, just foreach.
| | 01:21 |
And then, in parentheses, we have a
special statement.
| | 01:24 |
The first thing we're going to provide is
the array that we're looping through.
| | 01:28 |
So, that's what I've got there with the
variable array.
| | 01:30 |
It doesn't have to be called array.
It's a place holder.
| | 01:33 |
To show you that that's where the array
goes.
| | 01:35 |
And then, we've got this special keyword,
as.
| | 01:37 |
That's part of the foreach statement.
It's a special keyword that it uses.
| | 01:41 |
And then, we have another variable after
that, called value.
| | 01:46 |
Now, value also can be named something
else.
| | 01:48 |
What it's doing though, is that each time
it goes through the loop.
| | 01:52 |
Each item in the array is going to be
assigned to this variable value.
| | 01:57 |
Value is just now being defined here.
Array is a value that existed before the
| | 02:01 |
each statement, value doesn't.
Value is something that we're creating
| | 02:05 |
right now for use only inside the loop.
And then each time through the loop, we
| | 02:09 |
have access to that variable.
And that variable is a reference to the
| | 02:14 |
item in the array that we're currently
working with.
| | 02:17 |
If that at all seems fuzzy, don't worry.
It'll make a lot more sense when we see
| | 02:20 |
an actual concrete example.
Now, just like the other loops though, I
| | 02:23 |
think it's a good practice to put curly
braces around our loops.
| | 02:26 |
So, it's very clear where the loops start
and stop with a single nine statement.
| | 02:30 |
It's not necessary, but with a multi-line
statement, it is.
| | 02:33 |
Now, I want you to notice something else
about this foreach statement.
| | 02:36 |
It's the same thing that I told you at
the beginning.
| | 02:38 |
Notice that there is no condition here.
There's no condition and there's no
| | 02:42 |
mechanism for incrementing, right?
Those are two things that both of the
| | 02:46 |
while and for loops used.
They had a condition that had to be met
| | 02:50 |
to know when it should exit the loop, and
an Increment it.
| | 02:53 |
This one doesn't.
Why not?
| | 02:54 |
Well, because the condition for exiting
is when it gets to the end of the array.
| | 02:58 |
And it doesn't need to increment because
it knows when it gets to the bottom of
| | 03:01 |
the loop, it knows what it should do.
It should go to the next item in the array.
| | 03:04 |
So, both of those are missing here.
All we're doing is saying, for each one,
| | 03:08 |
assign it to this variable, and then do
the loop.
| | 03:11 |
Let's try an example.
An example will make this clear.
| | 03:13 |
Let's open up basic value HTML again.
Let's do Save As, this time we're going
| | 03:19 |
to save it as foreachloops.php.
I'll call it loops foreach, no spaces.
| | 03:29 |
So, I'm just going to paste in our first
example here.
| | 03:31 |
I've got an array filled with numbers,
and I'm assigning it to the variable ages.
| | 03:36 |
So now here, I have my foreach loop.
And it's going to take that array the
| | 03:41 |
ages points to as age.
So, each one of these values is going to
| | 03:45 |
be assigned to the variable age
temporarily.
| | 03:48 |
Notice the difference here, the singular
and the plural, right?
| | 03:51 |
Age and ages.
Ages was defined before age is just now.
| | 03:56 |
Being introduced here.
Now, each time through the loop I have
| | 03:59 |
access to the age, right here, to use,
right?
| | 04:02 |
Let's bring that up and try it in the
browser.
| | 04:04 |
Go to Firefox, and instead of for loops,
I'm going to bring up foreach loops.
| | 04:09 |
See how that works?
Age 4, 8, 15 and so on.
| | 04:12 |
Every time it goes through the loop, the
next item is assigned to age so that I
| | 04:18 |
can work with it and it just moves down
this list.
| | 04:21 |
If there are six items in the array, well
then it's going to loop six times.
| | 04:25 |
This is very useful when you want to
traverse an array.
| | 04:28 |
Imagine that you have a list of a user's
last 15 orders that you want to output to them.
| | 04:33 |
Well, that's an array.
The 15 orders are an array.
| | 04:36 |
You could use a while loop or a for loop
as you iterate through those, but the
| | 04:40 |
foreach loop really works better.
Because the foreach loop says, alright,
| | 04:44 |
start at the beginning of these orders.
And let's start going through them.
| | 04:46 |
And we get to the end, we're done.
Loop through it however many of them
| | 04:49 |
there are.
If there's 15, show all 15, if there's
| | 04:52 |
20, let's show 20.
And it's especially nice, because then
| | 04:55 |
each time through the array.
The customer's order would get assigned
| | 04:59 |
to a variable called order.
That would have everything we needed to
| | 05:02 |
then output the same way that we're
working with age now.
| | 05:04 |
And you may be asking well, if this works
for regular arrays, but what about
| | 05:08 |
associative arrays?
Remember we talked about associated
| | 05:10 |
arrays before where they have a key and a
value?
| | 05:13 |
We just have to modify our syntax
slightly to make associative arrays work
| | 05:17 |
with foreach.
The overall structure is the same, but
| | 05:20 |
we're going to have array as.
And we're going to have two variables
| | 05:25 |
that we're going to assign.
One for the key, and one for the value.
| | 05:28 |
Now, they don't have to be called key and
value.
| | 05:30 |
They can be called anything you want, but
the important part is that we have two variables.
| | 05:34 |
And between them, we have the equals
greater than sign.
| | 05:38 |
Just like we use in the associative array
to separate our keys and values.
| | 05:41 |
Let's try an example using associative
arrays.
| | 05:44 |
So again, I'm going to paste our block of
code.
| | 05:46 |
Remember, you can pause the movie if you
need to copy this down.
| | 05:49 |
What I've got here is a person.
And that person has an array of data.
| | 05:55 |
And it's labeled, first name Kevin, last
name Skoglund, address, city, state, zip, right?
| | 06:00 |
All that information.
And then for each item in person as, I'm
| | 06:06 |
calling it the attribute and the data.
Not key in value, these are the key and value.
| | 06:11 |
That's the technical name for it, this is
the key and this is the value.
| | 06:14 |
But I'm going to be assigning them to the
variables attribute and data.
| | 06:18 |
Now on the next line here, I've got a
couple of string functions that work.
| | 06:21 |
We talked about some of these before.
We have UC words and string replace.
| | 06:24 |
What I'm doing is cleaning up this
attribute which uses underscores in it
| | 06:29 |
and lower case to make it look nicer.
Take away the underscores and upper case
| | 06:33 |
the first words.
I'm calling that attribute nice and I'm
| | 06:37 |
outputting attribute nice and the data.
Let's take a look.
| | 06:40 |
I'll save it come back over to Firefox,
reload it, and there we go.
| | 06:44 |
First name Kevin, last name Skoglund.
Address 123 Main Street, and so on.
| | 06:48 |
See how that works.
I've specifically wanted to show you this
| | 06:51 |
example by using the ucwords as string
replaced.
| | 06:54 |
because this is the kind of thing you'll
get back from your database as the name
| | 06:57 |
of a database field.
And we would want to clean that up before
| | 07:00 |
we showed it to end user.
We don't want to show them all the
| | 07:03 |
underscores and everything.
I want to show them something that's a
| | 07:05 |
little bit nicer.
So, I'm going to give you one more
| | 07:07 |
associative array example before we move
on.
| | 07:10 |
Here, I've got just another array, and
I've got brand-new computer.
| | 07:14 |
One month of Lynda.com and learning PHP
are the keys.
| | 07:18 |
The values are 2000, 25 and null.
So, I've assigned that to prices.
| | 07:24 |
For each item in prices, I'm going to
assign those to item, because each of
| | 07:29 |
these are items.
And each of these are prices, and each of
| | 07:33 |
these is going to be assigned to price.
because that's what they are, they're prices.
| | 07:37 |
And then, as I iterate through each of
these, I'm also going to have a
| | 07:39 |
conditional statement here.
That's going to check to see whether or
| | 07:43 |
not price is an integer, which will be
true for the first two but not for the
| | 07:47 |
last one.
And so, let's go ahead and just reload
| | 07:49 |
that up over here.
It's a little bit of a joke.
| | 07:51 |
You can see, brand new computer, $2,000,
one month of Lynda.com, $25, learning
| | 07:57 |
PHP, priceless.
Hopefully, you now understand how foreach
| | 08:00 |
loops work.
Whenever you're working with an array,
| | 08:03 |
you should first think about using
foreach loops before you go to one of the
| | 08:07 |
other types.
Because foreach loops are tailor made for
| | 08:10 |
working with arrays.
So, if you have an array chances are
| | 08:13 |
foreach loops are what you want.
| | 08:15 |
| | Collapse this transcript |
| Continue| 00:00 |
We've now taken a look at three types of
loops.
| | 00:02 |
While loops, for loops, and foreach
loops.
| | 00:05 |
In the next two movies, we're going to
learn how to use the functions Continue
| | 00:08 |
and Break to get more utility and
flexibility from these loops.
| | 00:11 |
We'll start with Continue.
Continue is used inside a loop to skip
| | 00:15 |
the rest of the current iteration and to
go immediately to the conditional
| | 00:18 |
valuation that starts the next iteration.
It may sound silly, but for me, continue
| | 00:23 |
evokes the image of a Hollywood casting
director shouting next in the middle of
| | 00:27 |
an audition.
It's like saying, we've seen enough,
| | 00:29 |
thanks, let's skip ahead to the next one.
To give you an example, let's imagine
| | 00:33 |
that we have a database of college
students and our code is going to loop
| | 00:35 |
through them and send them all in email.
ut, if a student is already signed up for
| | 00:39 |
a biology course this semester.
Then we aren't going to send them the email.
| | 00:43 |
So we start the loop, we check to see if
the student has signed up for Biology,
| | 00:46 |
and if so then we know that we don't need
to do any further processing at all.
| | 00:50 |
We can just skip to the next student
record.
| | 00:52 |
That's what continue allows us to do.
And it makes our code more efficient.
| | 00:55 |
Let's see an example.
So let's open up basic.html and let's do
| | 01:00 |
Save As, and we're going to call this
continue.php.
| | 01:05 |
I'll change this to continue.
I'm going to start with just a basic for loop.
| | 01:10 |
So here's the same for loops we were
looking at before.
| | 01:12 |
Just counting the numbers zero to ten and
echoing them out.
| | 01:15 |
We saw an example earlier where we said
if the count is equal to five then do
| | 01:23 |
some different behavior.
Right, we outputted something different.
| | 01:26 |
Instead this time I'm going to have it
just do continue, that's our continue function.
| | 01:31 |
So, continue with a semicolon after it is
the only thing.
| | 01:34 |
Let's save it and let's bring it up in
our browser and look at what it looks like.
| | 01:37 |
So here I am in Firefox, and instead of 4
H loops, let's go to continue.
| | 01:41 |
There it is, I think this makes it really
clear what continue does.
| | 01:44 |
We get to the number 4, and there is no
number 5, it picks up again with 6.
| | 01:50 |
Let's go back and look at the code.
When it's for, well it doesn't meet this
| | 01:53 |
condition here, the if statement, so it
outputs for.
| | 01:56 |
Then for gets incremented to 5.
The count is still less than or equal to 10.
| | 02:01 |
Now the count is equal to 5 so we
continue tells it to immediately loop.
| | 02:07 |
That increments the counter to 6, it
checks and sees that 6 is less than or
| | 02:11 |
equal to 10, this time it echoes the 6.
One way that you can think about it is
| | 02:16 |
that there's an implicit continue at the
end of every loop.
| | 02:20 |
Now we don't need to actually write it
out there because it's built into the
| | 02:23 |
looping structure.
But what we're doing in this case is
| | 02:25 |
we're telling it to loop earlier.
Instead of having to wait til we get to
| | 02:29 |
the continue that's at the end of the
loop to tell it to loop again.
| | 02:32 |
We have this continue here telling it to
do it when we want.
| | 02:36 |
Now if you're doing something so simple
as this, I just want to remind you that
| | 02:39 |
white space doesn't matter.
Something like this would do just as well.
| | 02:43 |
I'm going to change this to be our modulo
operator with percent 2 equals to 0.
| | 02:49 |
Let's just bring that up and try it.
And that basically says that, we're
| | 02:53 |
going to print out the odd numbers,
right?
| | 02:55 |
Whatever the remainder of dividing count
by 2, is equal to 0, then we're going to continue.
| | 02:59 |
We're going to skip over it, and we don't
do anything, we don't do any further
| | 03:02 |
processing, and it never gets to this
echo statement.
| | 03:05 |
Now you may be tempted to think that we
could use an if statement instead.
| | 03:08 |
That we could do something like this.
If the remainder of dividing by 2 is not
| | 03:12 |
equal to 0, then echo it out, otherwise
don't, right?
| | 03:16 |
But that kind of misses the whole point
of why continue is useful.
| | 03:19 |
With a simple, one line statement like
this, they are practically the same.
| | 03:22 |
But imagine if our if statement had 100
lines of code in it.
| | 03:25 |
Then if I'm reading the code, I would
need to scroll down 100 lines to find the
| | 03:29 |
end of the if statement.
And then check to see if there's any
| | 03:32 |
other code that would be executed before
the loop finishes.
| | 03:35 |
With continue, we don't have to do that,
we know what happens.
| | 03:37 |
As soon as the code gets to continue, the
loop starts over again.
| | 03:41 |
It's explicit and it's definitive.
It says simply we're done and we're
| | 03:44 |
moving on.
It make our code clearer.
| | 03:46 |
Both easier to read and easier to
understand.
| | 03:48 |
Let's try another example using a while
loop.
| | 03:51 |
So here I've got a, a simple while loop.
You can see that it;s basically the same
| | 03:55 |
as I had with my for loop.
I've got my account being initialized.
| | 03:58 |
Here's my condition.
When count is equal to less than 10.
| | 04:01 |
And I'm incrementing here, count++.
I'm echoing the count just like I was before.
| | 04:06 |
And I've gone back to the other version.
Where the count is equal to 5 continue.
| | 04:10 |
But there's a big difference between this
one and the one before.
| | 04:13 |
And that's why I've put, what's wrong
with this, up here.
| | 04:15 |
Because this has a problem.
Do you see what it is?
| | 04:19 |
You may want to pause the movie, and take
a second to look at it.
| | 04:21 |
And see if you can figure it out.
Walk through the loops with me step by step.
| | 04:25 |
Let's image that count is equal to four.
That's less than or equal to 10, it's not
| | 04:30 |
equal to 5, so we output 4.
Then 4 gets incremented, it becomes 5.
| | 04:35 |
We got back to the top of the loop, 5 is
less than or equal to 10.
| | 04:39 |
Now, count is equal to 5, so we continue.
That goes back to the top of the loop,
| | 04:44 |
and it evaluates count is less than or
equal to 10.
| | 04:47 |
What is count now?
It's 5 still.
| | 04:49 |
We didn't increment it.
So it comes down here, count is equal to
| | 04:53 |
5, we get a continue, we go back.
We check the condition, and when stuck in
| | 04:57 |
an infinite loop, we never get to this
increment that's down here.
| | 05:00 |
With for loops that doesn't happen,
because this incrementation is built in
| | 05:04 |
to the loop.
It always does this last step with
| | 05:08 |
foreach loops it also is in a problem,
because those always move to the next
| | 05:12 |
element in the array.
But with while loops, it's not
| | 05:15 |
necessarily true.
This incrementation is optional, it's up
| | 05:19 |
to us to provide it.
So, what we really need to do is fix it
| | 05:22 |
by making sure that we still increment
before we continue.
| | 05:25 |
So when count is equal to 5, make it 6
and then go to the next one.
| | 05:30 |
As an important tip when working with
while loops and continues, it's a gotcha
| | 05:33 |
that can sneak up when you're not
watching for it.
| | 05:35 |
Okay, let's make sure that we fix that.
We're not going to bring it up in the
| | 05:38 |
browser right now, but I want to show you
one last example.
| | 05:41 |
because there's one more thing you need
to know about continue, and that is how
| | 05:44 |
to work with continue when you have a
loop inside a loop.
| | 05:47 |
So here I have one loop, I've got for,
and I'm using i for my counter now.
| | 05:51 |
By the way, that's very common when we
have loops, it's just to use a simple
| | 05:55 |
single letter variable.
So i is counting from 0 to 5, then I've
| | 05:59 |
got my modulo operator here.
So when we get to an even number it's
| | 06:02 |
going to use continue, then I've got
another loop, loop inside a loop.
| | 06:07 |
This one is going to use k and k is going
to count from 0 to 5 and it's going to
| | 06:12 |
just echo out i with a hyphen and then k
and then a br tag.
| | 06:16 |
Let's take a look and just see what it
looks like on its own before we try doing more.
| | 06:19 |
Let's come back over to the browser,
let's reload.
| | 06:22 |
You do want to make sure that you fix
this problem up here before we come back
| | 06:27 |
and try it, otherwise you'll have an
infinite loop.
| | 06:30 |
So you can see that the results I got are
1-0, 1-1, 1-2, 1-3, 1-4, 1-5 and then it
| | 06:33 |
jumps to 3-0 and then eventually 5-0,
okay?
| | 06:39 |
So there's my series that I've generated.
Now what I want to do now is I want to
| | 06:43 |
add another line here, right before the
echo, and I'm going to do another continue.
| | 06:48 |
Let's say if, k, talking about k here, is
equal to 3.
| | 06:52 |
Not the modulo, but k is actually equal
to 3.
| | 06:56 |
Then, let's continue, okay.
So let's take a look at what that does.
| | 07:00 |
Come back over and reload it, and notice
that now 1-3 is missing, 3-3 is missing,
| | 07:05 |
and 5-3 is missing, that makes sense.
That's what we would expect to happen.
| | 07:08 |
Continue, continues this loop.
Comes back to the top of this loop,
| | 07:13 |
therefor just skipping 3 essentially each
time on the second loop.
| | 07:18 |
But what if we wanted this continue not
to apply to the inner most loop, but to
| | 07:23 |
the outermost loop.
What if we were essentially saying, look,
| | 07:26 |
we've got a whole bunch of loops going
and processes, we're looking for something.
| | 07:29 |
And once we find it, w want to tell the
parent that it should continue.
| | 07:33 |
We don't need to keep doing any more of
the loops here as well.
| | 07:37 |
Well, the way that we do that is we can
provide an argument to continue.
| | 07:40 |
Put parentheses and then an integer
inside of it.
| | 07:44 |
By default, continue has an integer of
one as an argument, but if we provide
| | 07:48 |
another number, like 2.
The net tells it that it should continue,
| | 07:52 |
not just one loop, but 2 loops back.
So don't just look for 1, go all the way
| | 07:57 |
back to this looping structure and
continue that one.
| | 08:00 |
So this is different, this one is the
same as that one.
| | 08:03 |
I'll leave it in there just so that you
see the difference.
| | 08:05 |
And then 2 is telling it to skip that
one.
| | 08:07 |
Let's Save it and go back and reload our
page and see what happens now.
| | 08:11 |
Now when it gets to 1-3, it doesn't do
1-4 and 1-5, it goes ahead and jumps to
| | 08:17 |
the next one, to the next iteration using
3.
| | 08:20 |
Go back and forth between those two if
you need a little more time to understand
| | 08:23 |
the difference.
Now that we understand how continue
| | 08:25 |
works, in the next movie we're going to
take a look at break.
| | 08:27 |
| | Collapse this transcript |
| Break| 00:00 |
In this movie, we're going to learn to
use Break with our loops.
| | 00:03 |
We've seen Break already.
Remember, it was an important part of
| | 00:06 |
working with switch statements.
We used Break to stop execution of any
| | 00:09 |
other cases, and then to exit switch
function.
| | 00:13 |
We'll use it in a similar way with loops.
Break will end execution of the loop.
| | 00:17 |
Not just the current iteration, but all
iterations.
| | 00:20 |
You can think of it as breaking out of
the loop.
| | 00:23 |
In the last movie, for the continue
function I suggested the image of a
| | 00:26 |
Hollywood casting director saying next in
the middle of an audition that was in progress.
| | 00:30 |
Let's continue that metaphor here for a
second.
| | 00:32 |
Break, would be if the casting director
watched a few auditions and then said,
| | 00:36 |
this is the one.
You've got the job, the rest of the
| | 00:39 |
auditions are canceled.
I don't need to see any more.
| | 00:41 |
Break stops the whole process.
And it's very useful when we've found
| | 00:45 |
what we're looking for or when we've
reached a definitive answer to a question.
| | 00:49 |
We can stop looking, stop looping, and
move on.
| | 00:52 |
Let's create an example.
We'll start by taking basic.html I'm
| | 00:56 |
using Save As.
And we'll save it as break.php.
| | 01:01 |
Up here, we'll write break.
And I'm just going to put in a simple loop.
| | 01:07 |
And I've got continue in there.
This is the exact same loop that we had
| | 01:11 |
in the last movie.
First, let's just bring up this version,
| | 01:13 |
and look at it in a browser.
And remind ourselves what it does.
| | 01:16 |
Just save it, go to Firefox, and instead
of continue now, I'm looking at break.php.
| | 01:22 |
So there it is.
Notice that, the 5 is missing, because
| | 01:26 |
continue told it to skip over the 5.
Instead of continue though, now let's
| | 01:30 |
tell it to break, so if it, count is
equal to 5, it will break.
| | 01:35 |
Come back here, and notice now it ended
execution completely.
| | 01:40 |
It stopped not just this loop, and
stopped from doing the echo statement, it
| | 01:45 |
ended this entire loop.
It broke out of it.
| | 01:47 |
So once we get here and count is equal to
5, it executes break.
| | 01:51 |
And the very next thing it does, is it
drops down here to the last curly brace
| | 01:56 |
and continues processing our PHP code
from there.
| | 01:58 |
It never goes back an does any of these
tests, or increments, or anything else.
| | 02:02 |
It just immediately exits our loop.
Break will make your code a lot more efficient.
| | 02:07 |
Let's imagine for a moment that you have
20,000 customer records, and we're
| | 02:10 |
looking for one record that matches a
certain criteria.
| | 02:13 |
If we find the open that we want, when we
get to the 100th loop, do we really need
| | 02:17 |
to keep looping for 19,900 more times?
No, it's much more efficient to just
| | 02:23 |
break out of the loop and to say, I found
it.
| | 02:25 |
We can stop looping and move on from
here.
| | 02:27 |
Now, since you understand continue, I
don't think we need to spend a lot more
| | 02:30 |
time with break.
I think you get the idea behind it.
| | 02:33 |
But I do just want to show you that we
also should take a look at what happens
| | 02:36 |
when you have a loop inside a loop using
break.
| | 02:39 |
Remember we did this with continue and we
had this example or we had continue and
| | 02:42 |
we provided the argument of one or
continue2, which told it to continue out
| | 02:47 |
of two loops.
Well, let's look at this with break.
| | 02:49 |
Let's change this second one to be break.
Right?
| | 02:54 |
Actually, let's remind ourselves, first
of all, what this does.
| | 02:56 |
So, we'll just comment out this line and
let's go back and reload the page.
| | 03:00 |
Okay, so that's what it looks like.
That's what it does on it's own.
| | 03:03 |
Now, let's un-comment those lines and
then when it gets to k equals to 3, it's
| | 03:08 |
going to break.
There we go.
| | 03:11 |
Notice that the 1-3, it did a break and
at 3-3 it did a break.
| | 03:16 |
It didn't do the items that were after
that but it did keep doing the first item.
| | 03:21 |
It did keep iterating and advancing i.
Just like continue, if we provide an
| | 03:26 |
argument to it and say that we want to
break out of two loops, it will break out
| | 03:30 |
of both of them.
We have control over which ones we break
| | 03:32 |
out of by using an integer as an argument
to break.
| | 03:36 |
Let's just try that so you see the
difference.
| | 03:37 |
So here, we've basically said, all right,
we're drilling down through one loop,
| | 03:40 |
we're using another loop, we finally
found the item.
| | 03:43 |
K equals to 3.
That's it.
| | 03:45 |
So now I just break out of the looping
structure completely.
| | 03:48 |
We don't need to keep doing anything
else.
| | 03:49 |
You can go back and forth and change this
number between 2 and 1, to see the
| | 03:53 |
difference between what it does.
Now that we understand the three types of
| | 03:56 |
loops, and how to use continue and break
with them, we'll be able to write very
| | 04:00 |
efficient looping structures.
Before we move on, I want us to look at
| | 04:04 |
one more thing which is how array
pointers work, and we'll do that in the
| | 04:07 |
next movie.
| | 04:07 |
| | Collapse this transcript |
| Understanding array pointers| 00:00 |
Before we leave the topic of loops
behind, I want to talk about array
| | 00:03 |
pointers and show you how they work.
It may seem like a technical detail, but
| | 00:07 |
it will become important later when we
start working with databases.
| | 00:10 |
PHP maintains a pointer that points to
one of the items in an array.
| | 00:15 |
That item is referred to as the current
item.
| | 00:18 |
By default, that's always the first item
in the array.
| | 00:21 |
When we start looping through arrays
using something like four each, PHP moves
| | 00:26 |
the pointer down the array as it assigns
each value to the loop's variable.
| | 00:30 |
Moving the pointer to the next value is
how PHP keeps track of which item you're
| | 00:34 |
working with now and what the next item
is that it should give you after that.
| | 00:37 |
But loops are not the only way to move
pointers around.
| | 00:40 |
Let's take a look.
Let's create ourselves a little workspace
| | 00:43 |
by using basic dot html.
And we'll use Save As, and then we're
| | 00:47 |
going to create pointers.php.
To begin with, let's just give ourselves
| | 00:52 |
a little area here, php, and we'll put in
array of ages.
| | 00:58 |
I'll also change the title to be
pointers.
| | 01:00 |
Alright.
So this is the array that we were working
| | 01:02 |
with before.
So the pointer, by default.
| | 01:04 |
The current item that's being pointed at
in this array is the first item, four.
| | 01:09 |
As we do a loop through there, the
pointer moves.
| | 01:12 |
So, the first time, it's pointing at
four.
| | 01:14 |
The second time through the loop, it's
now pointing at eight.
| | 01:17 |
And then it's pointing at 15, and so on.
It moves all the way down, and when it
| | 01:19 |
gets to the last one.
Then it moves one more and says, oops,
| | 01:23 |
there's no more items, I must be done.
And so the loop ends.
| | 01:26 |
We can take a look at this current
pointer value by using the current function.
| | 01:31 |
So here it is, just current, and then we
pass in the array.
| | 01:34 |
That'll tell us what the current value
that it's pointing to is.
| | 01:38 |
Let's try that.
Let's save it.
| | 01:40 |
Let's go back to Firefox.
And instead of break, let's open up pointers.php.
| | 01:48 |
So there it is, it is pointing at the
item four, I've got the one if front of
| | 01:51 |
it just so we can tell between each of
these tests that we run.
| | 01:53 |
So here's four, now we can tell the
pointer to move to the next item manualy
| | 01:59 |
by using the next function.
So here's, this is just simply next ages.
| | 02:04 |
We're just saying hey, advance the
pointer for ages to point to the next item.
| | 02:08 |
And then will once again echo back what
the current item it's pointing to is.
| | 02:12 |
This time, I've got a 2 in front of it so
we can tell the difference.
| | 02:15 |
I'll save that.
Let's go here.
| | 02:18 |
And there it is.
It's pointing at eight.
| | 02:21 |
See how that works?
Let's go back and let's just do a couple
| | 02:23 |
more here.
Let's tell it to go two more times and
| | 02:27 |
then we'll do a third one and we'll ask
it what it is for the third time.
| | 02:34 |
All right.
It went 2 this time, so we're at 16, so
| | 02:39 |
you've skipped over the 15, and now we're
pointing at 16.
| | 02:41 |
We can also have it go backwards by using
Prev, short for "previous," p-r-e-v, and
| | 02:47 |
that's going to move it backwards.
So tell ages to go backwards one, and
| | 02:50 |
then we'll use number 4 to see what the
value's at now.
| | 02:55 |
And there it is, it's at 15, and my
backwards one.
| | 02:58 |
We also have two more that we'll look at
together, which are reset, so reset will
| | 03:03 |
move the pointer back to the first
element, and end will tell it to shoot
| | 03:07 |
all the way to the end, the last element.
I've numbered those 5 and 6, let's take a
| | 03:11 |
look at that real quick.
There we are.
| | 03:13 |
The first element and the last element.
And now, just so that we can see what
| | 03:17 |
happens when we're at the end, let's go
ahead and do next one more time.
| | 03:20 |
So, here it is, it's at the end, we're
going to call next after the last
| | 03:24 |
element,and that's going to be 7.
Let's see what that gives us back.
| | 03:27 |
Go to return.
We get back nothing, right?
| | 03:30 |
So that's how it knows that it's done.
When it retrieves the last item, the last
| | 03:33 |
item is null then it knows that it's
done.
| | 03:35 |
Okay, so how is this useful?
Let's take a look at a wild loop example
| | 03:40 |
that will illustrate how it works in
practice and which is going to set the
| | 03:42 |
stage for working with database records.
Switch back over here, and let's just put
| | 03:48 |
a br tag, and then we'll put in my while
example.
| | 03:52 |
So, it's a while loop that moves the
array pointer.
| | 03:54 |
This is very similar to what foreach
does.
| | 03:58 |
Notice that in the condition of our while
loop, right here, this expression, that
| | 04:03 |
we're doing an assignment, not a
comparison.
| | 04:05 |
That is a single equal sign, not a double
equals.
| | 04:09 |
What we're doing, is we're.
Assigning a value from current to age.
| | 04:15 |
And we're also testing then to see if
that assignment was successful.
| | 04:19 |
If it returned a value, then the
expression is going to evaluate to true.
| | 04:24 |
But if it returned null, in other words
we got to the end of it, then this
| | 04:29 |
expression will evaluate to a Boolean
false.
| | 04:31 |
Null will be considered false.
And so at that point it will exit.
| | 04:35 |
So what we're essentially saying is, get
the item that the array pointer points to.
| | 04:39 |
Assign it to age.
And if that is an item.
| | 04:42 |
If you successfully got one, then execute
the loop.
| | 04:46 |
If you did not successfully get an item,
well then exit the loop.
| | 04:49 |
We're all done.
And then of course, we do have to
| | 04:51 |
increment inside the array using next.
That'll move the pointer to the next item.
| | 04:56 |
So that now, current will fetch something
different the next time through the loop.
| | 05:00 |
So as I said, this is how foreach works.
So why not just use for each step?
| | 05:04 |
Well, I want you to see this technique
because when we start working with
| | 05:08 |
databases, we are going to use something
very similar.
| | 05:10 |
We'll retrieve many rows of data from a
database table as an array and then move
| | 05:14 |
through each row, advancing the pointer
as we go.
| | 05:18 |
But we can't use foreach for this,
because the database pointers are not
| | 05:22 |
exactly the same as the array pointers.
They're similar, but not the same.
| | 05:26 |
And the database pointers are going to be
moved by the database driver.
| | 05:30 |
Each time that we request a row.
Each time that we do this assignment here.
| | 05:35 |
It's going to increment the pointer as
soon as it finishes making that assignment.
| | 05:39 |
Foreach and next won't be able to move
those database pointers.
| | 05:43 |
They're going to work only on PHP arrays.
Not on these database arrays that we'll
| | 05:48 |
be working with later.
So let's just try out this code real quick.
| | 05:51 |
Let's save it.
We'll go back to.
| | 05:53 |
Firefox, let's try and reload the page.
Notice that nothing happened.
| | 05:58 |
Well why is that?
Well, it because current is still
| | 06:01 |
pointing at null, remember?
Current here was null, so what is it here?
| | 06:05 |
It's still null, nothing has changed.
We need to reset the pointer back to the
| | 06:11 |
beginning for ages.
And now.
| | 06:13 |
We come back here.
You'll see that it zips back to the
| | 06:17 |
beginning, and it starts going through
with each one for all the way through
| | 06:22 |
'til it gets to 42, and then the last one
is null so the loop ends.
| | 06:26 |
So I hope you understand how pointers
work, and this gives you some insight
| | 06:29 |
into how foreach works and also how you
can manipulate those pointers yourself.
| | 06:33 |
But more importantly I want to make sure
that you're familiar with this syntax
| | 06:37 |
using the wild loop, because that's what
we're going to be using when we start
| | 06:41 |
working with databases.
| | 06:42 |
| | Collapse this transcript |
|
|
8. User-Defined FunctionsDefining functions| 00:00 |
A function is code that preforms a
specific task, which is then packaged up
| | 00:04 |
into a single unit that can then be
called upon whenever that task is needed.
| | 00:08 |
An example of a function that we've
already seen and used would be in array.
| | 00:11 |
Any time we want PHP to whether an
element is in an array we can call the in
| | 00:15 |
array function.
We provide the function data to work with
| | 00:18 |
in the form of arguments.
And the function returns output to us at
| | 00:21 |
the end.
These are common feature functions,
| | 00:23 |
providing them input and getting back
output.
| | 00:25 |
Up until now, we've been looking at the
built-in functions that PHP provides, but
| | 00:29 |
we aren't limited to just these
functions.
| | 00:31 |
We can also define our own.
In this chapter, we'll learn how to craft
| | 00:34 |
and use custom functions.
And we'll start by learning the syntax
| | 00:37 |
that's used to define them.
You define a function by first saying,
| | 00:41 |
function, and then a space.
And then the name that you want to give
| | 00:45 |
this function.
It's very much like the variable names
| | 00:48 |
that we assign.
And the rules are very similar.
| | 00:50 |
We can have letters, numbers,
underscores, and dashes.
| | 00:53 |
You can't have any spaces, though.
And it must start with either a letter or
| | 00:57 |
an underscore.
Unlike variable names though, function
| | 01:00 |
names are case insensitive.
So, with a variable, if we had capital
| | 01:04 |
var, and lower case var.
Those were two different variable names.
| | 01:08 |
But if we have a capital function and a
lowercase function, they're going to be
| | 01:12 |
the exact same thing.
However, it's really bad programming
| | 01:15 |
practice to start switching case around.
So, whatever you define them as, that's
| | 01:20 |
how you're going to want to always call
them as well.
| | 01:22 |
Pick one style and stick with it.
For me, I like the all lowercase with
| | 01:27 |
underscores style.
And that's what I'll be using.
| | 01:29 |
After we have the name, then we have
parentheses with our argument list.
| | 01:34 |
These are the arguments that the function
is going to accept.
| | 01:37 |
In this example, I've just got arg1 and
arg2 separated by a comma between them.
| | 01:41 |
Both of these are going to be variable
names so they're going to have the dollar
| | 01:44 |
sign in front of them.
We can have more than two, you just put a
| | 01:47 |
comma and have arg3, arg4 and so on.
You'll probably want to use a better name
| | 01:51 |
for yours that will actually describe
what you're expecting to get in.
| | 01:55 |
So, if you're looking for an array, the
name of it might be array.
| | 01:58 |
And then, we've got our curly braces
after that and inside that is the meat of
| | 02:03 |
the function.
Its all of that code that we're wrapping
| | 02:05 |
up inside this function.
This is the code that we want to reuse
| | 02:08 |
and be able to call from a lot of
different places.
| | 02:11 |
This code tells PHP take this block of
code inside the curly braces and keep it around.
| | 02:16 |
File it under the name.
And when I call for that name, take the
| | 02:19 |
input that I send run the code and give
me back the output.
| | 02:23 |
Let's try an example.
To start with, I will open up basic
| | 02:27 |
.html, and I will do Save As and we are
going to call this functions_defining.php.
| | 02:35 |
And I'll give the title Functions
Defining.
| | 02:39 |
Alright, so let's write our first
functions in our PHP tags.
| | 02:45 |
To write our first function, we're first
going to use the keyword function.
| | 02:48 |
Followed by the name, and I'm going to
call this one say_hello, and then the arguments.
| | 02:53 |
Now, there's not going to be any
arguments to this one.
| | 02:55 |
But I'm still going to put the empty
parentheses there so that it's clear that
| | 02:59 |
that's where the arguments would go.
And then, curly braces.
| | 03:03 |
And then, whatever code we want to
package up.
| | 03:06 |
Whatever we want to happen when we call
this function.
| | 03:09 |
And I'm just going to have it echo the
classic, Hello World, exclamation point.
| | 03:14 |
Put a br tag, and a semicolon at the end,
okay?
| | 03:18 |
So there I have, I've defined my first
function.
| | 03:20 |
It is not something that pre-existed.
This is something that I'm defining.
| | 03:24 |
Let's go to Firefox and take a look.
So, I'll open up Firefox.
| | 03:27 |
And it's going to be localhost, and then
for me, I'm going to use Kevin Scoglund
| | 03:35 |
sandbox and then functions_defining.php,
okay?
| | 03:41 |
I didn't get anything back.
Now, I am seeing the right page.
| | 03:44 |
And you can tell that because it says up
here at the top, Functions Defining, right?
| | 03:48 |
So, that is the page that I'm on.
And we can actually go to our Web
| | 03:51 |
Developer Tools.
And we can view the Page Source.
| | 03:55 |
And we can see that we got something.
But there's nothing in there.
| | 03:57 |
Nothing gets output when you define your
function.
| | 04:01 |
We have to then call the function.
So, what we're saying to PHP is take this
| | 04:06 |
code and store it for later, because I
may call on it.
| | 04:09 |
But we haven't done anything, we haven't
called it.
| | 04:11 |
It just simply set it aside and assigned
it the same way as if we assigned a
| | 04:14 |
variable a value.
We just assigned a block of code to the
| | 04:18 |
say hello function.
Now, in order to call it, we need to say hello.
| | 04:23 |
Let's try it now.
Save that, let's go back over here.
| | 04:27 |
And sure enough, Hello World pops up.
Now, the parentheses here are not optional.
| | 04:32 |
If we are calling a function, we want to
make it clear that it is a function.
| | 04:35 |
If we take it away, then PHP sees this as
something different.
| | 04:39 |
Over here, just reload it so you can see
the difference.
| | 04:40 |
You can see that we get a notice saying
that we are using an undefined constant,
| | 04:45 |
say hello.
And that's not what we want at all.
| | 04:47 |
We were trying to call our function.
So, when we call our functions, even if
| | 04:50 |
there's no arguments.
We're going to use just two parentheses afterwards.
| | 04:55 |
So, the correct way to say this is that
we are calling the function.
| | 04:59 |
We're calling the say_hello function.
Alright, now let's try this with our
| | 05:02 |
first argument.
Let's come down here and let's define
| | 05:06 |
another function, but this one is going
to be say_hello_to, underscore to.
| | 05:11 |
And then, we'll put an argument in here
and that will just be word, alright?
| | 05:16 |
So that's our first argument, is word.
And then, we have the ability to use that
| | 05:21 |
argument inside our function.
So for example, I can now have dollar
| | 05:25 |
sign word, right?
See how that works?
| | 05:28 |
So this is now accessible to me inside
me.
| | 05:30 |
Just like my loop local variables are
accessible inside of loop, well here.
| | 05:36 |
The function arguments are accessible
inside the function.
| | 05:39 |
So let's let's try that out now.
We're going to call it with say,
| | 05:43 |
hello_to, and we need to provide a word.
So let's just start with world, right?
| | 05:50 |
Let's go back, save it.
Reload the page and there you go.
| | 05:55 |
See we get the second time say Hello
World.
| | 05:57 |
Now, why use arguments?
Well, because we have flexibility then.
| | 06:00 |
That's the nice thing about arguments is
we can reuse that code so we can have it
| | 06:04 |
do something slightly different based on
the input that we pass in.
| | 06:08 |
So, say hello to everyone.
And that returns hello everyone.
| | 06:12 |
Now this is a super simple example but
regardless of what task we're trying to perform.
| | 06:17 |
What we want is to take input and base
don that input, get back different output.
| | 06:22 |
We don't want the same output every
single time.
| | 06:24 |
Now, of course, we can't call a function
that doesn't exist.
| | 06:28 |
Hello loudly.
Let's try that.
| | 06:31 |
Come back over here, reload it.
You'll see that you get fatal call to
| | 06:34 |
undefined function on say hello loudly.
It's undefined, we didn't define it like
| | 06:39 |
we did these other ones.
Now, it may surprise you to know, though,
| | 06:43 |
that we can actually take this and let's
do say Hello Loudly.
| | 06:49 |
And we'll just make this, Hello World, in
all caps.
| | 06:53 |
Let's go back and try this now.
Reload it and it worked but notice I
| | 06:58 |
defined it after I called it, right?
It doesn't do it sequentially.
| | 07:03 |
In php3, it did have to precede calling
it, but php4 and 5 actually pre-process
| | 07:10 |
the page to find all of the functions.
First, now it's still a good programming
| | 07:14 |
practice to put them before calling them.
Because someone looking at your code, is
| | 07:17 |
more likely to be reading from the top
down.
| | 07:19 |
And to be surprised that they haven't
seen it defined.
| | 07:22 |
So, don't get into the habit of defining
it later.
| | 07:24 |
But it's not going to cause you a problem
if you do accidentally put a function
| | 07:28 |
after calling it.
It's still going to be available to you.
| | 07:31 |
Now once we've defined a function,
though.
| | 07:33 |
We can't redefine it.
Just do another say_hello_loudly here.
| | 07:38 |
And I'll just change this to we can't
redefine a function.
| | 07:42 |
Let's just come over her and try that
out.
| | 07:46 |
And you'll see fatal error, cannot
re-declare say_hello_loudly.
| | 07:51 |
Previously declared at this spot.
It tells us where it's located.
| | 07:54 |
So, we can't do that.
So, I'm just going to comment that out so
| | 07:57 |
that won't cause our page to break
anymore.
| | 07:59 |
Now, you can define functions inside
other functions inside if statements,
| | 08:04 |
inside loops.
But this is really advanced usage and can
| | 08:07 |
lead to many problems and unexpected
results for beginners.
| | 08:10 |
So for now, be sure that you're always
defining your functions at the quote root
| | 08:14 |
of your PHP page, not inside any other
PHP structure.
| | 08:18 |
Now that we understand the fundamentals
of defining functions, let's explore how
| | 08:22 |
to use them a little further in the next
movie.
| | 08:24 |
| | Collapse this transcript |
| Function arguments| 00:00 |
In the last movie, we learned how to
define and to call functions.
| | 00:03 |
In this movie, we'll get a better
understanding about how we go about
| | 00:07 |
working with arguments to our functions.
Let's create ourselves a new file this,
| | 00:11 |
and open up Basic.html.
And we'll do Save As, and this one is
| | 00:16 |
going to be called
functions_arguments.php.
| | 00:23 |
Then let's change this up here to be
Functions: Arguments.
| | 00:27 |
And let's start out with just the basic
say_hello_to function that we used in the
| | 00:32 |
last movie, which has one argument, word.
Right?
| | 00:35 |
So we've already worked with arguments
before, we've seen one.
| | 00:38 |
Here it is, we've defined a variable,
word, that we know we're going to pass a
| | 00:42 |
value into.
We pass in the string, Everyone, to that.
| | 00:46 |
And that Everyone gets assigned to word.
And that's what's available for use
| | 00:50 |
inside the function then.
We already know how that works.
| | 00:54 |
I just want to show you a slight revision
of that.
| | 00:55 |
Let's say that we have another variable
here called, John Doe.
| | 01:02 |
And instead of passing in Everyone.
Well we're going to pass in the variable name.
| | 01:08 |
So name equals John Doe.
Say_hello_to name.
| | 01:12 |
And that gets then passed into the
function.
| | 01:14 |
And it should say Hello to John Doe.
Let's try it.
| | 01:17 |
Let's go back to FireFox and instead of
Functions Defining, it's going to be
| | 01:22 |
Function Arguments.
Hello John Doe.
| | 01:26 |
Works exactly as we expected.
And we're passing in a variable, but a
| | 01:29 |
variable is just a reference to a string.
So it shouldn't be a surprise to us that
| | 01:33 |
this works.
But I want you to notice that even though
| | 01:36 |
the variable is name outside the
function, the function assigns that to
| | 01:41 |
the variable word.
And word is what it uses inside the function.
| | 01:46 |
These two don't have to match.
So don't worry about that.
| | 01:49 |
Whatever it has out here.
That's the one that exists outside the function.
| | 01:52 |
The function has its own sort of
contained world of variables.
| | 01:56 |
We'll talk more about that a little later
on.
| | 01:59 |
For now, I just want you to see the two
don't have to have a relationship.
| | 02:02 |
Now, let's try some functions with more
then one argument.
| | 02:05 |
If you remember back when we used the
built in PHP function str_replace, we had
| | 02:10 |
three arguments to it.
The first was the string quick, the
| | 02:14 |
second was super fast, and the third was
a string That we assigned to the variable third.
| | 02:20 |
Now, of course, this is already
predefined for us by PHP.
| | 02:23 |
But if we were to define our own, it
would look something like this.
| | 02:26 |
Function, str_replace, and then the three
arguments, right, that go with it.
| | 02:31 |
And those might be find, replace, and the
target.
| | 02:34 |
So we're finding one string.
We're going to replace it with another string.
| | 02:37 |
Adn we're going to do that inside the
target string.
| | 02:40 |
That's the third one.
Now, it doesn't matter what this function does.
| | 02:43 |
What the statements are inside of it.
What I want you to see here is the
| | 02:46 |
relationship between those three when
we're defining the function and the three
| | 02:51 |
when we're calling the function.
Notice that there are always three of them.
| | 02:55 |
That fit each of those spots, and they're
always in the same order.
| | 02:58 |
That matters when we're having multiple
arguments.
| | 03:01 |
The order matters, and the number
matters.
| | 03:04 |
So let's try our own.
Let's start down here, I'll create new
| | 03:07 |
PHP tags, and let's put in function.
We'll call this one, better_hello.
| | 03:16 |
And for now, I'll just flush it out like
that.
| | 03:20 |
All right, so let's have three arguments
here to this.
| | 03:22 |
This one will be greeting, and then we're
going to pass in the target, and then the
| | 03:29 |
punctuation which I'll just abbreviate
punct.
| | 03:32 |
And then, I can use those three values
inside my function as I go about the work
| | 03:36 |
of my functions.
I'm going to say echo back the greeting.
| | 03:40 |
And then let's concatenate that with a
space.
| | 03:43 |
And then we'll put the target, that's the
person that we're greeting.
| | 03:47 |
And then followed by, concatenating that
with the punctuation.
| | 03:51 |
And last of all, put br tag at the end,
just to make it look nice, and a
| | 03:55 |
semicolon for the whole thing.
See how that works?
| | 03:59 |
So we have three different ones that we
are working with.
| | 04:01 |
Now, these don't have to be in this
order, we could jumble these around.
| | 04:04 |
The important thing though is that
whatever order we define them in, that
| | 04:08 |
that's the order we called them in.
We must always call it with the same
| | 04:11 |
number of arguments and in the same
order.
| | 04:14 |
So better_hello and then we can now say
that our greeting is going to be Hello.
| | 04:19 |
We'll say hello to the person that is
stored in the variable name.
| | 04:23 |
And also, we're going to have our
exclamation point as our punctuation.
| | 04:27 |
Alright, so let's just try this out and
make sure that our three arguments are
| | 04:31 |
all working.
There we go, we get Hello John Doe again.
| | 04:33 |
And of course, it's flexible.
We could just as easily have changes from
| | 04:39 |
the greeting and the three exclamation
points after it.
| | 04:43 |
Right?
Greetings John Doe with three exclamation
| | 04:47 |
points is what comes back.
Now, as I said, we have to have the same
| | 04:50 |
number of items but let's try putting in
null here for that last item.
| | 04:55 |
So we're providing a value.
But that value is null.
| | 04:58 |
Let's come back over and reload the page
and you'll see what it did.
| | 05:01 |
It converted null into a string, which is
nothing.
| | 05:04 |
We could have also just as easily
provided an empty string and it would
| | 05:07 |
have done the same thing.
The important thing is that we have
| | 05:09 |
something there in that place.
That if we are asking for three things
| | 05:13 |
in, when we define it, that we are
sending three things whenever we call it.
| | 05:18 |
There has to be agreement there.
A little later, we'll talk about how we
| | 05:21 |
can set default values for these
arguments.
| | 05:23 |
So that we don't always have to send the
exact same number.
| | 05:26 |
But before we do that, I want us to first
talk about return values from a function.
| | 05:30 |
We'll do that in the next movie.
| | 05:31 |
| | Collapse this transcript |
| Returning values from a function| 00:00 |
All of our functions, so far, have been
using echo to send output to the user's screen.
| | 00:05 |
But more often than not, we don't want
that behavior.
| | 00:07 |
Usually, we'd like to get a result back
from the function, and then decide what
| | 00:11 |
to do with that result ourselves.
We might end up deciding to echo it to
| | 00:14 |
the screen, but we might instead take
that value and continue to use it to do
| | 00:18 |
more processing.
If it outputs from inside the function,
| | 00:21 |
then we lose that flexibility.
It's better to get back a value.
| | 00:24 |
And for that, we need to understand, how
to return values from a function.
| | 00:28 |
Let's open up basic.html, and we're
going to do Save As on this.
| | 00:33 |
We're going to call this one
functions_returnvalues.php.
| | 00:42 |
(SOUND) Functions: Return Values.
So let's just write a simple function here.
| | 00:51 |
I've got an add function.
It takes two arguments value 1 and value 2.
| | 00:55 |
It adds them together and assigns them to
sum.
| | 00:58 |
So let's just load this up in Firefox to
begin with.
| | 01:01 |
Let's bring this up.
And instead of functions arguments,
| | 01:05 |
instead now we're going to be loading up
return values with no space in between.
| | 01:09 |
Alright, I get nothing back.
You can see it did load the page, but all
| | 01:12 |
I did was define the function.
I haven't actually called it yet, so
| | 01:15 |
let's try calling it now.
Add 3,4.
| | 01:19 |
All right, let's save that.
Let's go back and reload the page.
| | 01:23 |
Still didn't get anything back.
Why not?
| | 01:26 |
Well, it's because we're not echoing
anything from inside the function like we
| | 01:29 |
did before.
We could put an echo here, echo $sum and
| | 01:35 |
that would now output a value, but that's
not as flexible.
| | 01:38 |
That doesn't return the addition to us so
work with it.
| | 01:41 |
The only option it gives us, the only way
to get a value out is to print it to the browser.
| | 01:45 |
Now we could try printing this value from
outside the function.
| | 01:48 |
Let's try that.
Lets do after we add them together, let's
| | 01:53 |
echo that value.
And it comes up and says nope undefined
| | 01:56 |
variable, because sum is a variable
that's only inside the function.
| | 02:01 |
It doesn't exist outside the function.
So we can't use this as a way to get
| | 02:05 |
values out of there.
Instead, what we want to do is we want to
| | 02:09 |
return a value from the function.
So we would have return and then what we
| | 02:16 |
want it to return, sum.
So now it'll return sum to us.
| | 02:20 |
And then, we need to catch that result
here by saying the result is equal to Add
| | 02:28 |
3 and 4.
Now, we get back the result and we have
| | 02:32 |
the ability to echo that result to the
screen.
| | 02:35 |
Let's take a look at that.
And now we get the value of 7.
| | 02:39 |
Now, we don't have to echo this to the
screen.
| | 02:43 |
We could, instead, do more operations
with it, right?
| | 02:45 |
We could have, now the result is to take
5 and add that to result, what result was previously.
| | 02:53 |
I can actually name these results 1 and 2
if that makes it a little clearer instead
| | 02:59 |
of overriding the other one.
So result 2 know is equal to result 1
| | 03:03 |
plus 5, come back here, and we get 12.
Right.
| | 03:07 |
Much more flexible.
We have the ability to output it or the
| | 03:10 |
ability to keep working with it...
The best practice when you have functions
| | 03:15 |
is to always have a return value.
Explicitly, say, what does this function return?
| | 03:21 |
Maybe what it returns is null.
Maybe what it returns is the original
| | 03:26 |
input again.
Maybe your, your input was an array, and
| | 03:29 |
so the output is to output that same
array again.
| | 03:32 |
But you always want to return something
from your functions.
| | 03:35 |
The PHP functions have return values.
All of those are listed on the PHP.net website.
| | 03:41 |
There's usually a section that say's the
return values.
| | 03:45 |
It tells you what the inputs are and it
tells you what the outputs are, so you
| | 03:48 |
can look those up.
Now the return also exits the function immediately.
| | 03:54 |
It's similar to how a break works.
Where break just got us out of a loop
| | 03:57 |
immediately, or out of a switch statement
immediately.
| | 03:59 |
Well, return works the same way for
functions.
| | 04:01 |
It says, alright, just stop whatever
you're doing.
| | 04:04 |
Let's exit the function and return this
value.
| | 04:07 |
This is the thing that needs to be passed
out of the function.
| | 04:10 |
No more processing required.
And even if we put it inside loops or
| | 04:13 |
switch statements that are in the
function, it will immediately exits from
| | 04:16 |
those as well.
So, let's try an example here.
| | 04:19 |
Let's go back.
We did the Chinese Zodiac before.
| | 04:21 |
Let's paste an example using that again.
Should put a br tag here.
| | 04:25 |
I've defined the Chinese Zodiac as a
function.
| | 04:27 |
So, here it is, Chinese Zodiac, we pass
in a year.
| | 04:31 |
And then it takes that year and does the
same processing we did before.
| | 04:34 |
But now, instead of having assignment to
a variable followed by break.
| | 04:38 |
Instead now, we're just going to return
the value, that's it.
| | 04:43 |
So, case 0, return Rat.
Done.
| | 04:45 |
No break required.
And then, let's jump down here, and let's
| | 04:50 |
actually call our function.
So let's say, $zodiac is going to be
| | 04:55 |
equal to chinese_zodiac.
And let's put in 2013 as the year.
| | 05:03 |
And then after we've done that, we'll
echo back a string, 2013 is the year of
| | 05:09 |
the, and put the zodiac here.
I'll end it with a period.
| | 05:15 |
And it'll be our tag.
And a semicolon at the end.
| | 05:19 |
Alright, so let's bring that up and try
it.
| | 05:23 |
There it is.
See how that works?
| | 05:25 |
Now we have a function that (INAUDIBLE)
wraps all of this logic for us.
| | 05:29 |
So now, whenever we want to call this, we
can just simply call it with the new number.
| | 05:34 |
Let's say that we're looking for 2027,
2027 is the year of the, what?
| | 05:40 |
Goat.
Now we don't have to assign this to a
| | 05:46 |
variable here.
Sometimes it's cleaner to do it that way
| | 05:49 |
but I can do this in-line as well.
We can't do it with the curly quotes but
| | 05:54 |
we can do it if we break those and use
append and then we can just put it right there.
| | 06:01 |
See how I've done that?
Let me just widen my window a little bit.
| | 06:04 |
So I'm concatenating there.
2027 is the year of the, then call the
| | 06:09 |
function and then the br tags.
Let's save it and go back and check that out.
| | 06:14 |
And there it is.
It looks exactly the same.
| | 06:15 |
Now, by the way, I think it's a good
practice to rarely have functions which
| | 06:20 |
output directly from inside the function.
Now that's what we've been doing before
| | 06:24 |
now as examples because we didn't know
how to do return values yet.
| | 06:28 |
But most times I think it's better to
build up text inside the function and
| | 06:33 |
assign that to a variable.
And then, return the value of that
| | 06:37 |
variable at the end.
Then we can echo that returned value if
| | 06:40 |
we want, or we can do something else with
it.
| | 06:42 |
But it's better, I think, to leave echoes
out of your function.
| | 06:46 |
So, for example, we could redo our hello
function that we had before.
| | 06:51 |
(SOUND).
Right?
| | 06:53 |
And we had better_hello.
Instead of echoing from inside that
| | 06:56 |
function, just change it to a return.
Return that result, and now, we can just
| | 07:02 |
as easily call echo better_hello, with
Hello, and then, John Doe, followed by an
| | 07:14 |
exclamation point.
Right?
| | 07:16 |
We can still call it that way but see how
much more flexible this is.
| | 07:20 |
We could take this and we could call up
case on it or title case or something
| | 07:23 |
else before we output it.
We have more flexibility.
| | 07:25 |
So I think it's better to leave echo
statements out of your functions and
| | 07:30 |
instead use return values.
| | 07:32 |
| | Collapse this transcript |
| Multiple return values| 00:00 |
In the last movie, we learned how to
return values from a function by using return.
| | 00:05 |
In this movie, we'll learn how to return
multiple values from function.
| | 00:08 |
Well, sort of, you see a function can
only return a single value.
| | 00:14 |
That's a hard and fast rule about
functions, that it returns only a single value.
| | 00:18 |
So what if we need to return more than
one value?
| | 00:21 |
Well first let's create an example so we
can look at the problem more closely.
| | 00:24 |
I'm going to open up basic.html and I'll
do a Save As on that and I'm going to
| | 00:29 |
call this Functions Multiple Returns.
We'll give it the same name here.
| | 00:39 |
Functions multiple returns...
Alright let's start with a new function.
| | 00:46 |
And this function I've called add_subt.
Short for subtract.
| | 00:51 |
So it's going to add and subtract two
values.
| | 00:53 |
Value one and value two.
We're going to add them together.
| | 00:56 |
And then we're going to subtract them.
So we know how to use return and how to
| | 01:00 |
return add.
But how can we also return subt two?.
| | 01:03 |
Let's just try this real quick and see.
And let's go ahead and have result.
| | 01:07 |
It's going to be equal to add subt, let's
do 10,5.
| | 01:13 |
Right and then we'll echo back the
result.
| | 01:16 |
All right, so, let's just try this and
see what we get.
| | 01:18 |
Let's go to Firefox and instead of return
values going to go to Multiple Returns.php.
| | 01:27 |
Alright.
It gave me back 15.
| | 01:29 |
Now that's just the value of adding them
together.
| | 01:30 |
It has nothing to do with subtracting.
Now, you might try.
| | 01:33 |
Well, let's say, what if we had a comma?
Maybe just do comma subt, save.
| | 01:39 |
Let's go back, reload the page.
Nope, parse error.
| | 01:42 |
Unexpected comma.
So it won't let us do that.
| | 01:44 |
It's nothing that is that simple.
The problem is that functions only allow
| | 01:48 |
us to return one thing.
So that's it.
| | 01:51 |
We're limited to one thing that we can
return.
| | 01:53 |
So let's think for a second, we have two
things that we want to return.
| | 01:56 |
but a return will only let us pass back
one thing.
| | 02:00 |
Is there a single PHP entity that will
hold more than one entity?
| | 02:04 |
Yes, arrays.
Arrays to the rescue.
| | 02:08 |
What we can do is return an array here
instead.
| | 02:11 |
So array, and then add comma, and
subtract.
| | 02:18 |
There we go.
Alright, we'll remove that comment now.
| | 02:20 |
Now it will return an array.
So what we're getting back here for
| | 02:24 |
result is an array.
Just to make that clear, this is result array.
| | 02:28 |
Now we don't want to echo the result
array anymore, we probably want to do
| | 02:33 |
something a little nicer.
So let's go to add.
| | 02:36 |
And then we'll concatenate that with
result array zero to get the first value,
| | 02:42 |
that's the add.
And then we'll put br tags at the end.
| | 02:45 |
And then let's do that same line again.
Let's make the next one, though, subt.
| | 02:50 |
We'll make it result array one.
Alright, let's save it.
| | 02:55 |
Let's go back and try that.
There we go.
| | 02:57 |
Add and subtract.
We got both results back and we did it by
| | 03:01 |
using this array as the vehicle to get
more than one value back, and that's
| | 03:05 |
great because arrays can return a lot of
stuff, so we can return two values, three
| | 03:10 |
values, four values, its unlimited, we
just have to wrap them up in this single
| | 03:15 |
structure, this single array, because
that way that array can be our return value...
| | 03:20 |
Now, I don't know about you but I think
that this feels a little bit clunky to me.
| | 03:25 |
I really don't like the idea of having to
write this in my code all the time, and
| | 03:28 |
this isn't very descriptive of what it
does either.
| | 03:31 |
Fortunately, there's another, better way
to do it.
| | 03:33 |
And that is to use another PHP function
called list.
| | 03:37 |
You use list to assign array values to
variables.
| | 03:40 |
So for example let's just copy all of
this.
| | 03:44 |
And we'll drop down here, paste it in.
This time, though, let's just change it
| | 03:49 |
add and subtract, we'll make it 20 and 7
and instead of result array, we're just
| | 03:55 |
going to say list and then in
parentheses, provide the variables that
| | 04:00 |
we want to use.
I'll call this one add result and I'll
| | 04:04 |
call this one subt result.
Alright, so that's what it does.
| | 04:08 |
It takes these values from the array and
it assigns them to add result and subt result.
| | 04:14 |
Then here I can use those instead, a
little less clunky.
| | 04:18 |
All right let's just try that out.
Reload the page and there we go, 27 and 13.
| | 04:26 |
So I think list is super-handy because it
allows us to take all those elements that
| | 04:30 |
we just packaged up into an array to get
out of the function.
| | 04:34 |
And immediately break them back down,
unpack it, and assign them two variables
| | 04:39 |
that have good, commonsense names that
are easier to identify than resultarrayone.
| | 04:46 |
The combination of the two using array.
And list is the best way to return
| | 04:50 |
multiple values out of your functions.
| | 04:52 |
| | Collapse this transcript |
| Scope and global variables| 00:00 |
We've seen how the variable names inside
the function and outside the function are
| | 00:04 |
unrelated, and we wrote a function to do
addition.
| | 00:07 |
The variable's sum was only available
inside the function.
| | 00:11 |
We couldn't use echo outside the function
to retrieve that value, we had to use a
| | 00:15 |
return value instead.
Having this division between the
| | 00:18 |
variables inside a function and outside a
function, is known as variable scope.
| | 00:23 |
A variable isn't accessible all the time.
It's only accessible within it's context.
| | 00:29 |
A variable created inside a function, is
by default only accessible In the
| | 00:33 |
function we say that the function is the
variable's scope.
| | 00:38 |
In PHP there are two main scopes.
There is the Global Scope and there is
| | 00:42 |
the Local Scope.
Let's take a look at them so we can see
| | 00:44 |
the difference.
Let's start by taking basic dot HTML,
| | 00:48 |
calling Save As on it.
We're going to call this functions_scope.
| | 00:55 |
And I'm going to end it with php, Save
it.
| | 00:58 |
Now, let's create ourselves a little
example, here.
| | 01:00 |
Let's call this Functions: Scope.
I'll start my php tags.
| | 01:07 |
And let's assign a value to bar equal to
outside.
| | 01:14 |
This is going to be in the global scope.
Now let's write a function.
| | 01:21 |
Function, call it foo and inside that
function that's once again set bar equal
| | 01:28 |
to, this time it'll be inside.
That is going to be local scope, alright?
| | 01:36 |
So now down here let's echo 1 and
catenate with the value of bar followed
| | 01:44 |
by br tags, there we go.
And then we'll call the foo function.
| | 01:50 |
(SOUND).
And then, once again, we'll just echo
| | 01:54 |
back what bar is, this time, with the
number 2 in front of it, so that we can
| | 01:57 |
really see the difference, alright?
Clear what we're doing?
| | 02:00 |
We're looking at the value of bar.
Then we're calling the foo function,
| | 02:04 |
which is setting the value of bar.
And then we're echoing it again.
| | 02:08 |
Alright, let's try it.
Let's see what we get.
| | 02:10 |
Le'ts go to Firefox, instead of multiple
returns, it's going to be functions_scope.
| | 02:17 |
Okay, they both returned outside to us.
So this had no effect, what so ever, we
| | 02:22 |
did not set it to inside and that's
because, this bar here is not the same as
| | 02:29 |
this bar here.
Even though they have the same name, they
| | 02:33 |
have different scope.
So it's actually a different variable as
| | 02:36 |
far as PHP is concerned.
We can even test that further by going
| | 02:40 |
inside the function, and let's put a new
line there, let's say if is set bar,
| | 02:44 |
then, and let's just do our echo here.
We'll just paste it up here, but this
| | 02:55 |
time, we'll do it from inside foo, all
right?
| | 02:58 |
So if it's set, then let's echo it inside
the function.
| | 03:01 |
Let's take a look at what that looks
like, see?
| | 03:05 |
Nothing came out, because it wasn't set.
Now, it does seem to matter if we provide
| | 03:10 |
bar as an argument to foo, right?
We've also gotta have it down here.
| | 03:15 |
Let's send bar in.
Now, this is the bar that was set here,
| | 03:19 |
all right?
That's what we're passing in.
| | 03:21 |
Think about what's going to happen and
then let's take a peak.
| | 03:26 |
Notice that it's outside all the way
through.
| | 03:28 |
It still didn't change it, right?
Because it got in here and it was local scope.
| | 03:33 |
When it exits the function, well, this
bar right here, it's the same bar that we
| | 03:38 |
were working with before.
The change didn't stick, because these
| | 03:41 |
changes were not to this variable, they
were to the local variable, the local scope.
| | 03:47 |
Now there is a way around this, though.
There is a way for us to bring in the bar
| | 03:53 |
that's outside and to use it inside the
function.
| | 03:56 |
If we go back to our house metaphor, it's
like opening a window so that we do have
| | 04:00 |
access to it.
And the way that we do that is by using
| | 04:04 |
the keyword global, followed by bar.
Now, we'll take this away.
| | 04:09 |
We no longer have the argument anymore.
So let's go ahead and leave this all in here.
| | 04:14 |
And let's now, we've declared it as being
global.
| | 04:17 |
Let's take a look at what happens.
There we are, did you see it change?
| | 04:22 |
So now, it's outside, when we echo it the
first time, we call the foo function,
| | 04:30 |
which brings in from the global scope,
bar.
| | 04:34 |
Then, we have access to it, so now this
bar, it's checking to see if it's set,
| | 04:39 |
that's the global one.
That's the one up here that was outside,
| | 04:41 |
so that what it outputs for foo.
Then we set that same one equal to inside.
| | 04:47 |
When we leave the function we're back in
the global scope but our global scoped
| | 04:52 |
variable is what changed.
So that's why we get our third result as
| | 04:56 |
being inside.
So global allows you to bring in globally
| | 05:00 |
scoped variable for use inside a local
scope.
| | 05:04 |
Now use this with caution because you are
effecting that global scope variable,
| | 05:09 |
from somewhere deep inside a function.
That's not immediately obvious to you
| | 05:12 |
when you call foo that that's what you're
doing.
| | 05:15 |
That you might be changing some of these
values, right?
| | 05:18 |
It's not very transparent and easy to
see.
| | 05:21 |
You might be better off passing in a
value than changing the value and
| | 05:25 |
catching the return value that comes out
of it.
| | 05:27 |
That would be a lot clearer.
And definitely don't use globals as a
| | 05:31 |
crutch or a way to keep from writing good
code.
| | 05:34 |
In my opinion you probably won't use more
than three, four, or five variables that
| | 05:40 |
get declared as globals in most projects.
It really is just some special cases of
| | 05:45 |
things the thou have to bring in.
Most of the time you're going to want to
| | 05:49 |
use your arguments and return values
instead.
| | 05:52 |
Now even though you may never use that
keyword global, what's most important is
| | 05:56 |
that you understand the difference
between the two scopes.
| | 05:59 |
And how they either interact or don't
interact.
| | 06:01 |
| | Collapse this transcript |
| Setting default argument values| 00:00 |
We've already learned that the arguments
when you call a function must match the
| | 00:04 |
arguments that were used to define the
function.
| | 00:06 |
If you define it with three arguments,
then you need to provide three arguments
| | 00:09 |
as input.
However, there is a way around that, by
| | 00:12 |
setting default argument values.
Let's see how.
| | 00:15 |
So we'll open up basic.html.
We'll call Save As on that.
| | 00:19 |
And let's call this
functions_defaults.php.
| | 00:25 |
Functions default argument values.
All right, so to start off with let's
| | 00:33 |
write ourselves a little function.
And I'm going to call this function, paint.
| | 00:38 |
Alright, so function paint and, let me
just put the curly braces there.
| | 00:43 |
And this paint is going to take a color
as an argument.
| | 00:47 |
The work that this function is going to
do is we're going to return a string,
| | 00:50 |
which is the color of the room is, and
then we'll have our curly braces with the color.
| | 00:58 |
And then I'll put a period and br tag at
the end, followed by the semicolon.
| | 01:03 |
Okay.
So now, we know how to call that, right?
| | 01:05 |
You can simply say echo paint and blue.
Now, let's bring that up in a window and
| | 01:12 |
check it out.
So here we go into Firefox and now set
| | 01:15 |
Function scope.
Here we are looking at Defaults.
| | 01:19 |
The color of the room is blue, right?
Makes sense.
| | 01:21 |
We have one argument defined and we're
passing in one thing.
| | 01:25 |
Now, if we were to take blue away, then
we've got an error.
| | 01:29 |
It'll say, oops, sorry, missing argument
one for paint.
| | 01:33 |
Right?
And then, it also says, undefined color.
| | 01:37 |
Here, because we didn't pass anything in.
So that won't work.
| | 01:40 |
We would need to pass in a placeholder,
at least an empty string, something that
| | 01:43 |
it can work with.
But if we use a default argument, the way
| | 01:46 |
that we do that is we do an assignment
right after the argument.
| | 01:49 |
So we just do equals, and then let's put
in a color here.
| | 01:53 |
So let's say that the color's going to be
red.
| | 01:56 |
All right.
So now, let's just try this again.
| | 01:58 |
Reload.
The color of the room is red.
| | 01:59 |
See what happens?
It didn't get a value, so it get here and
| | 02:00 |
says color, and if you don't have a value
for color, then go ahead use red.
| | 02:00 |
We can have more than one of these.
Let's add another one, here.
| | 02:13 |
Let's say that we're going to have the
room and the room, by default, is going
| | 02:17 |
to be the office, we need a comma between
those, right?
| | 02:22 |
So room equals office, color equals red.
So now, we need to pass in two things.
| | 02:28 |
Let's also drop in the room here.
So room and then let's come back over here.
| | 02:35 |
The color of the office is red.
'Kay, I can also pass in something else.
| | 02:40 |
I could have bedroom and blue.
I'll just leave my old one as well, just
| | 02:47 |
so that we still have that paint.
You can see the difference.
| | 02:51 |
So now I'm going to use bedroom and blue,
and you'll see that we get the default
| | 02:54 |
behavior followed by our custom behavior.
See how that works?
| | 02:57 |
Now, let's try one more.
This time instead of passing in blue,
| | 03:01 |
let's pass in null and see what happens.
Back here, color of the bedroom is null.
| | 03:09 |
Even though it was null.
All right.
| | 03:12 |
We did not pick up this red value.
It did not go to the default.
| | 03:16 |
We passed in something.
That something happened to be null, but
| | 03:21 |
it was something.
The defaults only take place if nothing
| | 03:25 |
fills that spot.
And I don't mean nothing as in null.
| | 03:28 |
I mean no value gets sent at all.
Don't let that trip you up.
| | 03:33 |
Not sending something and sending nothing
are not the same thing.
| | 03:37 |
Now, also, while it will work for you to
just do bedroom and then provide nothing
| | 03:42 |
after it, all right.
The color of the bedroom is red, picked
| | 03:47 |
up that value there.
We can't do something like, blue, let's
| | 03:52 |
come back here, reload, the color of the
blue is red.
| | 03:56 |
That's what we got back, because we can't
leave out this first one.
| | 04:00 |
The order still matters, and something
has to hold that position.
| | 04:04 |
We can't put null here, because we know
what that would do.
| | 04:06 |
So something has to hold that.
So because of that, you going to want to
| | 04:09 |
make sure that any required values are
going to be listed first.
| | 04:13 |
The optional ones, the ones that have
defaults need to be listed.
| | 04:17 |
At the end of the argument list.
Now, the argument order doesn't matter to
| | 04:21 |
the functions operation.
It doesn't care what order they come in,
| | 04:24 |
as long as they match the way that it's
called.
| | 04:27 |
It can sort it out, it can swap color and
room and it won't make any difference at all.
| | 04:31 |
But you would not to do something like
this.
| | 04:34 |
All right, here color is required and
room has a default value.
| | 04:38 |
Well, we've always got to provide
something here.
| | 04:41 |
So this will never happen.
We'll never get that office because
| | 04:45 |
something will always have to be provided
here because this one is mandatory.
| | 04:50 |
That's all there is to working with
default argument values.
| | 04:53 |
It's pretty simple to provide defaults,
but there are some gotchas in there that
| | 04:57 |
can make it a little trickier than it
first seems.
| | 04:59 |
| | Collapse this transcript |
|
|
9. DebuggingCommon problems| 00:00 |
Now that we have a lot of the PHP
fundamentals under our belt.
| | 00:03 |
In this chapter, we'll discuss ways to
troubleshoot and debug your PHP code.
| | 00:07 |
And we'll start by looking at common
problems and talk about the errors that
| | 00:11 |
PHP returns.
And finally, look at some debugging techniques.
| | 00:14 |
Let's start out by looking at the common
problem.
| | 00:16 |
One of the most frustrating problems that
you can run into is getting a webpage
| | 00:20 |
that has no output on it at all.
There's no real information there to go on.
| | 00:24 |
It just seems like the page is not
working.
| | 00:26 |
The first thing you should do is try to
access a basic HTML page.
| | 00:30 |
Forget about PHP for a second.
Take it out fo the equation.
| | 00:33 |
Make sure your web server is running.
And that it's able to return HTML pages.
| | 00:38 |
That also helps to make sure that we're
accessing the right directory in the file
| | 00:41 |
system that we think we are.
Once we get that working, then we can
| | 00:44 |
bring PHP into the equation.
And we can try to create a basic PHP page.
| | 00:48 |
And see if we can get our web server to
process the PHP.
| | 00:52 |
Remember that we have that phpinfo
function that we can use to drop into a
| | 00:55 |
simple PHP page.
It will confirm that PHP is working and
| | 00:59 |
it also gives us lots of configuration
information that can be useful.
| | 01:02 |
Once we know that the web server's
running, that it's finding files in the
| | 01:05 |
right place.
That it can return those files and the
| | 01:07 |
PHP is running, then the problem is
probably in our code.
| | 01:11 |
You want to make sure the display errors
is turned on and configured properly.
| | 01:15 |
We're going to talk about that more in
the next movie.
| | 01:17 |
You maybe encountering an error and PHP
just isn't showing you that error so
| | 01:21 |
you're getting a blank page instead.
Now, let's talk about some of the common
| | 01:24 |
errors that you might have created in
your code that would cause those errors.
| | 01:28 |
The first and probably most common is
just simple typos.
| | 01:32 |
Maybe you just misspelled a variable,
maybe you commented out something that
| | 01:36 |
you didn't mean to comment out.
Typos are just a way of life when you're
| | 01:39 |
doing programming.
And you're going to have to get used to
| | 01:41 |
combing over your code very carefully.
Very thoroughly, looking for mistakes
| | 01:46 |
that you've made.
A very common mistake in PHP is missing
| | 01:49 |
the semicolon at the end of the line.
Remember those semicolons are super important.
| | 01:53 |
PHP is whitespace independent so it
doesn't matter if we have returns.
| | 01:57 |
But the semicolons are what tell PHP when
one command ends and another one starts.
| | 02:02 |
It's also super common to have missing
closing braces or missing closing quotes.
| | 02:07 |
And that would be that you open a
parentheses, but you don't close it.
| | 02:10 |
Especially once you start nesting
parentheses inside of parentheses.
| | 02:13 |
It can get hard to keep track.
Most code text editors either helpfully
| | 02:17 |
add that other parentheses for you when
you create the first one.
| | 02:21 |
But they will often also highlight the
opening parentheses when you type a
| | 02:26 |
closing parentheses.
So, it helps you know what that
| | 02:28 |
parentheses is paired up with.
So, you'll know when you have enough of them.
| | 02:32 |
Another problem can be from having case
sensitive variable names.
| | 02:35 |
Remember that something like myvar, all
lower case, and myvar with an upper case
| | 02:39 |
V, are two different variables.
That's why I think it's a good practice
| | 02:42 |
to pick one style, and stick with it.
For me, that's why I like using all lower case.
| | 02:46 |
So that I never have to run into this.
All my variables are always lowercase.
| | 02:50 |
And then, the last one that's super
common especially among beginners, is
| | 02:54 |
confusing equals with double equals.
And I don't mean confusing in the fact
| | 02:58 |
that you don't know the difference, but
that you mistakenly use the wrong one.
| | 03:02 |
So, if you have a simple if statement.
If I have count equals 3, well in this
| | 03:06 |
case, I'm assigning the value 3 to count.
So, if count was equal to 10 when it got
| | 03:11 |
to this statement, and I was checking to
see is count equal to 3, well guess what?
| | 03:15 |
Now it is equal to 3.
I meant to be checking it, but instead I
| | 03:19 |
set it.
Now, these are the most common mistakes
| | 03:21 |
that people can make.
But certainly, there are a lot of other
| | 03:23 |
things that can go wrong.
Remember that the php.net website is
| | 03:27 |
great resource.
And there's lots of documentation about
| | 03:30 |
how the different functions work as well
as helpful user comments.
| | 03:33 |
Below that that might be able to help you
out with the problem you're having and
| | 03:37 |
the PHP community is quite large.
So, there's all sorts of blogs and other
| | 03:41 |
documentation out there that you can
search the internet and find that might
| | 03:44 |
help you address your problem.
| | 03:46 |
| | Collapse this transcript |
| Warnings and errors| 00:00 |
When we encounter an error in our code,
more information that we have about that
| | 00:03 |
error, the easier it's going to be to
resolve it.
| | 00:05 |
In this movie, we'll look at the Warnings
and Errors that PHP generates and learn
| | 00:10 |
how they can help you to resolve
problems.
| | 00:12 |
The first and most important thing is to
make sure that error reporting is turned
| | 00:15 |
on, we can't deal with our errors if we
can't see them.
| | 00:18 |
So we want to make sure that PHP is
reporting errors as they happen to us,
| | 00:22 |
when we're in development.
There are two ways to do this.
| | 00:24 |
The first is that we can set the error
reporting in the php.ini file.
| | 00:28 |
And we still have to do that back in the
configuration chapter.
| | 00:31 |
You just edit your php.ini file to have
display errors on, error_reporting equal
| | 00:36 |
to whatever settings we want to have for
that.
| | 00:38 |
And then, restart our web server so that
it reloads that php.ini file.
| | 00:43 |
We can also set the error porting on the
fly in our PHP code.
| | 00:46 |
This is especially useful if we are
working on a server where several people
| | 00:50 |
are sharing a copy of PHP.
The PHP ini file might not have the
| | 00:55 |
settings that we want customized for our
little bit of code.
| | 00:59 |
So in that case we can use the function
ini_set to set the value as if it had
| | 01:04 |
been set in the ini file.
So I can set display errors to be turned
| | 01:07 |
on and I can set error reporting using
the error reporting function.
| | 01:12 |
Notice that On is the word On, it's a
string, but error Reporting is using a
| | 01:17 |
constant, E_ALL is a constant, it does
not get quotes around it.
| | 01:22 |
There are a variety of values that we can
provide to error reporting.
| | 01:26 |
A lot of those are detailed in the
php.ini file, or on php.net.
| | 01:30 |
Let's just look at a couple of them.
One is that we can provide E_ALL and then
| | 01:35 |
an upright pipe followed by E_STRICT.
What this is saying is we want all errors
| | 01:39 |
and also the strict errors.
Strict is a category of errors that was
| | 01:43 |
added to PHP 5 It wasn't incorporated
into ALL until 5.4.
| | 01:48 |
So if you're using 5.4 and only 5.4, you
can omit that second part and just have e
| | 01:53 |
underscore all.
But if you're using 5.3 or anything
| | 01:56 |
before that or if you're going to be
moving back and forth.
| | 01:59 |
Then you want to have both of them so
you're seeing all errors all the time.
| | 02:02 |
Now we don't have to see all errors, we
can selectively turn off some errors.
| | 02:06 |
So if we wanted to turn off the errors
regarding deprecated code, then we could
| | 02:11 |
have E_ALL ampersand.
And then the little twiddle followed by E_DEPRECATED.
| | 02:17 |
Deprecated is a fancy word for code
that's been scheduled for removal from
| | 02:21 |
PHP, so in the future it's going to stop
working.
| | 02:23 |
I think it is generally a good idea for
you to go ahead and have those errors in
| | 02:27 |
there so that you make sure that your
code is forward compatible.
| | 02:30 |
And last, I just wanted to show you that
you can return the current
| | 02:33 |
error_reporting level by just calling
error_reporting with no argument.
| | 02:37 |
Now, what it's going to return to you is
a number.
| | 02:39 |
It's not going to return to you one of
these constants.
| | 02:41 |
These constants all represent numbers.
And it takes those and compiles them
| | 02:45 |
together into a single number that it
will return back to you when you call
| | 02:48 |
error reporting.
You can get that full list of numbers on
| | 02:51 |
the php.net website.
There's a full list of all those
| | 02:54 |
constants and what numbers correspond to
them.
| | 02:56 |
Let's take a quick peek.
So here I am on that webpage.
| | 03:00 |
And it has all the predefined constants
here, starting with Errors and Logging at
| | 03:04 |
the top.
And you can see here is the constant, and
| | 03:07 |
here is the value that corresponds to it.
If you scroll down here to the bottom
| | 03:11 |
you'll see here is e underscore all and
the number that corresponds to it.
| | 03:14 |
What it does is it takes all of those
numbers and adds them up in order to
| | 03:18 |
figure out what the current user level
ought to be.
| | 03:20 |
It also gives you a description of what
each of these error constants mean.
| | 03:25 |
This can be really useful when you
want to configure your error reporting to
| | 03:28 |
report something less than all of the
errors.
| | 03:31 |
Most times I think, you probably just
want to have it report all errors back to
| | 03:34 |
you though.
And while this page lists off many
| | 03:36 |
different categories of errors, there's
actually four main categories.
| | 03:40 |
Let's take a look at those and see what
they mean.
| | 03:42 |
The first category of errors are Fatal
Errors.
| | 03:45 |
What this means is that PHP understood
what we were asking it to do, but it
| | 03:48 |
could not execute it.
It would be like, if I asked you to walk
| | 03:52 |
from New York to London, you understood
the English sentence that I just said, it
| | 03:56 |
made perfect sense.
But you can't do it.
| | 03:58 |
When PHP encounters a fatal error, it is
fatal for your code.
| | 04:02 |
It means that the page of code cannot be
executed, and so you don't get back anything.
| | 04:07 |
It's essentially saying that PHP tried
and gave up.
| | 04:09 |
The most common cause of a fatal error is
going to be calling an undefined class or
| | 04:13 |
a function.
If you try and call a function that
| | 04:16 |
hasn't been defined, obviously it can't
do that for us.
| | 04:19 |
The second category of errors are going
to be Syntax Errors.
| | 04:22 |
In this case, PHP just plain all couldn't
understand what we were asking it to do.
| | 04:26 |
This is usually the result of a typo, a
missing semicolon, a quotation mark, a
| | 04:30 |
parenthesis, something like that,
something that makes our code not valid.
| | 04:34 |
It tried to parse it; it tried to figure
out where one thing started and where one
| | 04:37 |
thing stopped.
And it couldn't make any sense of it.
| | 04:40 |
And therefore, it gives us a syntax
error.
| | 04:42 |
Often these will be reported as a syntax
error, and it will say unexpected
| | 04:46 |
something in this file at a certain
point.
| | 04:49 |
It was expecting to get a closing
parenthesis, but instead it saw a call to
| | 04:53 |
define a function.
Right, that was unexpected, and so it
| | 04:56 |
goes ahead and says oops, that's a syntax
error, something's wrong here.
| | 04:59 |
After the error, it'll also tell you what
file the error was in and the line number
| | 05:03 |
where the error was encountered.
It's helpful if in your text editor you
| | 05:06 |
turn on line numbering, so that you see
the line numbers running down the left side.
| | 05:10 |
That'll help you identify the line very
quickly.
| | 05:13 |
It's also very common with syntax errors
for the line number to be off by one.
| | 05:16 |
Because if it encountered an unexpected
something, the problem is probably that
| | 05:21 |
something happened right before it that
caused it to be unexpected.
| | 05:25 |
Next category are Warnings, this
indicates that PHP found a problem but it
| | 05:29 |
was able to recover from it, it was
non-fatal.
| | 05:32 |
It understood what you were asking, it
was able to execute it but it ran into a
| | 05:35 |
bump along the way.
It indicates that you made a mistake and
| | 05:39 |
have a bug in your code.
It wasn't a fatal error but the page is
| | 05:42 |
probably not correct, probably not what
you intended.
| | 05:45 |
An example of a warning would be if you
tried to divide by zero.
| | 05:47 |
It would come up and give you a warning
that you can't divide by zero.
| | 05:50 |
It'll keep processing everything after
that, but whatever that mathematical
| | 05:53 |
operation was will have failed.
You'll also get warnings if you have the
| | 05:56 |
incorrect number of arguments sent to a
function or if you have the incorrect
| | 06:00 |
path to a file.
Or if you don't have access permissions
| | 06:03 |
to access a file.
In the last category are Notices.
| | 06:07 |
Notices are just PHP offering you advice.
Think of it as PHP telling you that
| | 06:11 |
something smells bad.
It's a good indicator of bugs and sloppy
| | 06:14 |
programming style.
And it can also include those deprecation
| | 06:17 |
notices that tell you about code that's
scheduled to stop working, in future
| | 06:21 |
versions of PHP.
Your code is fine, but you really ought
| | 06:24 |
to think about making these adjustments.
We were going to turn off one of these
| | 06:28 |
four categories in development, notices
is probably the one that would go first.
| | 06:33 |
Now as I said at the beginning, we want
to make sure that we do see these errors
| | 06:36 |
and warnings when we're in development.
It's tremendously helpful for identifying
| | 06:40 |
problems, however we do not want to have
errors displayed when we launch our
| | 06:44 |
website to the world.
First of all, they're ugly.
| | 06:46 |
And second it would be a major security
failure that would reveal far too much
| | 06:49 |
detail about our code.
So you want to make sure that you turn
| | 06:52 |
off error reporting when you launch your
website to the world.
| | 06:55 |
But just because those errors aren't on
the screen anymore, doesn't mean that
| | 06:58 |
they don't exist.
Errors are notices still generated by PHP
| | 07:01 |
whether they're displayed or not.
And it's important to know that that
| | 07:04 |
takes up some time, so if you have a lot
of those pesky little notices that have
| | 07:07 |
been generated.
Well if you deploy with that peach p is
| | 07:10 |
still generating those notices and it
does have an impact on performance for
| | 07:14 |
your website.
So, it is better to go ahead and address
| | 07:16 |
them and fix them even if they are just
notices about defecations, go ahead and
| | 07:20 |
fix them now to speed up your website.
The second point is that, chances are,
| | 07:24 |
those errors are still being logged by
your web server.
| | 07:27 |
And that's good because we definitely
want to know about those errors that our
| | 07:30 |
users are getting on the live site.
In fact, it's as important, if not more
| | 07:34 |
important, to know about problems that
are happening on the live site.
| | 07:37 |
Then it is to find out about errors that
are happening when we're developing.
| | 07:40 |
Now where your web server logs those
errors can vary and can be configured,
| | 07:45 |
but I want to give you just a few of the
places that you might look for it.
| | 07:48 |
Here on Mac OS X, chances are that be
default, it's going to be logging to
| | 07:51 |
private/var/log/apahce2/error_log, and
that's where you would find it.
| | 07:53 |
If you're on Windows in WAMP, chances are
it's going to log into the WAMP folder,
| | 08:00 |
into Logs into php_error.log Or you can
access those logs from the WAMP menu.
| | 08:06 |
If you're running Apache, well then it's
probably being configured inside Apache's
| | 08:10 |
Config file, and you could look there to
find out where that's being stored.
| | 08:13 |
If you look in there, you do a search for
Log, it should tell you where the Log
| | 08:17 |
file is being stored.
If your version of Apache's loading in
| | 08:20 |
other configuration files, let's say
different files for different domains
| | 08:23 |
that are hosted on the same sever.
Well, then those might be the place to
| | 08:26 |
configure where the error logging occurs.
You want to make sure that you check
| | 08:30 |
these error logs from time to time,
otherwise you won't know what problems
| | 08:34 |
your users are encountering.
| | 08:36 |
| | Collapse this transcript |
| Debugging and troubleshooting| 00:00 |
In the last movie, we looked at the
warnings and errors that PHP generates.
| | 00:03 |
But often, we need to debug our code,
even when we aren't getting a PHP error.
| | 00:07 |
We just aren't getting the results that
we expected from our code.
| | 00:10 |
Maybe a condition that uses less than
ought to be using less than or equal to.
| | 00:13 |
Or maybe we put code outside of a loop
that ought to be inside the loop.
| | 00:17 |
As with errors the more information we
have about a problem, the easier it'll be
| | 00:20 |
to solve it.
And the best way for us to get more
| | 00:22 |
information is to output that information
to the HTML page as our code is running.
| | 00:27 |
Now sure this extra output is going to
make the page ugly while we're debugging
| | 00:30 |
but that's okay because we're not
planning to keep it.
| | 00:32 |
It's just temporary.
There are a number of ways we can out put
| | 00:35 |
useful information and we've seen three
of them already.
| | 00:37 |
The first one is super simple, we just
echo the value of the variable.
| | 00:41 |
If you're wondering what the value of x
is each time it goes through a loop just
| | 00:44 |
right in the middle of that loop echo out
the value of x.
| | 00:47 |
We'll see it on the page and we can watch
the value change with each iteration of
| | 00:50 |
the loop.
Echo's also going to be super helpful
| | 00:52 |
when we start working with databases
because we're going to assemble SQL
| | 00:56 |
statements in a variable and then send
that statement off to the database.
| | 00:59 |
If we don't get back the results from the
database that we expected, we can echo
| | 01:03 |
that statement and see what was the SQL
statement that we had constructed.
| | 01:06 |
And we can examine it for errors.
The second one we've already seen, is
| | 01:10 |
print_r, remember that's for printing a
readable array.
| | 01:13 |
Works with arrays and makes them output
in a much more user friendly way.
| | 01:17 |
Remember, that this is going to look best
if we surround it with HTML pretags, that
| | 01:22 |
will allow it to preserve the white
space, and make the formatting even nicer.
| | 01:25 |
The third one we've seen before is
gettype, and that just returns the type
| | 01:29 |
of a variable.
Maybe we're working with a string when we
| | 01:31 |
ought to be working with an integer.
Well gettype can help us to identify that.
| | 01:35 |
There's also some new ones that we should
look at.
| | 01:37 |
The first of which is var_dump.
And that's going to take everything that
| | 01:40 |
it knows about a variable and just dump
it out for you.
| | 01:43 |
It's going to return the value its also
going to return the type.
| | 01:46 |
And if it's an array, it's going to give
you a nice friendly output of that array.
| | 01:50 |
Essentially doing what all three of the
functions up above it do.
| | 01:52 |
Its extremely useful and I think you're
going to find that you use it a lot.
| | 01:56 |
The next one you should know about is
get_defined_vars and that's going to
| | 01:59 |
return an array of all of the defined
variables that PHP knows about at that moment.
| | 02:05 |
Now that's not just the variables that
you've defined.
| | 02:07 |
It's going to include a few other special
variables that PHP uses.
| | 02:10 |
That's good.
It's going to show us everything that PHP
| | 02:13 |
knows about variables.
Another very helpful one is debug_backtrace.
| | 02:17 |
And that's going to show you a backtrace.
It's going to return it as an array.
| | 02:21 |
A back trace, also called a stack trace
is a report of the function calls that
| | 02:25 |
been made that lead up to a certain point
in time during the execution of a program.
| | 02:29 |
It's like a report of what happened
before you got to where you are now.
| | 02:33 |
Backtrace, as in retrace your steps
backwards.
| | 02:36 |
Let's try some of these.
So I'm just going to open up basic.html.
| | 02:41 |
We'll do Save As, and we'll call this
debugging.php.
| | 02:44 |
Change the title to Debugging, and I'll
create my PHP tags.
| | 02:49 |
And I'm just going to assign a couple of
variables here.
| | 02:52 |
I've got number equal to 99, string equal
to bug question mark, and array equal to
| | 02:58 |
an associative array, so I've got keys
and values there.
| | 03:00 |
So let's try this var_dump, we won't try
the first three because we've already
| | 03:03 |
used those before.
But let's start with a var_dump, let's
| | 03:06 |
just do var_dump and $ number.
Let's go try that out in a browser, Local
| | 03:11 |
Host and for me, that's going to be
@Kevinscoglin/sendbox/debugging.php.
| | 03:16 |
And there you go.
I got back int 99.
| | 03:21 |
It's telling me it's an integer and the
value is 99.
| | 03:23 |
I even got some nice color formatting on
that.
| | 03:26 |
Let's go try the next one now.
Just copy this and paste it.
| | 03:29 |
And we'll change it from number to
string.
| | 03:31 |
Let's take a look at what that does.
String, here's the value, and it tells me
| | 03:35 |
the length of it.
Nice little added bonus there.
| | 03:38 |
And let's try it one more, I'll try it
with array.
| | 03:41 |
Save it and there we go.
See how it gives me a nicely formatted array?
| | 03:45 |
I didn't even have to use pre tags around
it, I went ahead and just said here's the
| | 03:48 |
array, there are three elements within
the array, that's its size, here they
| | 03:51 |
are, one two and three are the keys,
here's the values.
| | 03:55 |
And it went ahead and further broke down
that value and said that value is a string.
| | 03:58 |
Here's the value of the string and here's
the length of the string just like it did
| | 04:02 |
up here, for this one.
So, you can see that's super useful, right.
| | 04:06 |
Let's take a look at another one.
Notice that the var_dump actually did the
| | 04:10 |
output, we didn't need to say echo or
anything like that.
| | 04:12 |
It dumped it to the screen, that's what
it does.
| | 04:14 |
Now we're going to take a look at get
define vars.
| | 04:18 |
So let's do, php and get defined vars.
And let's just do that and we'll reload
| | 04:24 |
and the page and you'll see we got
nothing back.
| | 04:27 |
Why is that?
Well it's because what it does is it
| | 04:29 |
returns an array, the VR tag in here, so
we need to echo that out, but it's an
| | 04:34 |
array, so there's no point in using echo.
We could use print r and that would work,
| | 04:39 |
and remember that print r looks best when
we surround it with pre tags.
| | 04:43 |
Let's come over here and try this out.
And here we go.
| | 04:46 |
It's a long list.
You see that there's lots of these that
| | 04:48 |
are things that we didn't set, but if you
scroll down here to the bottom, you'll
| | 04:51 |
see here's number, string, and array.
And here's those values for us.
| | 04:54 |
So that can be useful to find out a lot
about the variables that php knows about.
| | 04:58 |
because that's so long right now, I'm
just going to comment that out.
| | 05:01 |
And then we'll move on down to the next
one, because I want us to try out this backtrace.
| | 05:05 |
Now debug backtrace also does not output,
it just returns an array to us.
| | 05:10 |
So let's go ahead, php, but this time
instead of using print_r, I'm going to
| | 05:15 |
use var dump, debug, backtrace, all one
word.
| | 05:20 |
So let's just try that and see what that
gives us.
| | 05:22 |
And here we are.
Array size zero empty.
| | 05:25 |
That's what it gave us.
Well, if you think back to what I told
| | 05:28 |
you about debug backtrace, it's a report
of the function calls that have been made
| | 05:32 |
that lead up to a certain point in time.
Well, we haven't made any function calls
| | 05:36 |
here, so we need to do that.
We need to have a function call.
| | 05:38 |
I'm just going to bring up one of the
functions that we've worked with before.
| | 05:43 |
And that is the say hello to.
Say hello to work remember this just
| | 05:46 |
outputs everyone.
Lets just bring that out so we can see it
| | 05:49 |
first of all.
Hello everyone that's what it does.
| | 05:52 |
So lets we're in the middle of debugging
this and there's a problem we can just do
| | 05:55 |
a debug back trace here.
So lets do var_dump and then debug.
| | 06:01 |
Backtrace.
So we'll do it right after that line and
| | 06:04 |
then we'll hit Return, and you'll see
that it comes back and it tells us, here
| | 06:08 |
we are, the array, size 4, and it has the
file that we're working in.
| | 06:12 |
See it lists where the file is located,
it lists of the line number, so line
| | 06:17 |
number 33 is where we are, and the
function that we're inside of.
| | 06:21 |
We're inside of the Say Hello To
function.
| | 06:23 |
And then the arguments that have been
passed in it also tells us and that's an
| | 06:27 |
array and everyone was the string that
was passed in.
| | 06:30 |
So we get all sorts of useful information
from inside that function and if we have
| | 06:34 |
several functions, we have functions
inside of functions or files that are
| | 06:37 |
calling other files it will trace its
step backwards through all of those and
| | 06:41 |
show you all of them.
It can be very very useful.
| | 06:44 |
Now, php does not have an official
internal debugger.
| | 06:47 |
Some software programs do have a debugger
built in to you that can help you debug things.
| | 06:52 |
But there are a couple of third-party
tools that are worth knowing about.
| | 06:55 |
The first of these is xdebug.
And xdebug is probably the most popular
| | 07:00 |
debugger for php.
It offers things like showing you that
| | 07:04 |
stack trace whenever you have an error,
automatically.
| | 07:07 |
It logs all the calls to your functions.
It will make your var dump output look
| | 07:12 |
even prettier.
All sorts of things like that it'll do
| | 07:14 |
for you.
It's a really nice companion to have.
| | 07:17 |
Another alternative is DBG.
You can go to their website and look up
| | 07:21 |
more information about that.
And then the last one is Fire.PHP, and
| | 07:23 |
this uses Firebug for Firefox.
So if you're familiar with Firebug it's a
| | 07:29 |
useful popular add on to the Firefox web
browser.
| | 07:32 |
That gives you more information about
what's happening on a webpage.
| | 07:35 |
And FirePHP enables you to log debug
information to your firebug console from
| | 07:39 |
your PHP application.
So that can also be really useful.
| | 07:42 |
Now as I said at the beginning, this kind
of debug code is a temporary measure,
| | 07:46 |
it's meant to only be there while we
solve the problem.
| | 07:48 |
Once we're done solving the problem we
want to make sure that we remove that
| | 07:51 |
code, so that it doesn't accidentally end
up in our application permanantly.
| | 07:55 |
| | Collapse this transcript |
|
|
10. Building Web Pages with PHPLinks and URLs| 00:00 |
Now that we have our PHP fundamentals,
we're ready to start building webpages
| | 00:04 |
with PHP.
Think back for a moment to the diagram
| | 00:06 |
that we saw, with the request response
cycle, that shows requests starting at
| | 00:10 |
the browser, going to the web server, the
web server processes our PHP and returns
| | 00:15 |
an HTML page back to the browser.
That cycle, that request response cycle
| | 00:19 |
begins with a browser request.
Nothing happens on the web server unless
| | 00:23 |
the user sends in some information to us.
And there are only three ways that we can
| | 00:28 |
get data from users on the web.
There's URL's and links.
| | 00:33 |
There's forms, web forms that you fill
out with information and click submit,
| | 00:37 |
and then there's cookies.
Those are browser cookies that are stored
| | 00:39 |
on the browser.
And each one of these corresponds to an
| | 00:42 |
HTTP method.
The first is Get, the second is Post and
| | 00:47 |
the last is Cookie.
So, URLs and Links are Get requests, a
| | 00:52 |
form is a Post request.
And Cookie, well, it's not really a
| | 00:55 |
cookie request, but it's the way that we
access the cookie information that
| | 00:59 |
piggybacks on each request.
So, these are the three main things that
| | 01:03 |
we're going to be looking at.
Now, every web language has a different
| | 01:05 |
way to interact with these three types of
user data, and over the next three
| | 01:08 |
chapters, we'll look at how it's done in
PHP.
| | 01:11 |
We'll start by looking at URLs and links.
So to begin looking at links, let's first
| | 01:15 |
go to our basic.html page.
And let's do Save As on that.
| | 01:19 |
And we're going to save the first one as
firstpage.php.
| | 01:24 |
And I'll save that.
And now, let's go ahead and, before we do
| | 01:28 |
anything else let's do another Save As.
And we're going to change this one to secondpage.php.
| | 01:33 |
That will make it really obvious that we
have these two pages, and I'm also going
| | 01:37 |
to go and open them both up so that
they're both visible.
| | 01:40 |
So here's first page, and here's second
page.
| | 01:42 |
Now I can switch back and forth, and I
can start to make links between them.
| | 01:46 |
That's what I want to do here, that's how
we're going to see this.
| | 01:48 |
So let's go ahead and give this one a
title of First Page and save it.
| | 01:52 |
We'll this one a title of Second Page.
Now, we'll be able to see that in the
| | 01:56 |
browser and know which one is which.
So let's go to first page, and let's make
| | 02:00 |
a link on First Page.
A link in PHP is just an HTML link,
| | 02:05 |
that's all it is.
So open our tag and then a href equals,
| | 02:10 |
and then inside quotes we're going to put
where the page is going to go to, then we
| | 02:14 |
need some text for it, call it second
page, and then close the A tag.
| | 02:19 |
So where are we going to link it to?
Second_page.php.
| | 02:22 |
That's going to link it to the second
page.
| | 02:25 |
Now, that's not PHP, that's just basic
HTML.
| | 02:28 |
We don't even have PHP tags in that.
Let's go and bring that up in a browser.
| | 02:32 |
Localhost, for me it's going to be
kevinstovand/sandbox and then firstpage.php.
| | 02:40 |
There we are, there's my link.
Nice and simple.
| | 02:42 |
I click on it, and there I am, now I'm on
the second page.
| | 02:44 |
See how that worked?
I can click the back button, go back to
| | 02:46 |
the first page, and link back to the
other one.
| | 02:48 |
So that's it.
That's all there is to making links, in
| | 02:50 |
PHP is to make HTML links.
Now, the great thing about PHP though is
| | 02:54 |
that we can start to improve these links.
We can make them more dynamic.
| | 02:58 |
So, for example, instead of having second
page, we can cut that and let's go up here.
| | 03:03 |
And let's make a PHP tag, and let's call
this link name equals and then in quotes,
| | 03:09 |
second page.
Now, we could use our PHP to provide the text.
| | 03:15 |
PHP Echo, and then dollar sign, link
name.
| | 03:18 |
Now, make sure that you use Echo, very
important here.
| | 03:22 |
We're outputting code into the HTML, so
we need that echo.
| | 03:26 |
Super important, don't forget it.
This won't get the job done for you.
| | 03:29 |
So, now let's just bring it up and try it
again real quick.
| | 03:32 |
We'll go back to our first page and let's
just reload the page, and you see that we
| | 03:36 |
still see the second page.
So now we've got some dynamic text, this
| | 03:39 |
is something that could potentially be
pulled from a database, for example.
| | 03:42 |
Or maybe we have an if else statement
that decides if one condition is true,
| | 03:46 |
give it the length this name, if it's
something else, give it a different name.
| | 03:50 |
We can also have PHP that decides which
link to display.
| | 03:53 |
So it's either log in if you're not
logged in, or it's log out if you are
| | 03:57 |
already logged in.
One of the ways that we send a lot of
| | 04:00 |
dynamic data, is by sending something in
the query string.
| | 04:03 |
The query string is what comes after the
page name.
| | 04:05 |
So for example, id equals 1, that would
be a static link.
| | 04:10 |
Let's just save that.
Come back over here and reload the page.
| | 04:13 |
And now when I go to second page, you can
see it says PHP id equals 1.
| | 04:18 |
That's the kind of thing that PHP is
really good for.
| | 04:19 |
Let's imagine that we had a loop for a
second.
| | 04:22 |
We could put a loop around this tag, and
then maybe we were outputting a list of
| | 04:27 |
categories on a website or subjects.
We could go through and use a loop to
| | 04:30 |
iterate through each of the subjects.
So we can have a subject id equals 1,
| | 04:34 |
this link, subject id 2 equals this
length.
| | 04:36 |
Let's go ahead and just make PHP equals,
and I'll just make 2 for now, just so
| | 04:43 |
that's something a little different.
And then here, PHP, remember we need that
| | 04:47 |
echo, and then id.
There we go.
| | 04:50 |
Rmemeber to do those echoes.
Now we come back here to first page,
| | 04:54 |
reload the page, and the link is going to
go id two.
| | 04:57 |
You can see that the very bottom of the
page, the browser gives me a hint for
| | 05:00 |
that, where I can click on it and see now
id is equal to two.
| | 05:03 |
So try that.
If you want to try putting this inside
| | 05:05 |
the loop and looping from the numbers one
through ten.
| | 05:07 |
And then you can see that every time id
will increment, and you would then have
| | 05:11 |
links that go to different ids.
Now, once we send it to the other page
| | 05:15 |
though, the second page needs to be able
to pick up on that.
| | 05:18 |
The second page, it's very important that
it says, alright, I have an id 2 that
| | 05:23 |
requires some different processing than
if I had id number four.
| | 05:26 |
Otherwise it's not a dynamic page, so how
do we get that get value to work with.
| | 05:31 |
We'll look at that in the next movie.
| | 05:32 |
| | Collapse this transcript |
| Using GET values| 00:00 |
In this movie, we'll learn how to send a
value from one page to the next by using
| | 00:04 |
the URL query parameters.
The query parameters are the part of the
| | 00:07 |
URL that comes after the question mark.
This is typically were you send
| | 00:10 |
additional parameters that the page
needs.
| | 00:13 |
The format is always the name of the
parameter, an equals sign and then the
| | 00:17 |
value of the parameter.
Often these parameters modify the
| | 00:20 |
behavior of the code used to generate the
returned HTML.
| | 00:23 |
For example, somepage.php?page=2 would
process the same PHP code but the content
| | 00:30 |
return would probably be modified.
It would do something like return a
| | 00:33 |
second page of search results.
And you can send more than one query
| | 00:36 |
parameter in the URL by using an
ampersand between each one of these parameters.
| | 00:41 |
You can surf around the web and see lots
of examples on sites like Google and
| | 00:45 |
Amazon where you can see these query
parameters being used.
| | 00:48 |
It happens all over the web.
Now in PHP whenever a page request is
| | 00:51 |
made, PHP is automatically going to take
all of those query parameters that were
| | 00:56 |
sent with the URL and put them into an
associative array where we can access them.
| | 01:01 |
That array is what is called a Super
global variable.
| | 01:04 |
We've talked about global variables
before and we saw how we needed to call
| | 01:08 |
global on them to bring them into the
local scope.
| | 01:11 |
Well super global's are always available
in all scopes and PHP set's them for us
| | 01:16 |
before the page even starts processing.
There are about nine super globals altogether.
| | 01:20 |
And we'll be looking at several of them
later in this training.
| | 01:23 |
But right now we're only concerned with
the super global that relates to the
| | 01:26 |
variables passed to the page via those
URL parameters.
| | 01:30 |
And that one is called GET.
And that makes sense, because GET is the
| | 01:34 |
HTTP method that relates to URLs and
links.
| | 01:37 |
And the way that we access it is with
dollar sign, underscore and then all
| | 01:40 |
capitals GET.
That's the same of the super global where
| | 01:44 |
the query parameters are put by PHP.
Notice the undersocre that's in the begining.
| | 01:48 |
That's how all super globals are going to
be named.
| | 01:50 |
Incidentially this is why in the movie
about variables I suggested that you
| | 01:54 |
should not name your variables with
underscores at the start.
| | 01:56 |
It helps to make these super globals
really stand out.
| | 01:59 |
And because it's in all capitals don't
get it confused with being a constant.
| | 02:02 |
It is still a variable, it has that
dollar sign at the beginning.
| | 02:05 |
It's a super global variable.
Alright, so now that we know where these
| | 02:08 |
are stored, let's see how we access them.
In the last movie, we created first
| | 02:12 |
page.php and we also created second
page.php.
| | 02:16 |
And we created a link on the first page
and that link sends a get request to the
| | 02:20 |
second page.
And all of the query parameters are sent
| | 02:23 |
with that link.
In this case we're sending id equals 2.
| | 02:26 |
We're doing that right here, id equals
and then the value of id, which is 2.
| | 02:31 |
And all of those query parameters that
are sent with that link are going to be
| | 02:34 |
stored in the super global get.
So the get super global is going to be an
| | 02:38 |
associative array.
So let's use print r so that we can take
| | 02:42 |
a look at the contents of that array.
So let's just go to second page, here we are.
| | 02:46 |
And in second page I'm going to use my
pretags first because that's going to
| | 02:51 |
make it look nice with print r and then
we'll use php print r.
| | 02:57 |
And we'll use our super global GET and
let's just take a look at what that value
| | 03:01 |
looks like.
We're just inspecting it.
| | 03:03 |
Let's go back here.
Now typing a URL in the browser bar is
| | 03:07 |
100% the same as clicking on a link.
We don't have to go back to first page we
| | 03:11 |
can just re-load this page.
Or we can type the URL in directly.
| | 03:14 |
Both of those make GET request to our
server.
| | 03:17 |
So let's re-load it and there it is array
id equal to 2.
| | 03:21 |
And we know how to access values that are
in the array.
| | 03:24 |
Before we do that, though, I just want to
show you, let's take away this parameter
| | 03:28 |
so that there is nothing after it and
let's just hit Return to load that URL.
| | 03:32 |
And you see that comes back empty.
You can put anything you want up here in
| | 03:34 |
the string to try it, 34&name=kevin, and
whatever's up here just gets dropped into
| | 03:39 |
our associative array.
So if our link sends these parameters, if
| | 03:41 |
that's what our link looks like then
that's what will be sent to the second page.
| | 03:49 |
Okay, so we know how to access the values
that are stored in associative arrays.
| | 03:52 |
I'm just going to make my little window
here a little smaller so I can now see.
| | 03:55 |
Here we go, second page.
What we want to do now is instead of
| | 03:59 |
doing print r, let's just comment that
out.
| | 04:02 |
And instead, let's set id equal to $ Get
and then the key that goes with it, id.
| | 04:10 |
That's it.
So that will now take that value that was
| | 04:13 |
in that associative array and assign it
to the variable id.
| | 04:17 |
Now we can work with it.
Let's just echo it for now, echo id back,
| | 04:20 |
let's go back and let's reload this page,
I'm going to go all the way back to first
| | 04:23 |
page, there we are, so now I'm on first
page, let's reload that...
| | 04:27 |
Click second page and there it is.
It output the 2 for us.
| | 04:31 |
Now I've still got my pre tags around all
of this which is why it put the 2 way out
| | 04:35 |
here in the middle.
You could also just move this down here
| | 04:38 |
and that's outside the pre tag.
So let's do a new block of php.
| | 04:42 |
All right, so that's just a little
cleaner.
| | 04:44 |
We'll go back and reload the page and
there it is, outputting our value of 2.
| | 04:47 |
So let's go back to our first page.
And let's say that first page sent an id
| | 04:51 |
of 5.
Save that.
| | 04:54 |
We'll go back to first page, reload that
page.
| | 04:57 |
Must reload it so that we get that new
value in here.
| | 04:59 |
because otherwise the HTML that's here on
the page is the old HTML.
| | 05:03 |
So we reload the page.
Now I click it and it comes up with 5.
| | 05:06 |
So we've already made our website a lot
more dynamic by allowing us to pass
| | 05:10 |
values between pages in these URL query
parameters.
| | 05:14 |
This raises an important issue though.
Can a user send any value in that URL?
| | 05:19 |
For example, what if their value had an
ampersand in it?
| | 05:23 |
Would it still be read correctly or would
it interfere with the processing of that URL?
| | 05:27 |
Well there are some characters that we
have to watch out for.
| | 05:30 |
And in the next movie we're going to look
at how to encode those values to make
| | 05:33 |
sure that they work properly.
| | 05:34 |
| | Collapse this transcript |
| Encoding GET values| 00:00 |
Now that we know how to construct URLs to
be used as links, we need to talk about
| | 00:04 |
the characters that we're allowed to use
in those URLs.
| | 00:06 |
URLs can contain the most common
characters like letters, numbers,
| | 00:09 |
underscores, and dashes.
But there are some characters that are
| | 00:12 |
going to cause problems because they have
special meaning to the URL.
| | 00:16 |
These are known as reserved characters.
Here are a list of the reserved
| | 00:20 |
characters that we have problems with if
we include them in the URL.
| | 00:23 |
You can see there are quite a few.
When we're constructing URLs for lengths,
| | 00:26 |
we must encode these characters so they
don't interfere with the function of the URL.
| | 00:31 |
Encoding a reserved character means
converting that character to a percent
| | 00:35 |
sign followed by a pair of hexadecimal
digits.
| | 00:37 |
Hexadecimal just means that in addition
to zero to nine, we can use the letters A
| | 00:41 |
through F as if they were digits, too.
So, what we'll need to do is be able to
| | 00:45 |
convert each of these characters into its
hexadecimal form, put it in a URL.
| | 00:51 |
And then we'll decode it by converting it
back once the page is received.
| | 00:54 |
PHP lets us perform the URL encoding by
using a function called urlencode, and
| | 01:01 |
its argument is the string that you want
to encode.
| | 01:03 |
So, urlencode is going to take letters,
number, underscores and dashes and let
| | 01:08 |
them pass through completely unchanged.
But those reserve characters, that we
| | 01:12 |
just saw, are going to become their
hexadecimal equivalent.
| | 01:16 |
That percent sign, follow by two digit
hexadecimal digit number.
| | 01:19 |
And spaces are going to become plus
signs.
| | 01:22 |
Remember that, because that's going to be
important in a minute.
| | 01:24 |
We'll talk about that.
Let's try using it.
| | 01:26 |
So, let's go back to our first page.
In addition to sending ID 5, let's also
| | 01:31 |
add another one here.
Let's do an &company equals and we'll put
| | 01:36 |
a company name.
We'll put PHP echo $company, there we go.
| | 01:42 |
And then, up here, we're going to provide
that company name.
| | 01:45 |
$company equals and that company name
could be any string.
| | 01:49 |
So let's imagine that we're looking at a
directory of employees lets say.
| | 01:53 |
And we're going to look at a company
called Johnson & Johnson.
| | 01:57 |
Do you see the problem here?
It has an ampersand in it.
| | 02:00 |
So, let's just try it on its own.
Let's save it.
| | 02:03 |
Let's go back to first page so that we
can see what the problem is, I'm just
| | 02:06 |
going to go first page.php.
Alright.
| | 02:09 |
It generates the link just fine, but when
I click on it gives me a hint what it's
| | 02:12 |
going to do down at the bottom.
When I click on it you'll see up here
| | 02:17 |
that the URL is Johnson space and space
Johnson.
| | 02:21 |
Let's go over to our second page, and
let's echo that value.
| | 02:23 |
So, I'm just going to copy this, for now.
Paste it, and we'll just change it to
| | 02:28 |
company, and company, and company.
So, it's going to get that company value
| | 02:33 |
out of it.
So, reload the page, Look what I got, Johnson.
| | 02:36 |
That's the company, because it saw this
ampersand, and it said, oh, that's a new
| | 02:40 |
query parameter.
That's what's used to divide those.
| | 02:42 |
So, you see what the problem is now.
Now, the solution is to use urlencode.
| | 02:47 |
So, what we do is we provide urlencode
right here, urlencode, this string.
| | 02:53 |
Make sure that you use echo with
urlencode, super important.
| | 02:57 |
This will not do it for you.
This will encode it, but then it'll throw
| | 03:01 |
the value away.
Nothing will happen with it.
| | 03:02 |
You need that echo there.
So, make sure you've got that.
| | 03:05 |
Echo, the urlencoded value of company.
Let's save it.
| | 03:09 |
We'll hit the back button.
Go back to first page, I'll just reload
| | 03:12 |
that page.
Now when I click on it, now look what it
| | 03:15 |
gives me up here.
Johnson a plus sign for the space, and
| | 03:20 |
then the ampersand became % 26.
And then when I pulled the value back, it
| | 03:26 |
correctly found it.
It correctly changed the ampersand back,
| | 03:28 |
converted those spaces back as well.
Now, we didn't actually do any decoding.
| | 03:33 |
It's because PHP automatically does the
decoding for us when it assigns those
| | 03:39 |
values to the get superglobal.
It's kind of assumed the values are
| | 03:43 |
encoded and ready to be used.
In which case they need to be decoded so
| | 03:47 |
that we can use them.
So, it just does it automatically for us.
| | 03:49 |
We don't actually have to decode.
There is a function called urldecode that
| | 03:54 |
works exactly like you would expect that
you can use if you ever need it.
| | 03:57 |
Must most times you don't need to.
Now I just want to insert a little
| | 04:00 |
parenthetical here that this is just for
get requests only.
| | 04:04 |
Later when we start working with posting
cookie, we won't need to do this encoding.
| | 04:08 |
This is just forget request because those
values are in the URL string at the top.
| | 04:13 |
So we're URL encoding them.
So, now that we know about URL encoding,
| | 04:16 |
there's another type of encoding that we
need to talk about called raw URL
| | 04:20 |
encoding and it's important to understand
and to know the differences.
| | 04:24 |
So, if we take a look at what we had for
urlencode, we saw that letters, numbers,
| | 04:27 |
underscores, and dashes are unchanged.
It converts the reserved characters and
| | 04:31 |
spaces become plus.
With rawurlencode, just put the word raw
| | 04:33 |
in front of it, no spaces or underscores,
just all smashed together, letters,
| | 04:39 |
numbers, underscore and dash are
unchanged.
| | 04:41 |
The reserved characters get converted
but, here's the big difference.
| | 04:44 |
Spaces become percent 20.
They get encoded into a two-digit
| | 04:50 |
hexadecimal also instead of using the
plus.
| | 04:53 |
It may seem like a minor point, but it
does make some big differences.
| | 04:56 |
It's easy for us to try it.
We'll just pop back over here, and we'll
| | 04:59 |
just type raw in front of urlencode.
Now we'll come back here.
| | 05:02 |
We'll go back to first page, we'll reload
the page.
| | 05:05 |
And you can see now, when I click on it,
now it is Johnson space Johnson with the
| | 05:11 |
percent 26 encoded.
So, now you may be thinking well wait a
| | 05:15 |
minute, I thought you told me that it was
going to convert this space into percent 20.
| | 05:20 |
It actually did.
Let's go back real quick to the first
| | 05:22 |
page and let's view.
Page source, here we go.
| | 05:26 |
See, here they are.
Percent 20, percent 26, percent 20.
| | 05:29 |
That's what the link was on this page.
Then when I clicked it, and it shows up
| | 05:33 |
in the URL.
Firefox is helpfully converting those
| | 05:36 |
back to spaces for me.
But not all browsers do that.
| | 05:39 |
It is percent 20.
That's what you're actually sending.
| | 05:42 |
So, now that we understand the
difference.
| | 05:44 |
One send pluses, one sends percent 20.
You're probably wondering, when should
| | 05:48 |
you use each one?
Well, here's my guideline for you.
| | 05:51 |
You're going to want to use rawurlencode
on the path, that's the portion that
| | 05:55 |
comes before the question mark.
So, everything comes before that, if
| | 05:59 |
there's anything dynamic in there that
you're generating, like the page name.
| | 06:03 |
That is all going to be done with raw URL
encoding, because the spaces must be
| | 06:07 |
encoded as %20 for the final system to be
able to find that file.
| | 06:11 |
For Apache to be able to locate the PHP
page you're looking for, it needs it as %20.
| | 06:16 |
But, you want to use urlencode on the
query string.
| | 06:20 |
That's everything that comes after
question mark.
| | 06:23 |
That's because spaces are better encoded
as plus.
| | 06:27 |
Now it worked fine as you saw when we
used percent 20 and used rawurlencode on
| | 06:31 |
it, but the plus sign is going to be a
better more compatible way to do it.
| | 06:35 |
However, rawurlencode is more compatible
generally.
| | 06:38 |
So, if you ever have a doubt, if you're
working with JavaScript let's say, you're
| | 06:43 |
doing an Ajax request, something like
that.
| | 06:46 |
You're working with communicating with
another server via an API, rawurlencode
| | 06:51 |
is probably what you want.
Urlencode is an early standard for how
| | 06:56 |
webservers should work.
Rawurlencode is based on a subsequent
| | 07:00 |
standard that came later.
So, it's the newer and later version and
| | 07:04 |
most times it's going to be better.
However, when we are working with URLs
| | 07:08 |
rawurlencode the path, urlenconde the
query string.
| | 07:12 |
Let's make ourselves a quick page that
can demonstrate this.
| | 07:14 |
I'm going to come back over here, and I'm
just going to take first page, and I'll
| | 07:19 |
do a quick Save As on it.
I'm going to call it urlencode.php, and
| | 07:22 |
let's call it urlencode.
We'll make it this way.
| | 07:27 |
And here in the body I'm just going to
paste in an example.
| | 07:30 |
And I've got a page that's going to be
used as a bio, so I'm looking for bio for
| | 07:35 |
William Shakespeare with a space in it.
I'm going to rawurlencode that and then
| | 07:39 |
I'm going to ask for a quote that's
going to be the parameter I'm going to
| | 07:42 |
send of to be or not to be.
And I'm going to urlencode that.
| | 07:45 |
Let's just bring that up and take a look
at it real quick.
| | 07:48 |
So, make sure that you save it.
And then, let's do urlencode.
| | 07:51 |
And there you go.
See the percent 20 there, versus to be or
| | 07:54 |
not to be.
This, the file system will be able to
| | 07:57 |
find for you.
If we, instead, flip those around.
| | 08:01 |
Make link two.
And let's just swap these.
| | 08:04 |
Let's make this one raw.
And this one is not raw.
| | 08:07 |
And then we'll just echo back like two
for contrast.
| | 08:12 |
Now the file system is going t be looking
for something called William plus Shakespeare.
| | 08:17 |
A literal plus sign not a space.
So, that's not going to find the file to
| | 08:21 |
begin with.
And then this to be or not to be.
| | 08:24 |
Probably will work out for us okay.
Usually, most web servers and PHP can
| | 08:28 |
sort that out.
But this is the better way to do it.
| | 08:30 |
This is going to look better, it's going
to work better all around.
| | 08:33 |
Rawurlencode, everything that's on this
side of the question mark, urlencode
| | 08:38 |
everything that comes after it.
| | 08:40 |
| | Collapse this transcript |
| Encoding for HTML| 00:00 |
In the last movie, we saw how to use PHP
to encode values for use in the url string.
| | 00:04 |
And that's because there's certain
characters that have special meaning when
| | 00:07 |
they're used in the url.
There's another place where we have to
| | 00:10 |
watch out for reserved characters, to
make sure we don't end up with unintended consequences.
| | 00:14 |
And that's in the HTML.
Here's a sample block of HTML.
| | 00:18 |
Now, there are reserved characters in
HTML that have special meaning.
| | 00:21 |
Most notably, the less than and greater
than signs that surround the HTML tags.
| | 00:26 |
These characters indicate to HTML that
everything inside here is an instruction
| | 00:30 |
for UHTML.
This is something that you should follow,
| | 00:34 |
and use for your formatting.
But don't output it to the end user.
| | 00:37 |
They won't actually see The results of
those tags.
| | 00:40 |
It's just for the HTML's benefit.
We want to take care that we don't output
| | 00:44 |
strings with characters that have special
meaning to HTML, or we'll break the HTML
| | 00:49 |
or break our text.
Imagine, for example, that in our
| | 00:52 |
paragraph of text there, we had a literal
less than sign that we wanted to downput
| | 00:57 |
to the screen.
As HTML is reading it, it's going to come
| | 01:00 |
across that less than sign and think that
that's the beginning of a tag.
| | 01:03 |
And it will interpret everything after
that as being part of a tag until it gets
| | 01:07 |
to a closing greater than sign.
So you can see how it would break it.
| | 01:10 |
There are 4 characters that are reserved
characters in HTML.
| | 01:14 |
They're the less than sign, the greater
than sign, the ampersand, and the double quote.
| | 01:19 |
Mostly it's going to be the less than and
greater than sign that we're most
| | 01:22 |
concerned with.
But we're going to go ahead and take care
| | 01:24 |
of all four of these.
So that they don't cause problems for us.
| | 01:27 |
Now the way that we'll do that is we're
going to encode them.
| | 01:30 |
It's the same strategy that we used with
the URL string.
| | 01:33 |
But it's completely different.
Different set of characters, different encoding.
| | 01:37 |
But the idea is the same.
We're rendering them harmless.
| | 01:40 |
When we encode them for HTML we're going
to do the encoding differently.
| | 01:42 |
We're going to encode them into their
HTML character entities.
| | 01:46 |
You may remember from HTML there's these
character entities.
| | 01:48 |
They're defined by an amperstand at the
beginning, an semicolon at the end and
| | 01:51 |
some abbreviation in the middle to
indicate what character it is.
| | 01:55 |
HTML sees these and displays them
correctly to the user, but it didn't know
| | 01:59 |
the difference between the encoded entity
and the regular symbol.
| | 02:04 |
So, for example, less than sign becomes
<.
| | 02:08 |
HTML sees < and outputs to the user A
less than sign, but it does not then see
| | 02:16 |
that less than sign as being part of the
tap.
| | 02:19 |
We can encode HTML using two functions of
PHP.
| | 02:22 |
The first one we'll look at is HTML
special chars short for characters.
| | 02:26 |
And the other one is HTML entities.
So lets try them both.
| | 02:29 |
So to start with lets open up basic
.HTML.
| | 02:32 |
Let's use Save As and we'll save this as
HTML encoding.PHP.
| | 02:35 |
There we go, HTML encoding.
Now, let's first illustrate the problem.
| | 02:42 |
Let's say that we have a link.
A href equals and inside that link we're
| | 02:47 |
going have text and that text is going to
say Click inside less than, greater than
| | 02:53 |
signs and learn more.
That's the text that we would like to
| | 02:59 |
output to the user.
We would like the user to see that
| | 03:01 |
literal text but HTML is going to come
along and it's going to read this and
| | 03:05 |
going to say wait a minutes.
This is a less than sign.
| | 03:08 |
This must be the begining of a tag.
When it gets to the greater than sign,
| | 03:11 |
it's going to say Okay that must be the
end of the tag.
| | 03:13 |
So, therefore this must be a tag.
This is an instruction for me.
| | 03:18 |
Not part of the text.
Let's see what that looks like.
| | 03:20 |
Let's save it.
Come back over here.
| | 03:22 |
HTML encoding.PHP.
And notice that click, doesn't show up,
| | 03:27 |
because tags don't show up.
We don't see a body tag, we don't see an
| | 03:31 |
"A" tag, those are just instructions for
HTML.
| | 03:33 |
So, it thinks that's what this is and it
doesn't output it to the user.
| | 03:37 |
Even worse, let's take away the second
one, let's save it again.
| | 03:40 |
Now HTML is going to read it and think
that all of this is one tag.
| | 03:44 |
All the way, until it gets to the greater
than sign again.
| | 03:47 |
Reload the page, and you'll see that,
sure enough, nothing now outputs.
| | 03:50 |
So this can cause a lot of problems for
us.
| | 03:52 |
So what we need to do is encode it.
We're going to use our PHP to do that.
| | 03:56 |
So let's start with PHP and we'll have
linked text equals, and then I'm just
| | 04:01 |
going to take all of this and cut it and
drop it into a string here.
| | 04:05 |
And then we'll output it using PHP
Linktext.
| | 04:09 |
Now I haven't done anything different
here except move the code into PHP.
| | 04:12 |
That's it.
Theres no encoding happening, we could
| | 04:15 |
reload the page and we'd see the exact
same thing.
| | 04:17 |
Now though, let's try encoding and we're
going to use that HTML special chars.
| | 04:21 |
What that's going to do is encode those
four special characters for us.
| | 04:27 |
We've got three of those here.
We've got the less than, we've got the
| | 04:30 |
greater than, and we've got the
ampersand.
| | 04:32 |
Now, notice this does the encoding.
It's not going to actually output them
| | 04:36 |
for us.
We still need to have the echo, very important.
| | 04:39 |
So let's save it, let's go back and look
at it.
| | 04:41 |
And sure enough, that's exactly what we
expected.
| | 04:43 |
That's the length that we wanted users to
see on the screen.
| | 04:45 |
We wanted them to see the less than sign.
If we go to Tools > Web Developer > Page
| | 04:50 |
Source, here we are, you can see that it
converted them to their character entities.
| | 04:55 |
See how that works?
That rendered them harmless.
| | 04:57 |
HTML was able to see that that was
different than an instruction that it
| | 05:01 |
should process.
Now there's also another function called
| | 05:05 |
HTML special characters decode.
If you ever needed to decode it for any reason.
| | 05:08 |
But we typically don't, because if we're
just outputting it to HTML, it's built
| | 05:13 |
into the HTML to do that decoding for us.
To decode character entities into
| | 05:18 |
something that we can send back to the
user, that's built in.
| | 05:21 |
So that's really all there is to HTML
special chars.
| | 05:24 |
Lets talk about HTML entities.
It works just like HTML special chars
| | 05:27 |
does, but the difference is that all
characters that have an equivalent HTML
| | 05:32 |
entity are translated into those
entities.
| | 05:34 |
HTML special chars just does those four.
But there's a lot of HTML character
| | 05:38 |
entities, bullets in dashes, trademark
symbols, copyright symbols, foreign
| | 05:43 |
currency symbols, accident characters.
All of those have HTML entities.
| | 05:48 |
Lets try an example with those real
quick.
| | 05:50 |
I'm going to come back over here.
I'll make a br tag.
| | 05:53 |
And lets do a new block of PHP.
And in it, I'm just going to put text
| | 05:58 |
equals and I'm going to put some special
characters in here.
| | 06:01 |
I got the trademark symbol, the pound,
the bullet, an opening curly double
| | 06:05 |
quote, and in dash and accinity.
Those are just a few, you can try typing
| | 06:09 |
others yourself.
So to start with, let's just echo back text.
| | 06:13 |
To just see what that looks like.
So I'll go back over here, and we'll
| | 06:16 |
reload the page.
Look at that, It gave me some
| | 06:18 |
gobbledygook, right?
Not at all what I intended.
| | 06:21 |
Those are not the characters that I had
in mind.
| | 06:23 |
These are called high ASCII characters
and they're handled a little differently.
| | 06:26 |
But, if we use our HTML entinties on it,
It will now grab all of those and encode
| | 06:33 |
them into their HTML entity equivalent.
Let's come back over here.
| | 06:37 |
Reload the page.
And there you go.
| | 06:39 |
Now we see we have the correct ones.
And sure enough if you go to View Page
| | 06:42 |
Source, you'll see that it's converted
them all into their equivalent entities.
| | 06:46 |
So, HTML entites does more than HTML
special characters does.
| | 06:50 |
It does not just those four, it does all
of them.
| | 06:53 |
HTML special charts are the ones that you
really have to do so that you don't break
| | 06:57 |
your page.
HTML entities are the one that you're
| | 07:00 |
going to want to do just to have nice
looking text.
| | 07:02 |
So that's the kind of thing if you have a
paragraph, and it might include some of
| | 07:05 |
these characters, then HTML entities
would be an appropriate thing to use there.
| | 07:09 |
But not doing it won't break your HTML,
it just might make your text look a
| | 07:12 |
little funny.
Alright, so now that we know about URL
| | 07:15 |
encoding, and we know about HTML
encoding.
| | 07:17 |
I want us to take a look at how this all
fits together, because it's important to
| | 07:21 |
understand how the two work in
conjunction.
| | 07:24 |
I'm going to give you just a big block
here, laying out the differences, what to
| | 07:28 |
use when.
So I'm going to have a URL page.
| | 07:30 |
A simple URL, then I'm going to have a
parameter, and that parameter is going to
| | 07:36 |
have some characters in it that HTML
doesn't like.
| | 07:38 |
Then I'm going to have parameter 2, which
has some characters that the URL string
| | 07:42 |
doesn't like.
And then link text is going to be the
| | 07:45 |
same thing we just had.
Click and learn more.
| | 07:47 |
So let's construct a URL from that.
We're going to take the base of the URL.
| | 07:50 |
We're going to append to that, raw URL
encoded, the URL page.
| | 07:56 |
Everything that goes to the left of the
question mark uses raw URL encode.
| | 08:01 |
Everything that goes to the right of the
question mark is going to get just URL encoded.
| | 08:05 |
So we do that to both of our parameters.
At this point now our variable, URL, is
| | 08:09 |
safe to put into the query string.
It's okay to use as a URL.
| | 08:14 |
We can use it for our link.
And it won't break the URL string.
| | 08:17 |
But it's not necessarily safe for the
HTML.
| | 08:21 |
And we're about to output it into the
HTML.
| | 08:24 |
Well, if we just echoed it into our href
attribute then it could cause a problem,
| | 08:28 |
and break our HTML.
Depending on what characters are there.
| | 08:30 |
So we also need to use HTML special chars
here to make sure that it is not going to
| | 08:37 |
break the html when the link is first
displayed, before it's clicked on.
| | 08:41 |
Up here we've taken care of after it's
clicked on.
| | 08:43 |
This takes care of it before it's clicked
on.
| | 08:45 |
And the the text that we're going to use,
we also want to use html special chars on.
| | 08:49 |
And of course, we want to echo it both
times.
| | 08:52 |
So let's save it.
Let's come back over here Let's reload
| | 08:55 |
the page.
let me just put a <br /> tag up here to
| | 08:58 |
make it look a little nicer.
There we go.
| | 09:00 |
Click & learn more, and that link won't
go anywhere because that page doesn't
| | 09:04 |
exist, but you can see it at the bottom.
You can see what it's going to load up.
| | 09:07 |
And you can see that it's perfectly safe,
that this is going to be perfectly fine.
| | 09:10 |
It has encoded all of them that it needs
for the URL string.
| | 09:14 |
So it's important to keep the differences
straight and to understand why you're
| | 09:17 |
doing each one.
You need to make sure that the URL is
| | 09:21 |
going to be safe.
And then, if you're going to put it into
| | 09:24 |
your HTML, you want to make sure that
it's safe to put it in your HTML as well.
| | 09:26 |
| | Collapse this transcript |
| Including and requiring files| 00:00 |
One of the most useful features in PHP is
the ability to include code from other
| | 00:04 |
files into a PHP page.
It may not be obvious why that's so
| | 00:08 |
great, but it's an important feature
because it's going to help us to stay
| | 00:10 |
organized and to not repeat ourselves.
For example, if you define a function for
| | 00:15 |
one page of your site and then you need
that same function again on another page.
| | 00:19 |
It's much better to be able to access the
same function from both places, exact
| | 00:24 |
same code.
Instead of copying and pasting the code
| | 00:27 |
into the second page so you can use it
again.
| | 00:29 |
A copy is going to mean that if we find a
bug or make an improvement to the function.
| | 00:33 |
Then we have to remember to update the
code in more than one place, and that
| | 00:36 |
leads to bugs and code that's hard to
maintain.
| | 00:38 |
And remember that's the whole reason that
we created functions in the first place.
| | 00:41 |
So that we wouldn't have to repeat that
code every time we needed to use it again.
| | 00:45 |
Instead what works better is to have a
file that contains a function and then
| | 00:49 |
we'll include that file in both PHP pages
that need it.
| | 00:53 |
Even better, we can have a file dedicated
to functions of a certain type and put
| | 00:57 |
all our functions in one, easy to locate,
easy to include, place.
| | 01:01 |
And we can do that, by using PHP's
Include function.
| | 01:04 |
Let's see how that works.
To start with, I'm just going to open up
| | 01:07 |
basic .HTML and I'll do a file save as,
on it.
| | 01:11 |
And we'll call this includes.php.
Includes, so lets also make another file
| | 01:19 |
that we want to include the code in this
file.
| | 01:22 |
So, lets do that, we'll do Save As on
this, and this one, lets call included header.
| | 01:30 |
And I'm intentionally giving it a name
that's very similar so that the two will
| | 01:33 |
show up close to each other in the list
here.
| | 01:35 |
So here's Included Header and here's
Includes.
| | 01:37 |
So, for this included header, let's just
have everything that's the header,
| | 01:42 |
everything up to the body tag.
That's it.
| | 01:45 |
Just this HTML.
Let's save that and now lets go back and
| | 01:48 |
open up our Includes file.
And let's take all of that out.
| | 01:52 |
We're going to remove all of that and
instead we're going to open up PHP tags,
| | 01:56 |
and we're going to use include, and then
what do we want to include?
| | 02:00 |
In quotes the name of the file,
included_Header.php.
| | 02:05 |
Now it's a PHP file even though there's
not any PHP in it, that's fine.
| | 02:09 |
It's not our problem.
It could also be called .html, we can
| | 02:11 |
also include HTML files.
But generally if I'm working on a PHP
| | 02:14 |
site, I'm going to go ahead and call all
of my files that I'm working with .php.
| | 02:19 |
Just so that there is not a problem if I
ever decide to drop in PHP, it's already
| | 02:23 |
ready for me.
It already has PHP in the name, alright
| | 02:25 |
so let's just save it and then let's put
something here about says the header has
| | 02:31 |
been included.
Okay.
| | 02:32 |
So, let's just save that, let's go to our
browser and let's open up includes.php.
| | 02:40 |
There it is.
The header has been included.
| | 02:42 |
Let's view the source.
That's going to be the real test.
| | 02:43 |
You can see that the title is up here,
Includes, that's good.
| | 02:46 |
Let's look at View Source.
And you can see, sure enough, it included
| | 02:50 |
all of that header right up above it.
The header's been included, and there's
| | 02:54 |
the body and everything that goes above
it.
| | 02:55 |
Nice and simple, right?
Include, just bring that in.
| | 02:58 |
That included just some basic HTML and
you can do the same thing for the footer.
| | 03:01 |
I won't do that now but you can do that
as an exercise on your own.
| | 03:04 |
It's just to move that to an included
footer.
| | 03:06 |
Let's try another one now, let's just do
a Save As.
| | 03:09 |
It doesn't really have to Save As but
I'll just do Included_Functions.php.
| | 03:14 |
Let's take all of this out, and let's put
in a Function here.
| | 03:19 |
How about, function hello.
Followed by a name.
| | 03:23 |
And that will simply return, hello, and
then the name that we gave it.
| | 03:30 |
Exclamation point and semicolon.
The hello function is now defined in our
| | 03:34 |
included functions file.
We'll switch back.
| | 03:37 |
I don't need the header.
What I need is, includes.
| | 03:39 |
There it is.
And now before I even get to the header,
| | 03:43 |
let's do another include, which is going
to be included_functions.php.
| | 03:48 |
And I need semi-colons after both of
those, there we go.
| | 03:53 |
So now we have the ability to include
both of those, we should have access to
| | 03:56 |
this function.
We should be able to call, hello.
| | 03:59 |
So let's try that.
Let's use some PHP code here.
| | 04:01 |
Now it just returned a value, it didn't
echo it so we're going to need to do an echo.
| | 04:05 |
Hello, and let's just do everyone.
Now we'll put a br tag at the end, there
| | 04:09 |
we go.
Come back over here, reload the page.
| | 04:12 |
There's hello everyone.
Put a br tag here too just to make it
| | 04:15 |
nice and clean.
So, there we go.
| | 04:17 |
It's that easy.
We're including the function, it's as if
| | 04:20 |
it was written on this same page.
It goes ahead and just processes it.
| | 04:23 |
Now, there's one gotcha that I want to
make sure, super clear to you.
| | 04:27 |
Here, we're inside PHP tags, we're in PHP
mode, we do the include.
| | 04:32 |
Let's look over at the other file.
It has PHP tags also.
| | 04:36 |
It's very important that you turn PHP
back on in every file that's going to
| | 04:41 |
have PHP.
'Cuz what happens is, during this
| | 04:43 |
include, it turns off PHP processing.
Even though we're still inside these PHP
| | 04:48 |
tags, when it goes to get what's in the
other file.
| | 04:50 |
It turns off PHP processing so that this
first thing in this file could be,
| | 04:55 |
potentially, some HTML like it was for a
header.
| | 04:58 |
And we need to turn it back on explicitly
in this file.
| | 05:03 |
So just make sure that, that doesn't get
you.
| | 05:04 |
Make sure you don't think that you're
still in PHP mode here.
| | 05:07 |
You're not.
As soon as you go to the other file, if
| | 05:09 |
you want to write PHP code, the same
rules apply.
| | 05:12 |
You're going to have to turn it back on.
So we've seen two good examples of what
| | 05:16 |
to include.
Let's talk about a few others.
| | 05:18 |
I would say that the good things to
include would be functions, because then
| | 05:21 |
we can define them once and only once,
and they can be broken up by type.
| | 05:25 |
We could have our form functions, and our
database functions, our general
| | 05:29 |
functions, right?
We could have several different files of functions.
| | 05:32 |
We could have layout sections of the
page, the header, the footer, maybe
| | 05:35 |
there's a side bar that has navigation
elements on.
| | 05:38 |
That's common to all pages, that could
also be included.
| | 05:41 |
Reusable bits of html PHP code, this
could be banner ads, page analytics.
| | 05:46 |
It could be little snippets of HTML/PHP,
like maybe you're going to display a
| | 05:50 |
bunch of images.
All those images are going to have titles
| | 05:54 |
underneath them that are all formatted a
certain way.
| | 05:56 |
Well you could to use that throughout
your site, over and over and over again.
| | 05:58 |
You can use Include to bring that in.
And so it's useful to use Include when
| | 06:03 |
you want to bring in some CSS and
JavaScript.
| | 06:05 |
.
Especially something that wasn't defined
| | 06:07 |
in the header of the page.
Now, in addition to include, there are
| | 06:11 |
three variations on it that are important
for us to look at.
| | 06:13 |
The first is require.
Require does exactly the same thing as
| | 06:18 |
include, but it raises a fatal page error
if the file can't be found.
| | 06:23 |
It says, look it really is required.
This page will not be able to go forward
| | 06:27 |
if you can't get this file, so require
it.
| | 06:29 |
Include doesn't do that.
Include will try to load the file, but if
| | 06:32 |
it can't find it, well it'll just keep on
going anyway.
| | 06:34 |
Now it might cause an error later on the
page if we try and call a function or
| | 06:38 |
something that isn't defined.
But include itself won't throw the error,
| | 06:42 |
require will.
It will go ahead and say oops, we have a
| | 06:44 |
problem here.
We can't find the file, now generally the
| | 06:46 |
files that you want to include are going
to be present.
| | 06:49 |
So, either one of these will due but,
it's worth thinking about whether a file
| | 06:52 |
is really required or optional.
For example a file that contains function
| | 06:55 |
or database connectivity is probably a
requirement while a file that outputs a
| | 06:59 |
standard html footer is probably not.
Another variation is include_once.
| | 07:04 |
Include_once keeps an array of paths to
the files that it has already included.
| | 07:09 |
So as it includes a file, it just adds
that file path to an array.
| | 07:13 |
And then if we asked to include it again,
it will ignore it, because it sees that
| | 07:17 |
it has already included it before.
This is great to use with functions,
| | 07:20 |
because we can't define functions more
than once without getting an error.
| | 07:23 |
And then of course there's the
combination of those which is require_once.
| | 07:26 |
The same idea, but we're requiring it,
instead of just including it.
| | 07:30 |
So, by using these four functions we're
going to be able to keep our code well organized.
| | 07:34 |
And be able to bring in common code that
we need, whenever we need to use it on a
| | 07:38 |
new page.
| | 07:39 |
| | Collapse this transcript |
| Modifying headers| 00:00 |
In the movie following this one, I want
us to talk about page redirection.
| | 00:03 |
But in order to do that, we first need to
talk about headers because headers are
| | 00:07 |
the mechanisms that we'll use for page
redirection.
| | 00:10 |
We're going to get into some technical
details here.
| | 00:12 |
But bare with me because this is
important to understand and there is a
| | 00:15 |
payoff at the end when we learn how to do
redirection.
| | 00:17 |
To understand headers, let's go back to
the diagram of the request response cycle
| | 00:21 |
that we looked at early on.
When our web server responds to a browser
| | 00:25 |
request, we know that it sends back an
HTML page.
| | 00:28 |
It also sends data just before the page
that provides basic information to the
| | 00:32 |
browser about what to expect from the
data that's going to follow.
| | 00:35 |
This data is called the HTTP header.
And not only does it go from our web
| | 00:39 |
server to the browser with a response the
browser actually send and http header
| | 00:43 |
with its request to the web server to
begin with.
| | 00:46 |
Its because the header is part of the
http protocol that all communication on
| | 00:51 |
the web uses.
Here's a common example of an http header
| | 00:55 |
In the first line it tells us what
version of http protocol it's using.
| | 00:59 |
And then it gives us a status code.
This is very important.
| | 01:01 |
This number indicates whether or not this
was a successful response or not.
| | 01:06 |
200 means it was.
That the page did successfully load.
| | 01:09 |
Other common status codes you would see
here, would be 404 for page not found.
| | 01:14 |
You're probably familiar with that.
And then 500 would for errors, things
| | 01:18 |
that have gone wrong.
There at list a dozen other ones.
| | 01:20 |
But those are the three main ones that
you would get there.
| | 01:23 |
And then you can see after that we have
pairs of data.
| | 01:26 |
We have a label or an attribute followed
by a colon, a space and then a value for it.
| | 01:32 |
So we have the date and we have the
server, the content type, content length
| | 01:36 |
and then the fact letting us know the
connection is no longer open.
| | 01:40 |
Now usually you don't have to think about
or even see this header information.
| | 01:43 |
It's automatically constructed for you by
your web server when it responds to the request.
| | 01:48 |
But we can use php to give instructions
to the web server to tell it how we want
| | 01:53 |
it to construct the header.
We do that by using the header function.
| | 01:57 |
So header followed by a string.
So for example, header, Content Type:
| | 02:01 |
Application PDF.
Here we would be telling it, hey when you
| | 02:04 |
send this back, this is not HTML that
you're sending, it's a PDF.
| | 02:08 |
You should treat it like a PDF when it
arrives, and in fact if your browser has
| | 02:12 |
configuration options, it let's you
decide how do you want to handle content
| | 02:15 |
that comes back as application PDF.
You can also tell it that it's going to
| | 02:19 |
be an attachment and you can suggest a
file name for what that attachment ought
| | 02:23 |
to be or we could send status codes like
we talked about the 404.
| | 02:26 |
And the 500 status codes that we could
send back.
| | 02:29 |
Notice that those don't use that colon
format, they just have HTTP and then a
| | 02:34 |
space followed by the status.
Now here's the super duper important
| | 02:38 |
thing that you have to remember about
headers.
| | 02:41 |
They come before all other page data.
Remember they tell the browser here's
| | 02:46 |
what kind of data is about to come to
you.
| | 02:48 |
And are required by the HTP standard to
proceed any communication back and forth.
| | 02:53 |
That means that any changes that we
want to make to the header have to be
| | 02:56 |
done before we output anything from our
file.
| | 02:59 |
If we send even a single character to the
user's browser, then the header is
| | 03:02 |
already on its way out the door right
ahead of it.
| | 03:05 |
And at that point, it's too late to make
changes to the header information, and I
| | 03:08 |
mean anything, even a blank space or a
line return.
| | 03:10 |
Those count.
And you also have to watch out for spaces
| | 03:13 |
and line returns in included files; those
can cause you problems.
| | 03:17 |
It can come inside of a php block that
includes white space.
| | 03:20 |
That doesn't matter.
As long as the php doesn't output
| | 03:23 |
anything to the HTML.
It's okay to turn on php tags, then have
| | 03:27 |
some white space.
And then do your redirect.
| | 03:29 |
That's not an issue.
It's only HTML.
| | 03:31 |
Things that would be output to the
browser, that would warrant a response.
| | 03:36 |
Back to the browser from the web server.
At that point web server will already
| | 03:40 |
have started constructing its response
back.
| | 03:43 |
Let's try an example.
I'm going to open up basic.htm and we'll
| | 03:46 |
do save as.
And let's just call this headers.php.
| | 03:51 |
Headers.
And if we want to change the header All
| | 03:56 |
we have to do is say php, and then header
http colon space 1.1 slash 404 not found semicolon.
| | 04:01 |
Okay, but what's the problem with this?
It's not the first thing.
| | 04:12 |
By the time that we get here the web
server has already read all of this and
| | 04:17 |
already started processing it.
Let's go ahead and save it and bring it
| | 04:20 |
up and try it.
We'll go back here to Firefox and in the
| | 04:23 |
Sandbox we're going to load up headers.
Warning can not modify header information.
| | 04:27 |
Headers already sent by and it tells us
the output started here.
| | 04:32 |
So, that's the warning that you're
going to get, that's the common thing
| | 04:35 |
that you'll see, headers already sent,
cannot modify that information.
| | 04:38 |
And that's why this has to be the very
first thing.
| | 04:41 |
So let's move it up here at the top.
Actually I'll leave this one here, just
| | 04:45 |
so you see, this won't work and then
let's just paste it up here at the top.
| | 04:50 |
And now there's no space here at all, the
first thing is the php tag.
| | 04:54 |
Spaces don't matter, it's only the fact
that we don't have any HTML before it.
| | 04:59 |
Let's save it and let's go back and
reload the page.
| | 05:01 |
It happily renders the page for us now,
now if you didn't have that happen, if in
| | 05:06 |
fact it worked for you down here, it may
be that you have output buffering turned on.
| | 05:11 |
So we'll come back and talk about that.
For now though let's assume that we all
| | 05:14 |
have it off and let's put it up here at
the top.
| | 05:16 |
Now, how do we see this?
We sent it, it came back but we don't
| | 05:19 |
have a way in our browser really to see
it.
| | 05:21 |
You can use Firebug.
It's a nice tool that goes with Firefox.
| | 05:24 |
You can see it that way.
But PHP also lets us see it.
| | 05:28 |
I'm going to use pretags and I'm going to
call a function here.
| | 05:31 |
We don't really need to remember this
function because you will never ever need
| | 05:34 |
it again.
But it is useful just in this one context
| | 05:37 |
and that is headers_list.
And that will return a list of the
| | 05:42 |
headers to us.
And I'll use print r with that.
| | 05:46 |
Oops put parenthesis around it.
There we go.
| | 05:49 |
And that is going to return, then, an
array, that shows us what we're getting
| | 05:52 |
back as the headers.
Now, the headers here did not include the
| | 05:55 |
status, that status wasn't one of those,
but we can change something else about it.
| | 05:59 |
Let's say header, it reported back my
version of PHP.
| | 06:03 |
That's something that's important to
note, that it does tell other web server
| | 06:07 |
what versions of PHP you're running.
A common thing that people do is change
| | 06:11 |
it, so that it doesn't.
There we go.
| | 06:14 |
And we could say, x powered by none of
your business.
| | 06:19 |
So there we go.
Let's come back and let's reload the page.
| | 06:22 |
X powered by none of your business.
So that's it.
| | 06:24 |
That's all there is to being able to set
headers.
| | 06:26 |
The most important thing is just
remembering that no space can come before
| | 06:30 |
those headers, unless you have output
buffering turned on.
| | 06:33 |
Now, we're not going to be setting
headers very often.
| | 06:35 |
It's really a rare kind of thing that
you'll do, in this context.
| | 06:38 |
But there's one time that we do do it a
lot and that's for redirects, and that's
| | 06:43 |
what we'll talk about in the next movie.
| | 06:44 |
| | Collapse this transcript |
| Page redirection| 00:00 |
Alright.
So in the last movie we took a small
| | 00:02 |
detour so that we could understand what
headers are and how they work.
| | 00:06 |
During that detour, hopefully I impressed
upon you the importance of altering
| | 00:09 |
headers before even a blank space is
output to the browser.
| | 00:13 |
Now, we're ready to talk about when
you'll need to work with the headers most
| | 00:16 |
often, and that's when we want to do Page
Redirection.
| | 00:18 |
That's where they're really useful.
So what is page redirection, and why do
| | 00:22 |
we need it?
Well, let's say that the user goes to a
| | 00:24 |
log in page, they submit their log in
information on a web form, gets sent to a
| | 00:28 |
PHP page for processing.
If they succeed in logging in, we want to
| | 00:32 |
send them to one page.
If they fail, we want to send them to
| | 00:35 |
another page.
That's where redirection comes in, being
| | 00:38 |
able to send them to another page.
A page that's different from the one that
| | 00:41 |
they requested, or even the one that the
form submitted to.
| | 00:45 |
I'm sure you've seen this in e-commerce.
You submit your order, and then after a
| | 00:49 |
brief pause, you suddenly are redirected
to another page.
| | 00:52 |
Your browser suddenly ends up with a
different URL at the end, that says
| | 00:55 |
something about your receipt, has your
order number or something like that in it.
| | 00:58 |
When that happens, you've been
redirected.
| | 01:01 |
In HTTP, the standard that the web uses,
we can redirect the browser to a new URL
| | 01:06 |
by using a 302 redirect.
It has two parts: a status code of 302
| | 01:12 |
found and a location attribute that
indicates the new URL.
| | 01:17 |
We saw in the last movie how we can
change the header.
| | 01:20 |
Well, PHP is smart enough to know that if
we're setting the location in the header,
| | 01:24 |
then we also want to set the status code
to 302 at the same time.
| | 01:27 |
We don't have to manually set it and make
a two-header request, we can just do it
| | 01:31 |
with one.
It's really nice.
| | 01:33 |
Now, notice "Location:" call in space,
and the capital "L" in location.
| | 01:37 |
It has to be exactly like that, or it
won't work.
| | 01:40 |
Capital L location: call in space and
then the new URL.
| | 01:44 |
A 302 status code in the header when it's
received by the browser, tells the
| | 01:50 |
browser that it should immediately make a
new, second get request to the new location.
| | 01:56 |
It ignores any page data that follows the
header, in fact it expects that there
| | 01:59 |
won't be anything else.
The re-request happens very fast in the
| | 02:03 |
background, so the user doesn't even know
it's happened.
| | 02:05 |
But it actually is a second get request
that takes place.
| | 02:08 |
Let's try it.
So, let's try to open up basic.html and
| | 02:12 |
let's do Save As on it, and we're
going to call this redirect.php.
| | 02:17 |
So, let's put it at the top here,
remember we already know that it has to
| | 02:21 |
go at the very top, no space at all.
We'll open our PHP tags, and this is how
| | 02:27 |
you redirect to a new page.
Header, open our parentehse, open our
| | 02:33 |
quotes, capital L Location: call in space
and then the page that you want it to go to.
| | 02:39 |
I'm just going to use basic.html.
That's going to redirect to basic.html.
| | 02:44 |
Now, this can be a relative path to the
file, which is what I'm doing here.
| | 02:47 |
I'm saying it's basically in the same
directory, or I could provide everything.
| | 02:50 |
I could say, this should go to
http:www.lynda.com, put a full URL there.
| | 02:56 |
Now, if we try this as it is, what'll
happen?
| | 02:58 |
Well, the redirect will be sent here, but
there's also data that's being sent afterwards.
| | 03:03 |
And we don't really want to send any data
after that.
| | 03:05 |
So, the last step is that you always
want to do exit afterwards.
| | 03:09 |
So, exit is going to tell it, we're done.
The script is over, the PHP script is
| | 03:12 |
finished now, we've done the redirect.
That's it.
| | 03:15 |
And that's because we don't need anything
else.
| | 03:17 |
Once we've done a redirect, that's it.
That's the whole response that's going to
| | 03:20 |
be sent back.
So let's try it.
| | 03:22 |
And we should change then this to
Redirect, just for completeness.
| | 03:26 |
And let's go back to Firefox, and let's
just try redirect.php.
| | 03:31 |
And there we are.
It redirected, notice now the URL is basic.html.
| | 03:35 |
That's where it went to, untitled is the
title of that page.
| | 03:38 |
And if we do web developer view source,
then we'll that there's the content of
| | 03:43 |
the basic.html page.
So it did send me immediately to it again.
| | 03:48 |
If you want to just try it a few times,
you can watch it happen.
| | 03:50 |
Redirect.php, and there it is, it happens
very, very quickly, especially since
| | 03:55 |
we're on the same machine, not even
reaching out across the internet.
| | 03:58 |
But even then, it happens very fast.
It's not, the speed of making a second
| | 04:01 |
request to something you really don't
have to worry about.
| | 04:03 |
Now, instead of trying to remember the,
the syntax here for remembering that you
| | 04:09 |
have to call header, you have to put
location and it has to be exactly like
| | 04:12 |
this, that you have to call exit, I find
it really helpful to wrap all of this up
| | 04:17 |
into a function.
So, let's create a function for ourselves.
| | 04:20 |
Function, and we'll call it redirect_to,
and it's going to take as an argument new location.
| | 04:28 |
And then we'll open our curly braces
there, I'll just indent all of this, it
| | 04:33 |
looks nice.
And then of course instead of putting
| | 04:36 |
basic.html here, it's going to just
append new location.
| | 04:41 |
Now, we can just call redirect to and
that will do the redirect for us.
| | 04:46 |
Just try here, redirect_to.
And let's go ahead and just give it basic.html.
| | 04:52 |
Here we are.
Come back here.
| | 04:54 |
Redirect.php, basic.html.
And send us back there.
| | 05:00 |
I find this really helpful.
I find this much easier to use, and
| | 05:02 |
user-friendly than trying to remember
that I have to do things like add exit after.
| | 05:05 |
So let's just use this in some PHP.
So let's say that we're going to a value
| | 05:10 |
for logged in, and that's going to be
equal to get, and we'll say logged in.
| | 05:16 |
So the value of get logged in is what
will become logged in, and then let's do
| | 05:20 |
a simple if statement.
If $logged_in is equal to 1, then we'll
| | 05:23 |
do one thing, otherwise, we'll redirect
somewhere else.
| | 05:31 |
If they are logged in, we're going to let
them see basic.html.
| | 05:34 |
If they are not logged in, let's redirect
them to somewhere else, like lynda.com.
| | 05:38 |
So you can see what it looks like when
you go to a different URL completely,
| | 05:42 |
that works just as well.
Save it.
| | 05:44 |
Alright.
So I definitely have to send a value for
| | 05:47 |
logged_in here.
I'm not doing any kind of default value,
| | 05:49 |
so let's make sure that I send that.
Redirect.php logged in equals 1.
| | 05:56 |
There it is, sent me to basic.html.
Let's try again.
| | 06:00 |
Redirect.php and logged in equals zero.
Really anything would work.
| | 06:08 |
And you see that it brings me up to the
lynda.com page.
| | 06:11 |
So that's it.
You need to know this syntax, which I
| | 06:13 |
think you can roll up into a nice easy to
use function.
| | 06:16 |
And that you can't put any space before
it.
| | 06:18 |
If I try something like this, then even
if I am logged in, redirect.php logged in
| | 06:26 |
equals 1.
I still get that warning, cannot modify
| | 06:30 |
header information, because something has
already been sent.
| | 06:32 |
Even though it's just a line return,
something has been sent before the headers.
| | 06:36 |
The only way that we can get around that
is by using output buffering, and that's
| | 06:41 |
what we'll talk about in the next movie.
| | 06:42 |
| | Collapse this transcript |
| Output buffering| 00:00 |
In the last two movies we talked about
headers and redirection.
| | 00:03 |
And one of the very important points
about working with headers and
| | 00:06 |
redirection is that they have to come
before any output to the HTML.
| | 00:12 |
And I stressed that several times.
Unless there's one big caveat, which is
| | 00:16 |
that we can have output buffering turned
on.
| | 00:18 |
And that's what we're going to talk about
in this movie is what output buffering is.
| | 00:22 |
And how it works.
Let me give you a metaphor to illustrate.
| | 00:26 |
Let's imagine, for a moment that our PHP
code is a faucet and our web server is a glass.
| | 00:31 |
And we're going to fill up that glass
with the output of the HTML.
| | 00:36 |
So as we execute our PHP, code the output
line by line is being sent to the web server.
| | 00:44 |
The web server as soon as it gets the
first little drop of water has the
| | 00:48 |
headers established for what it's
going to send.
| | 00:51 |
That's why we can't change them after we
start sending that code or the water to it.
| | 00:56 |
And then once the page has completely
finished rendering, then the web server
| | 01:00 |
will take that information that it has
gathered up and send it all off to the browser.
| | 01:05 |
So it accumulates in the web server.
The web server starts the headers for it,
| | 01:10 |
accumulates all the data behind it and
when it's done, ships it out the door.
| | 01:13 |
Now let's imagine that instead we have
our PHP code of the web server but we
| | 01:18 |
have PHP's output buffer that's like a
measuring cup.
| | 01:21 |
And instead of filling the glass directly
we're going to fill up the measuring cup first.
| | 01:27 |
So that's what fills up with all our data
and then when it's full or when we get to
| | 01:32 |
a certain point, a certain measure.
We're going to take and then send it over
| | 01:38 |
to our web server.
And fill up our web server with it and
| | 01:41 |
send it off.
That's how output buffering works.
| | 01:44 |
While it's still there in that measuring
cup, it's still inside php, we still have
| | 01:49 |
access to it.
That's why we're able to modify the headers.
| | 01:52 |
We haven't given anything over to the web
server yet.
| | 01:56 |
We have it still in our buffer, change
the headers in the buffer then it'll go
| | 02:00 |
to the web server at the end and at that
point the headers have all been
| | 02:04 |
established by our PHP code.
That's what output buffering is.
| | 02:08 |
Now we do pay a small performance penalty
by turning on output buffering because we
| | 02:12 |
do take the time to do this extra step.
We take a little bit of time to tell PHP
| | 02:18 |
put things together before you send them
out the door instead of just expressing
| | 02:22 |
them right on to the web server.
But I think that you'll find in most
| | 02:25 |
cases the performance penalty is not that
thigh and the benefits to gain by having
| | 02:30 |
output buffering are pretty significant.
There are two ways we can turn on this
| | 02:35 |
output buffer.
The first is that we can open up our
| | 02:38 |
php.ini file and make the adjustment
there.
| | 02:41 |
The second is that we can do it on a page
by page basis.
| | 02:44 |
So first let's open up our php.ini file.
You'll remember that early on in the
| | 02:48 |
configuration chapter we set up in our
sandbox something called my_phpinfo.php.
| | 02:51 |
And that should bring up all of your php
info.
| | 02:54 |
And if we search that page for
B,U,F,F,E,R.
| | 03:00 |
You'll see output buffering.
Output buffering for me has no value.
| | 03:06 |
That means that it is turned off, it is
not doing output buffering.
| | 03:09 |
If it had a size there, then that would
be the size of our measuring cup.
| | 03:14 |
That's how much it's going to buffer
before it sends it.
| | 03:17 |
So let's remind ourselves where our
php.ini file lives.
| | 03:20 |
If we go up to the top, you'll see that
it tells us the loaded configuration file
| | 03:25 |
is right here at this path.
So that's where it's located.
| | 03:29 |
Let's open up our command line and I'm
just going to do nano on that file, php.ini.
| | 03:35 |
That's going to open it up in the Nano
Text Editor and let's go ahead and do a
| | 03:40 |
find for output buffering.
Ctrl+W allow me to search inside of there
| | 03:46 |
and I'm going to just look for buffer,
output buffering, so it shows you the
| | 03:52 |
default value is off, development value
4096.
| | 03:55 |
That's a pretty good and standard value,
that 4096.
| | 03:58 |
But this is just code comments.
This is not the actual setting of it.
| | 04:02 |
Let's just do another search in here,
Ctrl+W, search for buffer again.
| | 04:06 |
All right, here's a paragraph all about
output buffering.
| | 04:08 |
It explains how it works.
It's everything that I just told you and
| | 04:12 |
a little bit more.
And it goes on to then tell you that the
| | 04:14 |
possible values are On Off, or an
integer.
| | 04:17 |
So right now we scroll down a little bit
more, you'll see that mine is turned off.
| | 04:23 |
We did that back in the configuration.
We made sure that it was turned off for
| | 04:26 |
this reason.
Now I'm going to turn it back on.
| | 04:29 |
Now I could simply type on but instead of
having an unlimited output buffer, I'm
| | 04:33 |
going to go ahead and give it a limit.
That's the idea of the measuring cup here
| | 04:37 |
that it will fill up at some point.
It will take 4,000 characters before it
| | 04:41 |
fills up.
So I have 4,000 characters in which to
| | 04:44 |
still change my headers before the output
buffer is full and it sends it to the web
| | 04:48 |
server for the first batch.
So now that I have that change.
| | 04:52 |
I can exit using Ctrl+X.
And let's type a y for save the changes.
| | 04:57 |
And a Return to save them in place.
Now you'll need to restart your web server.
| | 05:01 |
Sudo apachectl restart.
Wants my password.
| | 05:08 |
And there it is that's my password used
to install software.
| | 05:11 |
Now it has restarted and my output
buffering should be turned on.
| | 05:14 |
If we come back over here, we reload this
page and once again search for buffer.
| | 05:20 |
I'll put buffering as now 4096, now
output buffering is turned on.
| | 05:24 |
That means now that if you went back to
the previous two movies again with
| | 05:28 |
modifying headers and page redirection
you would be able to send white space
| | 05:33 |
before you do those redirects and header
modifications.
| | 05:38 |
Now addition to turning in on all the
time for all the pages in the php and I
| | 05:43 |
can also turn it on on a page by page
basis.
| | 05:46 |
And I do that by using a PHP function
called ob_start for output buffer start
| | 05:53 |
and then when I'm done output buffer end
flush.
| | 05:57 |
So that's going to start the buffer.
Everything that happens after that will
| | 06:01 |
go in the buffer.
And at the end I have to say okay, I'm
| | 06:04 |
done now.
You're free to flush this over to the web server.
| | 06:08 |
So your page starts with ob_start and
ends with ob_end_flush.
| | 06:13 |
Very important though, ob_start has to
come before any content.
| | 06:18 |
Just the same as the headers, otherwise
we won't have started the output buffer.
| | 06:22 |
Will output a little bit of HTML.
The headers are already sent, then we
| | 06:26 |
turn on the buffer but it's too late.
So it has to be the very first thing that
| | 06:29 |
we do.
Also it won't hurt to turn it on again if
| | 06:32 |
you've already turned it on in the
php.ini file.
| | 06:36 |
So which one should you use?
Should you set your php.ini file or
| | 06:39 |
should you set it in your application?
I would say that it's better to set it in
| | 06:43 |
the php.ini file so you don't forget to
do this for any page.
| | 06:47 |
So if you just have it set there then it
will always be on.
| | 06:51 |
However you need to understand that it
means that if you put it on a server
| | 06:55 |
where it is not enabled it's not going to
work.
| | 06:58 |
You're going to have to make sure the php
and ini file has the output buffering
| | 07:01 |
turned on and by default it usually is
now so that shouldn't be a problem.
| | 07:05 |
If you're in doubt, if you're creating
something that is going to go out to
| | 07:08 |
other servers out there and you're not in
control of those php.ini files, then I
| | 07:13 |
would say use the page by page version
and just make sure that you do it application-wide.
| | 07:18 |
Maybe it's something that you always
include in your template so that it
| | 07:22 |
always starts and ends the output
buffering before you do anything else.
| | 07:26 |
And that way you'll get the maximum
amount of compatibility.
| | 07:29 |
But for yourself, I think you can just
turn it on your php ini file and be just fine.
| | 07:33 |
| | Collapse this transcript |
|
|
11. Working with Forms and Form DataBuilding forms| 00:00 |
Now that we know how to build web pages
to use dynamic data.
| | 00:03 |
In this chapter we're ready to use data
that has forms for submitting form data.
| | 00:08 |
this represents the second way that we
can get data back from a user.
| | 00:11 |
They can either click a link, or type a
URL, which is basically the same thing.
| | 00:15 |
Or they can submit a web form to us.
You remember that URL and links are GET
| | 00:20 |
requests, and submitting a form is going
to be a POST request.
| | 00:24 |
With GET request we saw that PHP
automatically took all of the query
| | 00:27 |
parameters and put them into an
associated array assigned to the super
| | 00:30 |
global GET.
Well guess what, PHP is going to do the
| | 00:34 |
exact same thing for us with POST request
by using the super global POST.
| | 00:38 |
That's the dollar dign, underscore
followed by capitals POST.
| | 00:43 |
All the values posted in the form data
will be in that associative array, ready
| | 00:46 |
for us to access, just like we did with
the links.
| | 00:49 |
So, if a form has a first name input
field, then we'll be able to ask the POST
| | 00:53 |
superglobal for the key that matches the
input field name.
| | 00:56 |
When we submit forms, there's generally
going to be two pages, one page which has
| | 01:00 |
the web form on it, ready to be filled
out.
| | 01:02 |
And then, a second page that does the
processing of the form.
| | 01:06 |
Let's try creating those two pages.
So to begin with, let's make our form
| | 01:09 |
page, and I'm going to just use basic
HTML as a template.
| | 01:12 |
I'll do Save As, and I'm going to call
this form.php.
| | 01:15 |
Form, and then I'm just going to paste in
some HTML here, so you can pause the
| | 01:21 |
movie if you need to copy that out, but
it's just basic HTML form.
| | 01:25 |
So I've got my form with its action,
which is form processing.php.
| | 01:29 |
That's the page that doesn't exist yet,
but it's where we're going to send this
| | 01:33 |
data, so this is going to post the data
to form processing.php.
| | 01:38 |
And the method that it will use is post,
and that's common, and we're going to do
| | 01:42 |
that with almost all forms, send them as
post data.
| | 01:45 |
And then we've got our user name, where
I'm going to have an input field.
| | 01:48 |
It's going to be a text field, and the
name of that is going to be username.
| | 01:52 |
Now, this is important because this is
the key in the associative array on the
| | 01:56 |
processing page that I'm going to look
for.
| | 01:58 |
So inside post, there'll be a value for
username, and it'll be exactly the name
| | 02:03 |
that's here.
Now, there is no value for it, it's just
| | 02:05 |
going to be a blank field.
Then on the next line, I've got password.
| | 02:09 |
Everything's the same, except that it's
of type password, which just means that
| | 02:13 |
it doesn't show the text as I've typed
it.
| | 02:14 |
It puts bullets instead.
And then last of all, I've got a submit
| | 02:18 |
button down here, that's what this is.
And it's going to have the text submit on
| | 02:22 |
it, the type is submit.
I've also got a name attribute for submit.
| | 02:26 |
We'll talk about in a couple movies why
we want to have that.
| | 02:29 |
For now, just go ahead and make sure that
yours has it too.
| | 02:31 |
And then we close the form tag.
So that's it, it's just basic HTML.
| | 02:34 |
Let's save it and just bring it up in a
browser.
| | 02:36 |
So for me that's going to be localhost
and then kevinskoglund/sandbox/form.php,
| | 02:44 |
so there it is.
It's not the prettiest form in the world
| | 02:46 |
but it gets the job done.
So we would type in a name here,
| | 02:49 |
username, and then you type in your
password, you can see that it puts
| | 02:53 |
bullets there instead of showing you the
text.
| | 02:55 |
And then I click Submit, and there is it.
It submitted it to form_processing.php a
| | 03:01 |
page that doesn't exist yet because I
haven't created it.
| | 03:03 |
But you can see it did switch the URL to
be that and in the process of doing that
| | 03:08 |
it sent along all those attributes as
post parameters.
| | 03:11 |
They don't show up here, they're not up
in the URL, but they were sent.
| | 03:15 |
They were packaged up in the browser and
sent along with the request to the web server.
| | 03:20 |
So let's take a look at how we can view
those.
| | 03:23 |
Let's just set this aside, and I'm
going to go back to Sites and open up
| | 03:26 |
basic.html again.
This time, though, I'm going to Save a
| | 03:29 |
copy as form_processing.php.
I don't really have to have the HTML here.
| | 03:38 |
I could just have a strictly PHP page,
but I'm going to go ahead and be
| | 03:41 |
displaying some output.
So I'm going to leave the HTML.
| | 03:43 |
So now if I were to reload the page, the
page does exist, but I would get nothing.
| | 03:48 |
It would do nothing there.
Let's take a look at those values to
| | 03:51 |
start with.
So inside pre tags, let's do our php tags.
| | 03:55 |
And our closing pre tags, so I don't
forget.
| | 04:00 |
And inside there, we're going to use
print_r and take a look at that POST
| | 04:05 |
super global, which is an associative
array.
| | 04:06 |
So let's go back over here, let's go back
to this page.
| | 04:11 |
I'll put in another password again and
then hit Submit, there we go.
| | 04:16 |
You see what came through?
Here's an associative array.
| | 04:18 |
Username, there's the key, there's the
value.
| | 04:21 |
There's the key, there's the value and
then submit.
| | 04:24 |
It went ahead and just gave me that value
as well.
| | 04:26 |
Now let's just try loading that page on
it's own again, just from the URL string.
| | 04:30 |
Let's just hit Reload up here.
It actually says do you want to reload it
| | 04:34 |
with the post data, let's skip that, we
don't want to do that.
| | 04:37 |
What I want to show you is, let's just
hit Return from the URL bar, so it's
| | 04:41 |
actually making a get request to form
processing.php, do you see that?
| | 04:45 |
It's not a post request, it's a get
request, because it's coming from the
| | 04:48 |
URL, and now my array is empty.
We have to come from this page so it's a
| | 04:53 |
post request and so that the data is in
fact posted with the post request.
| | 04:59 |
See how that works?
So now we're able to go back to that page
| | 05:02 |
and see those values let's actually just
output some values.
| | 05:05 |
Let's do br tag here and we'll open up
php again.
| | 05:09 |
And let's get the values, let's say
username equals, well how do we do that?
| | 05:14 |
POST, and then the key that we want,
username, there you go.
| | 05:18 |
We'll do the same thing, I'll just Copy
and Paste it, and password, password.
| | 05:24 |
So now I've set variables equal to that
value.
| | 05:27 |
So now I'm ready to work with them, it's
a little more convenient I think to work
| | 05:29 |
with these variable then it is to always
have to type out the super global version.
| | 05:33 |
Plus later we're going to do some
checking and some conversion on some of
| | 05:36 |
this stuff.
So we're probably going to do that when
| | 05:38 |
we're bringing it into our variable
making it a good safe value.
| | 05:42 |
And the let's, let's do echo, so echo and
lets do dollar sign username and password.
| | 05:49 |
Now obviously you would not be doing this
kind of echoing in the real world.
| | 05:53 |
We're just doing it for testing purposes.
Probably you would want to do something
| | 05:55 |
else with these values like store them in
the database, use them to look up data.
| | 06:00 |
Or in this case to use them to actually
log this user at.
| | 06:03 |
But for now we're just going to show that
we've successfully pulled those values
| | 06:06 |
in, assigned to variables, and now we can
work with them.
| | 06:09 |
So let's go back, let's have our form,
and let's Submit, and there we go.
| | 06:14 |
There's my username, my password, and
then down here you can see that I've got
| | 06:18 |
kevinskoglund and secret.
Now unlike GEt request, POST data does
| | 06:22 |
not need to be encoded or decoded.
So all that stuff that we worried about
| | 06:26 |
before with the query string, you aren't
going to need to worry about with post data.
| | 06:29 |
That post data is packaged up separately,
so that it doesn't interfere with what's
| | 06:34 |
going on in the query string at all.
So the only time you'll need to worry
| | 06:38 |
about that is here for the action.
Because that is going to be in the query
| | 06:41 |
string if you were doing some dynamic
processing there.
| | 06:45 |
Then you would need to do those normal
things that we saw for working with get requests.
| | 06:49 |
But the data itself, what's inside here
inside the form, does not need any kind
| | 06:54 |
of encoding whatsoever.
So the very last thing is, let's just
| | 06:57 |
come back over here now and let's reload
this page again, just like we just did
| | 07:01 |
before and let's hit Return.
But this time I'm not just casually
| | 07:05 |
showing the output of the array.
I'm actually trying to assign some values.
| | 07:09 |
And I did that by asking for an actual
key, user name.
| | 07:12 |
And it says oops, notice that key doesn't
exist.
| | 07:15 |
So I'm getting a notice now when I try
and do this because that form value
| | 07:20 |
wasn't sent.
In the next movie let's talk about how
| | 07:22 |
you detect form submission to know
whether you can expect that value to be
| | 07:26 |
there or not.
| | 07:27 |
| | Collapse this transcript |
| Detecting form submissions| 00:00 |
In the previous movie, we learned to
create and retreive the values from a web
| | 00:03 |
form, but we ran into an issue.
When the form was submitted, our page
| | 00:07 |
worked just fine.
But if the URL was loaded directly,
| | 00:09 |
instead of being submitted by the form,
then the key values that our code was
| | 00:14 |
expecting to be in the post array weren't
there.
| | 00:17 |
In this movie, we're going to talk about
how you can detect whether or not a form
| | 00:20 |
has been submitted, so that you can
handle this case appropriately.
| | 00:24 |
So, the problem that we have is that
we're looking for this key, username, and
| | 00:29 |
it's not there.
It doesn't exist in the post array,
| | 00:32 |
because the form wasn't posted.
So therefore, it doesn't exist, and we're
| | 00:35 |
getting an error when we try and access
it.
| | 00:38 |
One simple solution to this would be to
use is set.
| | 00:41 |
We remember how to do that, so we could
do something like this.
| | 00:44 |
We could say if and then is set, no
spaces.
| | 00:47 |
Dollar sign post and then username.
So if that is set, then set username.
| | 00:56 |
And we can do the same thing for
password.
| | 00:58 |
So now if we go back and try our page,
we're just going to load it directly by
| | 01:01 |
hitting return and we still come up with
an error.
| | 01:05 |
Why are we getting the error this time
well this time it's different.
| | 01:07 |
This time it's undefined variable.
That's because here we're trying to use
| | 01:11 |
it but if it's not set.
Well this value doesn't get set so now
| | 01:14 |
this doesn't exist.
So we solved one problem and created
| | 01:17 |
another problem at the same time.
So we could do else dollar sign user name
| | 01:23 |
equals, and then let's do the same thing
down here, else dollar sign password equals.
| | 01:30 |
Now let's go back and load up our page
this time, and now we've solved both problems.
| | 01:34 |
We're still getting this stray colon
output, because it's outputting blank
| | 01:37 |
string, followed by a colon and a space,
and then a blank string.
| | 01:40 |
But we've gotten rid of the errors by
doing this.
| | 01:43 |
But we've written a whole lot of code
here, just to hangle this.
| | 01:46 |
Now, this incidentally is a good way to
set default values.
| | 01:49 |
I'm going to go ahead and make a note
here that default values.
| | 01:52 |
Because if you needed a default value for
something, this is a good way to do it,
| | 01:56 |
to say If it's set, then set it to that
value.
| | 02:00 |
If it's not set, set it to your default,
and you would just provide the default
| | 02:03 |
value here.
It doesn't have to be empty.
| | 02:05 |
It could be my default value is Kevin.
Maybe if you have a form that has a
| | 02:08 |
country field, you want to default to
your current country, the United states
| | 02:12 |
or Canada or something like that.
Now, If you do decide to go this way, I
| | 02:16 |
just want to show you that there is a
better way to do that, and that is by
| | 02:20 |
using the ternary operator.
Let me just close the PHP tags here.
| | 02:25 |
There we go.
And let's open a new one.
| | 02:26 |
And now those are separated.
And let me just show you what a ternary
| | 02:30 |
operator looks like.
We didn't talk about this back when we
| | 02:33 |
talked about if statements, conditionals.
It's just a special format for doing the
| | 02:37 |
if statements, and it has a Boolean test,
followed by a question mark, then the
| | 02:42 |
value if the test is true.
Then a colon and then the value of the
| | 02:45 |
test is false.
So, you can see that this is a lot more
| | 02:47 |
efficient than writing all of this, but
it does the exact same thing.
| | 02:51 |
So, it's just a more compact way of
writing it.
| | 02:53 |
If this is set, then set it to this, if
not, set it to that.
| | 02:57 |
So, I want you to see that because that's
a little more efficient.
| | 02:59 |
I think, a nicer way to do that.
But, I think there's an even better way
| | 03:03 |
to do it than that, in this case.
A way to detect whether the form has been
| | 03:07 |
submitted or not.
And the way to do that, will put an if
| | 03:11 |
statement, is set.
Just like we just did.
| | 03:15 |
And we'll look in that post array.
But were going to look for summit.
| | 03:19 |
And then, for now, let's just do echo,
form was submitted.
| | 03:23 |
So, what we're doing here.
Let's go back and remind ourselves what
| | 03:26 |
that looks like.
Let's go to the form, and let's submit it.
| | 03:29 |
I'll click submit.
Notice that there's this other value here
| | 03:33 |
called submit.
Doesn't matter what all of the form is.
| | 03:35 |
I have something here called submit.
And that's what I'm checking.
| | 03:38 |
Form was submitted.
So it did, in fact, say that it was submitted.
| | 03:41 |
If I come here, and I do it this way.
Now the form was not submitted.
| | 03:45 |
All of my default values also kicked in.
That's beside the point.
| | 03:48 |
The thing that I want you to see is that
this form was submitted whether it shows
| | 03:52 |
or not.
So what's required to make this work is this.
| | 03:56 |
Name equals and then something.
I just use submit, that's the easiest thing.
| | 04:00 |
But you need to have some name attribute
there.
| | 04:03 |
because that's going to make sure that in
any time that the submit button is
| | 04:06 |
clicked, it's value is included in that
array.
| | 04:09 |
If we take that away.
Let's try that.
| | 04:12 |
Let's go back and we'll reload this page.
And I'll just type in Kevin, password and Submit.
| | 04:19 |
Now it's not there.
So that's the key thing.
| | 04:21 |
You have to have this name equal submit
for this trick to work.
| | 04:24 |
So what does that gain me?
Why do I want to do that?
| | 04:26 |
Well now, I have one way to check right
off the bat whether or not this has been submitted.
| | 04:32 |
Before I do any of these other tests, I
can take all of these.
| | 04:35 |
Let me just cut it and let's paste it in
here, and the ternary one.
| | 04:42 |
I'll go ahead and grab as well, just so
that they're all together, and get rid of that.
| | 04:47 |
Okay?
You see the difference?
| | 04:48 |
So now It's going to check and see
whether the form was submitted, and if it
| | 04:53 |
is, it's going to do all of this
processing.
| | 04:56 |
If not, it skips right past it.
Now, it'll give me an error if I try and
| | 05:00 |
load this, because this is outside of
that, and I don't have a value for
| | 05:04 |
username and password if I skip
everything.
| | 05:06 |
But I could easily fix that.
Else and then set the default values
| | 05:10 |
here, username equals and password
equals.
| | 05:15 |
That will then work just fine.
Let's come back over here, form
| | 05:18 |
processing, reload the page, none of that
takes place, these are the values that
| | 05:22 |
kick in.
And if you want to prove to yourself you
| | 05:24 |
can put in Bob here Lets just reload the
page you see that's the value being set.
| | 05:30 |
So this checks to see whether not post
variables submit a set.
| | 05:33 |
And then we know it was posted to us.
If it wasn't then it's a get request then
| | 05:38 |
we can handle the page differently based
on whether it's a get request or a post request.
| | 05:43 |
What's nice about this, is that instead
of using two pages to do our form submission.
| | 05:48 |
One that has the form and one that
receives the form, we can actually use a
| | 05:53 |
single page to both have our form and
perform our form processing.
| | 05:57 |
Let's look at that next.
| | 05:58 |
| | Collapse this transcript |
| Single-page form processing| 00:00 |
In the last movie, we learned how to
detect if a form was being submitted to a
| | 00:03 |
page or whether the page was being loaded
directly.
| | 00:07 |
We first learned it in order to keep our
page from blowing up on us if our values
| | 00:10 |
weren't in the post array.
However, there's another way that we can
| | 00:13 |
use it that's super-handy.
It will allow us to have a single page
| | 00:17 |
that contains both the form and the form
processing.
| | 00:20 |
We'll essentially have a form that
submits to itself.
| | 00:22 |
Now there's nothing wrong with having the
form and the processing on two separate pages.
| | 00:26 |
I'm not trying to tell you that one is
better than the other.
| | 00:28 |
But having them on one page does add a
few nice perks.
| | 00:32 |
All the logic related to the form, then,
is in one file.
| | 00:35 |
So, if you have a form that is for
logging in, we have both the login html
| | 00:40 |
code that is for displaying the form.
And the processing of it, all in a single
| | 00:44 |
file maybe called login.php.
It also makes it very easy then to
| | 00:49 |
redisplay the form if there are errors,
because if there are errors we just let
| | 00:54 |
the page keep processing.
And guess what's further down the page?
| | 00:57 |
The form.
It also makes it easy then to return
| | 01:00 |
error messages to that form, that's
going to show up a little later.
| | 01:02 |
And to populate those fields with
previous values.
| | 01:06 |
So, let's say that you were asked to
enter a country name.
| | 01:09 |
And the country value that you entered
was not a valid country.
| | 01:11 |
Well we could redisplay the form and
include the value you had before.
| | 01:15 |
So, that you could make a correction to
it and resubmit it again.
| | 01:19 |
Now a single page form is not the only
way to get these benefits.
| | 01:22 |
Especially the last three.
You also know how to use include, to pull
| | 01:26 |
in a common form file for both pages that
would also work.
| | 01:29 |
But I do want you to see this technique.
I do want you to see how it works because
| | 01:33 |
this way of doing it can also be useful
instead of just doing it by using include.
| | 01:38 |
So, we already had our form, and our form
processing pages.
| | 01:41 |
I just want to take these and resave them
as a few file.
| | 01:45 |
So, I'm going to go to form.php and I'll
use Save As on this, and we'll call this
| | 01:49 |
formsingle.php, we'll save it.
Now instead of going to form processing
| | 01:54 |
we know it's going to submit to form
single.
| | 01:56 |
So, it's going to submit to itself.
Now once it gets there it needs to do
| | 02:00 |
some processing, so let's go to our form
processing and let's steal some of the
| | 02:04 |
bits that we had here.
What I mainly want to get is this line
| | 02:07 |
here that checks to see whether or not
the submission took place.
| | 02:11 |
And I'm going to put it before we even
get up to the dock type.
| | 02:14 |
Put all the way up here at the top inside
some php tags there we go.
| | 02:19 |
So, if it has been submitted form was
submitted that's what that tells us, if
| | 02:26 |
not we will go ahead and put an else here
well do whatever happens if it's not submitted.
| | 02:31 |
So what do we want to do?
If the form was submitted, then we want
| | 02:34 |
to set username equal to post and
username.
| | 02:39 |
And we'll do the same thing for password.
Password.
| | 02:42 |
And for now, let's just go ahead and have
it also output a message, we'll set
| | 02:46 |
message equal to logging in.
And then we'll put the user name.
| | 02:52 |
So, this is a just placeholder, just so
that we can see something come back to us
| | 02:57 |
when we're doing this.
Of course we're not actually trying to login.
| | 03:00 |
We're not accessing a database or
anything like that.
| | 03:02 |
We're just going to have it display I'm
trying to log them in.
| | 03:05 |
And we'll just leave it at that.
Now if it's not submitted, then let's
| | 03:08 |
have an alternative message.
And that one's going to be please log in.
| | 03:13 |
Nice and simple so now we have a message
that occurs one time or not the other time.
| | 03:17 |
Let's drop down here into the form and
let's just echo that message right above
| | 03:21 |
the form.
We'll put our PHP tags echo the message
| | 03:25 |
and we'll put a br tag at the end.
So, let's just try that let's bring that
| | 03:29 |
up and see what we get.
And instead of form processing, let's
| | 03:33 |
load form single.
So, there it is, I get please log in and
| | 03:37 |
now I"m going to try to log in.
Kevin (UNKNOWN), password password, and
| | 03:41 |
there it is, logging in Kevin (UNKNOWN).
Now you can see that the two actions, the
| | 03:46 |
two possibilities for this page, one is
if the form was submitted.
| | 03:50 |
And one is if it's not, and in both cases
it's going to redisplay the form to us
| | 03:54 |
right now.
We'll take care of that in just a minute.
| | 03:57 |
Now we also could put in default values
here.
| | 04:00 |
So, the username you notice when it came
back and said logging in, it didn't give
| | 04:05 |
me the user name again.
But what if this had been an error?
| | 04:07 |
Let's just do that, instead of logging in
let's have it say, there were some errors.
| | 04:14 |
Now of course there aren't errors.
We'll just reload the page this way.
| | 04:17 |
It'll ask me if I want to resubmit it
with the post data.
| | 04:20 |
That's what it's asking me for.
And then it comes back and says there
| | 04:23 |
were some errors.
All right, so now let's imagine that
| | 04:25 |
there are errors.
Well, we would want it to redisplay the
| | 04:27 |
username to us.
Echo, make sure you use echo username,
| | 04:30 |
there we go.
It's going to echo the username, but
| | 04:35 |
let's be careful here.
We have now the condition where username
| | 04:38 |
is set if it was posted, but if it
wasn't, this isn't going to have a value.
| | 04:42 |
So, our form won't work in this case
unless we provide the default value here.
| | 04:47 |
So, now we have a default value.
Let's come back, we'll reload the page by
| | 04:50 |
hitting the Return key in the URL, and we
get please log in.
| | 04:53 |
Kevin and Submit.
There were some errors and it repopulated
| | 04:59 |
my username here with Kevin.
Now, because that's something that the
| | 05:02 |
user is submitting, we want to make sure
that we're careful about that and use
| | 05:06 |
HTML special chars on it.
We learned about that.
| | 05:10 |
Put my semicolon at the end.
So, that will make sure that no matter
| | 05:13 |
what the user typed in there, maybe they
typed in a name that had a less than sign
| | 05:17 |
in it, it's still not going to break it.
It comes back just fine.
| | 05:20 |
The message here, we don't need to worry
about putting HTML special chars on
| | 05:23 |
because we have control of that message.
It's not something that the user would
| | 05:27 |
have submitted.
Now the moment that we start putting user
| | 05:29 |
data in there, or database data in there.
Then we'll want to also make sure we
| | 05:33 |
escape that, but for now I know what that
message is going to be.
| | 05:36 |
It's going to be one of two things so I
don't really need to be worried about it.
| | 05:40 |
So, let's also try incorporating a
redirect.
| | 05:42 |
We saw how to do redirects earlier.
I could add the redirect function to the
| | 05:45 |
top of this page but I'm actually going
to do it the proper way.
| | 05:48 |
And I'm going to go over here to included
functions that I was working with before.
| | 05:52 |
And I'm just going to add a new function.
Don't worry about, hello, that's fine.
| | 05:55 |
But I'm going to add that redirect to
function from here, and this file then,
| | 05:59 |
can hold my functions.
And then, I'll come back over here and
| | 06:02 |
I'll use require once on included
functions.php.
| | 06:10 |
And now I can use that redirect too.
The form was submitted.
| | 06:13 |
Let's put in a conditional that checks to
see whether or not we've successfully
| | 06:17 |
logged in.
Now we're not, don't have a database set
| | 06:20 |
up or anything, but let's just do a real
simple one.
| | 06:21 |
Let's say the user name is equal to Kevin
and the password is equal to Secret, then
| | 06:30 |
we'll have a successful login.
Successful login.
| | 06:34 |
If not, well, then there were some
errors.
| | 06:38 |
Take this and paste it up here.
I actually don't need password, because
| | 06:41 |
I'm not re-displaying the password to the
user.
| | 06:42 |
But I do want to have username set, so
that I get that nice value dropped back
| | 06:47 |
in my form.
So a successful login now will redirect to.
| | 06:51 |
And let's just have it go to basic.html.
So let's try it.
| | 06:56 |
Come back into my form, load it up.
Please log in.
| | 06:58 |
I'll put in Kevin S, to begin with, with
password of secret.
| | 07:04 |
That's not going to work.
Oh, undefined variable user name, alright.
| | 07:07 |
So I have a problem here.
Let's go and take a look, at line seven.
| | 07:12 |
User name is not set.
ha.
| | 07:15 |
I need to move this, up above.
And I do need to add my password back in again.
| | 07:20 |
Password, password.
Okay.
| | 07:23 |
So now, those will be set, and then I can
check those values.
| | 07:25 |
Let's try it one more time.
There's the form.
| | 07:27 |
Kevin S.
Secret.
| | 07:30 |
Submit, there were some errors.
My name re-displayed.
| | 07:33 |
let me fix that.
It's kevin, secret, make sure I type it
| | 07:36 |
right, Submit and I got redirected to
basic.html.
| | 07:39 |
You're sure to see how this all comes
together, how we're able to have
| | 07:43 |
different conditionals, the redirects,
the default values, a form.
| | 07:48 |
All of this happens and we're doing it
all on a single page.
| | 07:51 |
Now that we have some comfort working
with forms, in the next movie I want to
| | 07:55 |
talk about how to validate form data.
| | 07:56 |
| | Collapse this transcript |
| Validating form values| 00:00 |
When we accept data from users especially
from web forms, let's say, submit, we
| | 00:04 |
almost never want to just accept any old
data that they send In fact, it's to our
| | 00:08 |
benefit as developers to assume that
users are sending us the worst kinds of
| | 00:12 |
data possible, that may be even trying to
hack our site and do us harm.
| | 00:16 |
Checking the submitted data carefully is
the road to having robust and secure code.
| | 00:20 |
So we need to spend some time thinking
about requirements for our data and
| | 00:23 |
learning how to enforce those
requirements.
| | 00:25 |
I promise you're going to spend
significant time on this for every
| | 00:28 |
project that you do, so it's worth
investing the time in learning how to do
| | 00:31 |
it and to avoid the pitfalls.
Imposing data requirements is called
| | 00:35 |
validating our data, and you'll hear me
refer to the process as validations or
| | 00:39 |
passing or failing our validations.
If data passes validations, it means that
| | 00:44 |
data was acceptable and we can use it.
If it fails validations, that means there
| | 00:48 |
was a problem and we need to reject it.
And most often that means going back to
| | 00:51 |
the user to ask them to make revisions
and submit it again.
| | 00:55 |
The simplest and most common requirement
for a form field is that the user submits
| | 00:59 |
some value that the form field can not be
left blank.
| | 01:02 |
We call this validating the presence
making sure that something was present in
| | 01:06 |
the field.
Now we can have other requirements too.
| | 01:08 |
For example, we could check for the
number of characters that they submitted
| | 01:12 |
to make sure that something is either
longer than a certain number of
| | 01:14 |
characters, shorter than the certain
number, maybe between a certain range
| | 01:18 |
where that is exactly a certain number of
characters.
| | 01:21 |
We can validate the type, make sure that
they've sent us a string or an integer or
| | 01:25 |
a float, if that's what we're expecting.
We can validate that it's included in a
| | 01:29 |
set from a select set of choices.
So, if we'd ask someone to choose whether
| | 01:34 |
they're male or female, the answer we get
back, we would expect to be either male
| | 01:38 |
or female.
And if it's not one of those two choices,
| | 01:40 |
we want to reject it.
Once we start working with databases,
| | 01:43 |
we'll start wanting to check whether
things are unique or not.
| | 01:45 |
This especially comes into play with
something like a username.
| | 01:48 |
Everyone needs to pick a unique username,
so we need to take the value they've
| | 01:51 |
given us for their preferred username and
then check the database to see whether or
| | 01:55 |
not that is a valid, unique username.
And the last common one would be format.
| | 02:00 |
And that's just checking that an email
has the at symbol in it, that currency
| | 02:04 |
has a dollar sign at the beginning.
For dates and times they might need to
| | 02:08 |
end with a.m.
or p.m.
| | 02:09 |
Any kind of format for the format that
something should be submitted to us in,
| | 02:12 |
we can also check for.
Now these are general categories.
| | 02:16 |
They're, by no means, a full list.
You'll need to write custom validations.
| | 02:19 |
For example, a field might contain a path
to a PDF file and your validation would
| | 02:23 |
check to make sure that that file already
exists.
| | 02:26 |
Or you might validate whether a value is
greater than 20 or that a value is odd or even.
| | 02:30 |
Really, the sky's the limit when it comes
to validations.
| | 02:32 |
So let's try a few of these common ones
just to get a feel for them.
| | 02:35 |
So to start with, let's open up
basic.html again and we'll do Save As on it.
| | 02:41 |
I'm going to call it validations.php.
Validations.
| | 02:42 |
So in here let's open our PHP tags and
I'm going to put some place holders for
| | 02:44 |
the different types that we just talked
about are common.
| | 02:49 |
Present string length, type inclusion,
uniqueness, and format.
| | 02:56 |
So let's go through each one of these.
Let's talk about presence first.
| | 02:59 |
So, with presence, you could use isset.
We can say, just make a conditional if
| | 03:04 |
it's not set.
And let's say we're just going to check
| | 03:07 |
for value.
Then, we can check and say well, the
| | 03:12 |
validation failed.
Echo validation failed.
| | 03:16 |
Okay.
So that just checks whether the value was
| | 03:18 |
actually set.
So let's say, we have value equals quote,
| | 03:23 |
quote, semicolon.
Right?
| | 03:25 |
So we have an empty string sent.
Well, it is set.
| | 03:28 |
That's not what we were checking for.
It's good to check to see if it's set or
| | 03:31 |
not, that's a good sanity check.
But a blank field will still set a key in
| | 03:35 |
our post superglobal.
So, it's not a bad fail-safe to have, but
| | 03:38 |
we really need to do more checking than
that.
| | 03:40 |
And one way to do that is with empty.
And I'll just do empty value.
| | 03:45 |
So, now if we try this.
Let's bring it up in a browser.
| | 03:49 |
Let's check validations.php.
The validation failed but, if we have
| | 03:52 |
something in there let's put an x.
Save it.
| | 03:56 |
Reload the page, and it succeeded.
We didn't get anything back.
| | 03:59 |
So we're only getting a message if it
fails our validations.
| | 04:02 |
So I'm going to make it so that it does
pass.
| | 04:03 |
We did make sure we have the presence
there.
| | 04:05 |
Incidentally, we're going to talk more
about empty in the next video and some of
| | 04:08 |
the problems that come up with it.
So, for now, we're going to leave it.
| | 04:11 |
And just make that what we look at for
presence.
| | 04:13 |
We'll revisit it in a moment.
With string length, we could have the
| | 04:17 |
minimum length.
Let's say that we have another value
| | 04:20 |
here, and for now, let's say that the
value is empty again.
| | 04:24 |
And this time, we're going to check to
see if the string, we know it's present.
| | 04:28 |
Now we want to see string length, strlen,
of value.
| | 04:33 |
And that's going to return an integer for
how large it is.
| | 04:36 |
Is it less than the minimum?
And if it is, well, then the validations failed.
| | 04:42 |
I'll just copy that and paste it down
here.
| | 04:44 |
So that's going to be minimum length and
that's minimum length.
| | 04:49 |
And let's set a value for the minimum.
Let's say that the minimum is equal to 3.
| | 04:55 |
So it must be at least 3 or it fails our
validations.
| | 04:59 |
Let's go ahead and make another one here
and we'll just do max length.
| | 05:04 |
And we'll use the same value but we're
going to write max equals, and let's make
| | 05:09 |
it 6.
So if it's greater than the max, well,
| | 05:13 |
then we have another problem.
So, we're validating the length is, above
| | 05:16 |
a minimum and below a maximum.
So, let's just try that real quick.
| | 05:19 |
We've got this value here, that's empty
right now.
| | 05:21 |
Let's reload the page and validation
failed.
| | 05:23 |
That's not my first validation.
This one's passing.
| | 05:25 |
It's one of these that we're seeing.
And it doesn't tell us which, but that's okay.
| | 05:28 |
We know which one it is.
Let's go ahead and make this value, now,
| | 05:31 |
something else.
Let's say that it's abcd.
| | 05:34 |
So now it's in that range and it works
just fine.
| | 05:37 |
We make it efghijk, save it, validation
failed.
| | 05:42 |
Now, of course, you could use custom
messages for this, so that it would say
| | 05:46 |
that the validation failed because of the
following reasons.
| | 05:49 |
But for right now, I just want to show
you how validations work.
| | 05:52 |
So let's do type.
I'm just going to copy this because it's
| | 05:55 |
going to be very similar.
And let me do value equals first and this
| | 06:01 |
time the test though that we're going to
put in here is going to be, is it a string?
| | 06:07 |
So, if value is not a string, then the
validation fails.
| | 06:10 |
So, if we put in here a number.
Let's put 1.
| | 06:13 |
It comes back just fine.
But if we put in 1 as an actual integer,
| | 06:18 |
then it doesn't.
Now this brings up an important point,
| | 06:21 |
that when we submit form values in PHP,
there always going to be strings.
| | 06:25 |
Even if it's a number, it's going to be
submitted as a string, because PHP
| | 06:29 |
doesn't know whether we intended it to be
a 1 as a string or 1 as a number.
| | 06:34 |
So it's going to be a string.
And you'll need to do a conversion to
| | 06:37 |
convert its type if you need it to be
something else.
| | 06:40 |
Let's do inclusion in a set.
Let's take this right here.
| | 06:45 |
Now, let's say that we have a set of
values here.
| | 06:48 |
Let's say that the set is equal to the
array.
| | 06:52 |
And in the array, we have 1 or 2 or 3 or
4.
| | 06:58 |
So it must be in one of these values.
So how do we do that?
| | 07:01 |
Well we know we can use the in array.
You want to check if it's not in the array.
| | 07:07 |
And so for that we use value followed by
the set.
| | 07:10 |
So if value is in the set it will pass.
If it's not then it won't.
| | 07:15 |
Let's try that real quick.
Okay.
| | 07:17 |
It passed.
We now make it 5 and now it fails our validation.
| | 07:22 |
You getting a feel for how this works?
So uniqueness, we really can't do without databases.
| | 07:25 |
So we're going to leave that one for now.
But the basic idea is that when they
| | 07:29 |
submit a value, we then turn right around
and take that value and ask the database.
| | 07:33 |
Hey, database.
Do you have this value?
| | 07:35 |
And if it returns yes it does, well,
then, we erase the validation error.
| | 07:38 |
If it returns no it doesn't, then we
don't.
| | 07:41 |
And then last of all, I want us to look
at format.
| | 07:44 |
Now, for format, I want to teach you a
new function.
| | 07:46 |
It's a little bit of a high-level
function, but it's super-useful.
| | 07:49 |
And it's preg_match.
And what we're doing is we're applying a
| | 07:53 |
regular expression to see if something
matches.
| | 07:55 |
So here's the format.
We provide a regular expression here and
| | 07:58 |
the subject we want to match, and it
returns true whether it matches or not.
| | 08:02 |
Let me just give you a quick example
here.
| | 08:04 |
I'll just paste it in, so here's our
regular expression inside the slanted
| | 08:08 |
lines as a string, and I'm going to match
whether PHP is inside PHP is fun.
| | 08:13 |
And it will either say match was found or
match was not found.
| | 08:16 |
Switch back over here, reload it, a match
was found and if we put in something else
| | 08:22 |
like an x at the end.
Then it comes up and says that a match
| | 08:25 |
was not found.
So that's how it works.
| | 08:27 |
Let's instantly just got back up here and
fix this one just so that passes it's validation.
| | 08:32 |
And I'm just going to make a note here,
uses a database to check uniqueness.
| | 08:39 |
So let's just drop back down here.
And let's finish working on format.
| | 08:42 |
So the way that we're going to do this,
is very similar to what we've been doing.
| | 08:46 |
We'll write an if statement.
But instead of in_array, were going to
| | 08:50 |
use this preg_match.
There we go, I'll drop it in.
| | 08:53 |
But we don't want to check that.
Were going to check it against some value.
| | 08:56 |
Let's say value is equal to
nobody@nowhere.com, and you can write a
| | 09:03 |
complex regular expression here for now.
I'm just going to do, real simple, let's
| | 09:06 |
check to make sure that there is an at
symbol in it, and we're going to use value.
| | 09:10 |
That's all it's going to do, is check to
see whether the at symbol is inside there.
| | 09:14 |
So let's go back here.
Oops, unexpected if.
| | 09:18 |
Where did I get my problem?
I forgot my semicolon.
| | 09:20 |
There we are.
Back, there we go.
| | 09:23 |
Match validation failed.
Why did it come back and tell me that
| | 09:25 |
it's not there?
It should be, if it's not matching.
| | 09:29 |
That was my mistake.
So, not match.
| | 09:31 |
So, it did match, so now if it does not
match, then we want to return validation failed.
| | 09:36 |
Now we don't get our validation error.
Now, preg_match is great because you can
| | 09:41 |
use regular expression which are very
powerful.
| | 09:42 |
I actually have a regular expression
training on lynda.com that you can refer
| | 09:46 |
to that'll tell you more about how to use
regular expressions.
| | 09:49 |
But I think that it's not the fastest,
sometimes using string, string, STR, STR.
| | 09:54 |
And string pos for postion, STR POS, are
going to actually be a little bit faster.
| | 09:59 |
So for example, you can have something
like this, if the string position of the
| | 10:03 |
value is exactly equal to false then the
validation failed.
| | 10:08 |
Now why did I use exactly equal?
Because string position will return 0 if
| | 10:13 |
it finds it at the beginning of the
string.
| | 10:15 |
And so we don't want to have equals false
because 0 would be considered false in
| | 10:19 |
that case.
So if it's exactly equal to false using
| | 10:22 |
that triple equals, then we want to match
it...
| | 10:25 |
And that leads us into a discussion then,
about some of the problems with
| | 10:29 |
validation logic.
Because those kinds of problems can trip
| | 10:33 |
you up and give you false positives or
false negatives.
| | 10:36 |
So, let's look at that in the next movie
and talk about what some of the common
| | 10:38 |
problems are.
| | 10:39 |
| | Collapse this transcript |
| Problems with validation logic| 00:00 |
In this movie we're going to talk about
problems that can occur in your
| | 00:03 |
validation logic.
Now these are problems that happen with
| | 00:05 |
any if statement, but they're critical to
get right when we're working with validations.
| | 00:08 |
Normally in your code, if your if
statement's not right, well, then you'll
| | 00:11 |
see it right there on the page and know,
while you're developing, that you've done
| | 00:14 |
something wrong.
But if our validations fail us, then we
| | 00:17 |
lose control over the data that's coming
into our application.
| | 00:19 |
And we may not even know about it because
we're not there at the time that it happens.
| | 00:24 |
So let's work to understand some of the
common problems.
| | 00:26 |
The last movie we wrote a validation,
that would check to make sure |
|
|