Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll take the concepts we've learned to develop a daily exercise program. We'll use variables and conditionals to show us a different exercise each day.
Starter Code for Daily Exercises
// store each exercise in a string variable
$exercise1 = 'Display "Hello World!"';
$exercise2 = 'Convert Pounds to Kilograms';
$exercise3 = 'Convert Kilograms to Pounds';
$exercise4 = 'Convert Miles to Kilometers';
$exercise5 = 'Convert Kilometers to Miles';
$exercise6 = 'Month long string of the day';
$exercise7 = 'String of the day with levels';
// create a variable containing the day of the week
// use an if statement to test for the day of the week
// display the corresponding exercise string
Documentation
The date() function returns a string formatted according to the given format string.
'N' returns a numeric representation of the day of the week 1 (for Monday) through 7 (for Sunday)
To see all formatting options, visit the PHP Documentation for date().
Additional Practice
- Perform other conversions or mathematical computations.
- Add a skill level to your exercises and display an exercise by skill level as well as day.
- Create a quote of the day or
- Show a greeting based on the time of day.
We're starting the daily exercise program.
0:00
So let's create a new file and
name it exercise.php.
0:02
Again, we need to start the file using
the opening and closing php tags.
0:11
Like we did for unit converter let's add
comments to document the steps needed for
0:17
this program.
0:21
First we need to store each
exercise in a string variable,
0:24
then we'll need to create a variable
containing the day of the week.
0:31
Then we'll use an if statement
0:42
to test for the day of the week.
0:47
And finally we'll display
the corresponding exercise string.
0:54
So the first thing we need to do is
store each exercise in a string.
1:03
So let's create our first string.
1:07
We'll call this $exercise1.
1:09
We're going to set this equal to a string.
1:13
You're probably expecting us to
write a list of physical exercises.
1:16
While we could do that
in the exact same way,
1:20
I thought it would be more fun
to use programming exercises.
1:22
So for exercise one, let's use the first
exercise we performed for this course.
1:27
Display hello world.
1:31
Now let's create six more exercises for
the rest of the week.
1:38
I'm going to paste these in
instead of typing them all out.
1:42
I'll put them in the teacher's notes for
you as well, but
1:45
feel free to use any exercises you'd like.
1:47
Now that we have our exercises ready,
1:51
we need to create a variable
containing the day of the week.
1:53
I'll call this Day.
1:59
This could be a string with a day name or
just an integer for the day number.
2:03
I'm going to go with integer.
2:07
We could set the variable manually and
we will for testing, but
2:10
we want this to be automated based
on the actual day of the week.
2:14
PHP has another function we can use for
this called date.
2:19
Date returns today's date in the format
we specify in the parenthesis.
2:25
Passing in the upper case n within quotes
2:30
will give us the numeric
representation of the day of the week.
2:34
One for Monday through seven for Sunday.
2:39
Let's var_dump day and
run our script just to see.
2:43
As you can see it gives us the numeric
representation of today's day of the week.
2:59
Today is Wednesday.
3:05
Great.
3:07
Now we can use that in an if statement
to check the day of the week and
3:07
return a corresponding exercise.
3:11
If our day equals one.
3:17
If this is Monday.
3:23
Then we want to echo $exercise1.
3:25
Let's duplicate this for
each day of the week
3:32
And then let's replace these numbers.
3:41
Okay, let's try this out.
4:04
It only displays the exercise that
corresponds to that day of the week.
4:08
Convert kilograms to pounds
we notice is exercise 3.
4:13
This is the third day of the week.
4:17
This works like we want, but
by creating separate if statements,
4:20
we force PHP to evaluate each statement.
4:24
If the first statement is true, we know
that the other statements must be false.
4:28
So let's combine these
statements using the elseif.
4:33
Elseif day two,
4:35
elseif day three, day four, day five.
4:42
Day 6 and day 7.
4:51
Let's remove this far dump.
4:57
And run the script again.
5:02
Great now we see just the 1 exercise.
5:05
Let's change day just so
we can see if this is actually working.
5:09
We'll change day manually to equal six.
5:15
And now let's run this code again.
5:21
We now see exercise six.
5:25
Month long string of the day.
5:28
Great job working with strings and
creating your second program.
5:31
You've now used all the basic scalar
type variables that PHP offers,
5:34
boolean, integer, float, and string.
5:39
You've also used many different
operators to store, retrieve, and
5:43
manipulate the values in those variables.
5:47
You've also learned conditionals to tell
the script how you want to use that data
5:50
and control the flow of your script.
5:55
Before you move on, try practicing some
of the skills you've been learning.
5:58
Perform other conversions or
mathematical computations.
6:01
Add a skill level to your exercises and
6:05
display an exercise by
skill level as well as day.
6:07
Create a quote of the day or
show a greeting based on the time of day.
6:11
In the final section of this course, we'll
show how to work with PHP on a website so
6:14
that you can start sharing your
newly learned skills with the world.
6:20
You need to sign up for Treehouse in order to download course files.
Sign up