Don't Repeat Yourself3:35 with Alena Holligan
The DRY Principle is one of the tenets or key concepts of programing. DRY (D-R-Y) stands for Don't Repeat Yourself. If you see yourself repeating code in the same place, for example, writing the same string over and over again, it's a good sign you can improve your programming using DRY programming techniques.
Changing Video Speed
In the bottom right of the video you can change the speed: speedometer icon, when you scroll over it has a slider like the volume.
Now that we know how to process a PHP file on the web, 0:00 let's start putting PHP to good use. 0:03 Remember, variables are a way to store data. 0:06 One reason to store data in a variable 0:08 is to use that data in multiple places in your code. 0:11 This will also allow us to change the data in one place while 0:14 affecting every line of code in which that data is used. 0:18 This is one of the tenets, or key concepts, of programming. 0:23 It's called the dry principle. 0:26 Dry, D-R-Y, stands for don't repeat yourself. 0:28 If you see yourself repeating code, for example writing the same string over and 0:33 over again, It's a good sign that you can improve your programming 0:38 by using DRY programming techniques. 0:41 Let's go back to Workspaces and take a look at how to use the variables and 0:43 the DRY programming principles in the name we've been using for our page. 0:49 In the last video, I set up code block and echoed my name. 0:55 Let's see how we can make this program more flexible using a variable. 0:59 Instead of echoing out this string, let's change this to a variable display name. 1:03 Before we can use the display name variable we have to give it a value. 1:13 Let's set the value for display name at the top of our file so 1:17 that we can use this anywhere. 1:20 Before the opening HTML tags, and even before this doc type. 1:22 let's add a new PHP block. 1:27 Then we can set our variable display name equal to my name. 1:33 We can add as many code blocks as we want throughout our page. 1:39 Now we can save this file and refresh the browser. 1:43 We see that nothing has changed but that's good it's working just as before. 1:47 Now let me show you why variables are so useful. 1:51 Let's use this display name variable in a few different places. 1:55 Let's copy this whole code block. 2:00 And up in the title, instead of saying PHP Basics at Treehouse, 2:04 let's change this to PHP Basics by Alena Holligan. 2:08 Using our display name. 2:12 Let's add this in one more place, the copyright in the footer. 2:15 Instead of saying Copyright 2016 Treehouse Island Inc, 2:19 let's say Copyright 2016 Alina Holligan using our display name variable. 2:23 We'll replace Treehouse Island Inc with our display name variable. 2:29 Now let's save the file and refresh the browser. 2:34 Now I have Alina Holligan in three places, the title, the left bar, and the footer. 2:37 That makes this a little more useful. 2:43 But now what happens if I want to change the way my name is displayed? 2:45 Let's go back to work spaces and the first code block at the very top. 2:49 Instead of saying Alena Holligan let's change this to Alena A Holligan. 2:53 We'll save the file and refresh the browser. 2:59 Now everywhere that uses the display name variable is updated to this new value. 3:02 You're doing a fantastic job 3:06 learning foundational programming concepts and building your first PHP driven page. 3:10 By storing and retrieving information in a variable, 3:15 we are able to quickly update our page when requirements change. 3:18 Wouldn't it be nice to have changes update without us having to change anything? 3:23 Okay so this isn't always possible but 3:28 there are some things that we can set up and let PHP do all the work. 3:30
You need to sign up for Treehouse in order to download course files.Sign up