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