PHP for Beginners | Coding Academy | Skillshare
Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
82 Lessons (4h 42m) View My Notes
    • 1. Main course intro

      1:40
    • 2. Preparing your PHP development editor

      5:23
    • 3. Hello, World!

      2:41
    • 4. How to use the correct syntax in PHP

      3:26
    • 5. How to assign values to variables in PHP

      2:31
    • 6. What PHP data types are there?

      2:38
    • 7. Echo and print - how to view PHP output

      2:33
    • 8. How to add comments to PHP code

      2:41
    • 9. Getting organised before we start the challenges

      2:40
    • 10. How to add and subtract numbers in PHP

      2:46
    • 11. How to multiply and divide numbers in PHP

      2:46
    • 12. How to use the PHP modulus operator

      2:05
    • 13. How to find the power of a number in PHP

      1:37
    • 14. Challenge 1: Back to school - TASK

      2:07
    • 15. Challenge 1: Back to school - SOLUTION

      2:30
    • 16. How to join words and sentences using concatenation in PHP

      4:28
    • 17. Challenge 2: What's in a name - SOLUTION

      3:13
    • 18. More concatenation

      1:45
    • 19. What are the comparison operators in PHP

      1:50
    • 20. What are the logical operators in PHP

      0:54
    • 21. Counting: incrementing and decrementing

      2:16
    • 22. How to use an IF statement in PHP

      4:54
    • 23. How to use an IF ELSE statement in PHP

      2:39
    • 24. How to use ELSEIF statements in PHP

      5:37
    • 25. How to use FOR loops in PHP

      3:22
    • 26. How to use WHILE loops in PHP

      2:20
    • 27. Challenge 3: Multiples - SOLUTION

      2:04
    • 28. Challenge 4: Let's Play Fizz Buzz - SOLUTION

      4:13
    • 29. How to use the PHP online reference manual

      2:55
    • 30. How to generate random numbers in PHP

      2:34
    • 31. Challenge 5: Heads I win - SOLUTION

      4:24
    • 32. Challenge 6: Who ate all the pi? - SOLUTION

      3:28
    • 33. How to use the date function in PHP

      6:19
    • 34. Challenge 7: Carper Diem - SOLUTION

      4:11
    • 35. Time functions

      6:31
    • 36. Challenge 8: Santa Claus is coming to town - SOLUTION

      2:48
    • 37. How to create a basic HTML page structure

      3:44
    • 38. How to create a basic HTML form

      6:36
    • 39. How to use form data in PHP

      6:12
    • 40. Challenge 9: Temperature conversion - SOLUTION

      8:05
    • 41. How to find the length of a word or sentence

      2:11
    • 42. How to count the number of words in a string

      1:12
    • 43. Challenge 10: How long is a piece of string - SOLUTION

      5:11
    • 44. How to replace characters in a string

      2:51
    • 45. How to randomise a string in PHP

      1:14
    • 46. Challenge 11: Mission Impossible - SOLUTION

      1:47
    • 47. Double versus single quotes in PHP

      1:54
    • 48. How to escape strings in PHP

      2:09
    • 49. Challenge 12: The Great Escape - SOLUTION

      1:54
    • 50. What is an array?

      3:43
    • 51. Looping through arrays

      3:27
    • 52. How to split a string and place into an array

      2:18
    • 53. Challenge 13: ISBN checker - SOLUTION

      9:43
    • 54. Challenge 14: Mind Your Language - SOLUTION

      3:09
    • 55. How to sort data in arrays in PHP

      2:42
    • 56. How to explode and implode strings

      3:00
    • 57. How to add elements to an existing array

      2:42
    • 58. Challenge 15: Around the world in 80 days - SOLUTION

      3:49
    • 59. Removing elements from the beginning / end of an array

      2:55
    • 60. How to use array_slice() to split an array

      3:50
    • 61. Challenge 16: It's getting hot in here - SOLUTION

      5:42
    • 62. How to select a random element from an array

      2:15
    • 63. Challenge 17: Pick a card, any card - SOLUTION

      3:35
    • 64. Challenge 18: Let's go shopping - SOLUTION

      11:21
    • 65. How to understand the structure of an associative array

      2:16
    • 66. How to loop through an associative array

      2:48
    • 67. Challenge 19: Capital cities of the World - SOLUTIONS

      2:54
    • 68. How to create a function in PHP

      1:45
    • 69. How to use variables in PHP functions

      3:03
    • 70. How to pass arguments to a PHP function

      3:02
    • 71. How to return a value from a PHP function

      4:09
    • 72. How to return multiple values from a PHP function

      3:52
    • 73. How to create a cookie

      3:43
    • 74. How to read a cookie

      2:45
    • 75. How to update a cookie

      1:50
    • 76. How to delete a cookie

      2:07
    • 77. Challenge 20: All about me - SOLUTION

      7:27
    • 78. How to create a session in PHP

      3:29
    • 79. How to read a session in PHP

      3:23
    • 80. How to update a session in PHP

      2:40
    • 81. How to delete a session in PHP

      3:42
    • 82. Challenge 21: Hit Me Baby One More Time - SOLUTION

      4:48

About This Class

Learn PHP for Beginners. Every line of code is explained as you are guided from complete beginner to confident coder. The unique challenges have been carefully crafted to stimulate your logical approach and deepen your understanding. 

Transcripts

1. Main course intro: welcome to learn PHP. Unlock your potential via examples and many tasks. My name is Patrick Morrow on I Am a national award winning teacher with over 25 years teaching experience on I Want to use that experience to help you learn PHP via riel World examples. Now I don't about you, but I've often looked at other online tutorials on family use of food and bar. Very confusing, especially for beginners. Now you've probably seen the sort of thing that I'm talking about. Well, you won't find any abstract examples in this course. Each tutorial has been carefully crafted to guide you towards solving tasks that make sense . You will learn basic concepts through examples and will then have an opportunity to tackle unassociated task. Always full tutorial support and full solutions. If you are a beginner to the world of programming or even if you have some experience on which to enhance your skills, then this is the course for you. I really do encourage you to review the free video previews that I have enabled throughout the course. Andi, if you like what you see, join me on my other students and unlock your potential 2. Preparing your PHP development editor: in this lesson, We're going to set up the development environment. Now, if you are completely new to PHP a do recommend that you follow along with me. However, if you tried PHP before, you probably already have a development environment set up. In which case she points. Skip the rest of this video on Just follow along using your own editor. I've been doing this for a long time. I've used probably virtually every editor out their views. PHP Storm Dreamweaver Basic text Editors, Ma'am Pop Wamp, Zampa you name it. I've probably tried it. Now, the reason I chose code anywhere is when I was in classrooms using different code editors with students there would always be some sort of issue on setting software upon your own can be quite daunting. And if you completely new to these code editors, errors can creep in now with code anywhere. If you are having issues accurately, log in and fix things for you. So I do recommend that if you completely new probably follow along on see what I'm doing here. Now I will point out that code anywhere is not free. It's free for seven days, but then you have to pay a small monthly fee. I think it's currently run about $3. Personally, I think it's worth it. But again, as I say, you do not have to use code anywhere. You can use one of the free tools out there. An example is a very popular one with PC users. Ma'am is a popular one with Mac users, and there are others for different operating systems. Now, if you choose to follow along with me, you need to go to cold anywhere dot com on you. Get to a screen similar to this. Obviously, you'll need to click on that sign up. You put your details in your email address on Click on Create to create an account. Now they will send you an email, which you will need to confirm before you go to the next step. So make sure you do that now. The next step, once you're logged in, is you'll see a screen like this. Now this is where we set up what they call a container. It's like having a server on an online system. There's a couple things we need to do. First thing is to put in a name you can call it anything you wish. You can call it test. You could call it pitch P for beginners. It could be your name. Anything you wish. I'll just pop in test for the time being. Now, down here, we have all these different stacks now, these air different operating systems we could use. You can scroll through and you can see what we've got here. But we're looking for a P hedge. P on. Currently, there are two listed. Now they are different, and the one you want is Thea Bun to one of into 16.0 for currently. Now, that version might be slightly different when you come to set years up. So you just select that one and then you'll click on create. Now, once you played some, create a little pop, it will appear saying that it's setting up the server and that you can actually close that pop up. No, I'm not gonna go ahead and crypt. My cervix already done that. So I just close this Stan on once uses set up. This is what you will see. You'll have to tabs One is just the back end console tablets. We can close that one on the 2nd 1 is just information about your container. I can close that one as well. Now you'll notice on the left hand panel. I've got lots of containers set up. That doesn't matter. You'll just have your one container. If you called it test or PHP for beginners or your name, it doesn't matter. I'm just gonna go ahead and open by container and you can see I've got all the different sections off course now, see, years will be blank and you will be creating these folders on within the folders. You will have various files such as this. Now, we're gonna go through that as we go through the course Now, one thing you will need to do is to set up how you view the files on your server. Now there's a couple ways to do this. One is just to click on this little play icon on what it will do. Will open a new tab and it'll go to the root off the server. The second way is to right click on your folder and click on this info. Now, that's the one that appeared originally when you set up your server on you See down here it says to access your Web application. We click on this link. So if I click on that link, it opens up the tab and there are the list of all my folders. Now, you might want to bookmark that to get too quickly. Let's go back to code anywhere. And I'll just close that now because you're probably on a free account for seven days. What you might find is when you log out of your account, this server will actually stop. So you do need to start it when you log in. So again, you right, click on you, click on start or restart. You could even turn it off if you wish. Now, hopefully that was fairly straightforward to set up off that server container. But if you do have any issues, please just drop me a little message. Send me a private message. I will log in. I will set up the server for you and get you up and running as quickly as possible. 3. Hello, World!: It's somewhat of a tradition in coding circles that when you learn a new programming language for the first time, the first grip that you learn is one called hello world and who might break with tradition . So let's go ahead on will create a hello world script. So I head on over here to my server on one of the first things you may wish to do is to create a folder to store some of your files. To do that, you right, click on your server box and you click on Create Folder. Now, as you can see, I've already credits and folders here, and I've got one for each section off the course. Now, we're working in section two and I want to create a failed to place in here. So I right click on section two and create a file. Now, this file is going to be called hello world. So hello. They use a capital w o r l d don't p h p. So for the name of my fail have used something called Camel case. So the first word is lower case on the second word begins with a capital letter. I'll go ahead and I will create that fail over here, we could see the file is being created, so I'll click on it. And here I am, in my men window here. And this is why I can begin to write the PHP code now, explain any step of the code in the subsequent lessons. But for the time being, let's just go ahead. Will type in some code and we'll see what happens. So we'll start with a PHP code block. I then echo Hello, world. I will save it by pressing Commander s or Control s on a PC. I'll go to my browser. I don't think of my boot, Mark. There's my boot, Mark. I'm going to go into section two. There's Hello world. I'm going to click on it and there's they're put to the screen. Hello world Now don't worry. If you don't understand this code yet, we will be going through in finer detail in subsequent lessons. But hopefully you managed to get the hello world output toothy browser screen 4. How to use the correct syntax in PHP: in the last lesson, we looked this code here to produce the Hello World message to the browser. Now, as I was typing this code, I never explained what any of it meant. So what I hope to do in this lesson is to break it down a little bit on to go through what each line means. So let's start with Lane one in line one. We are starting a PHP code block. We start with the left angle brackets, the question mark and then PHP. Now this is referred to as the opening PHP code block or tag. And we do have a closing tag. And those of you who have done any HTML will be used to opening and closing tags. Well, in PHP, the closing tag looks like this. It's a question mark and then a right angle bracket. So this here becomes a PHP code block. Now, unlike html, we don't actually need this closing tag here. If we're only using PHP coat now on line three, we have this eco command and that will output this string here to the browser. And if you notice we finish the line with a semi column now each line of PHP code must be terminated with a semi column. Now, I've just mentioned that we don't actually need a PHP closing tag this one and that's if we're only using PHP code within this fail. If I actually go on to write somehow html code and I'll do that now, I'll have ah hasty Mel tag. I'll have a body tag. I'll have a paragraph tag. I'll just type in here. Hello world. So if I save this and go to my browser and refresh, you can see we get to Hello worlds. This one is the PHP code on this one is the head html code. Now, if I remove this closing tag here, you can see immediately we get this change of color. If I say that, go to my brother and refresh, you can see we get an error and that's because we must have that closing PHP tag if we're using anything other than pure PHP code. So let me get rid of all of this again on Just go through this, that anything we do in the 1st 3 lessons will be using pure PHP code. We must start with this PHP opening coat block here and every line of PHP we have to terminate with a semi column 5. How to assign values to variables in PHP: Hi, everyone on Welcome to the next video in the series. I hope you had a good chance to look through the documentation on variables on that you attempted to quiz on. Did well on that. In this lesson, I'm going to follow on from that and show you how to assign values to variables. So let's get started over here. I've got a file called variables dot PHP and I've started with my PHP conflict there. My first variable is going to be called a and I'm going to assign the number two to A and finish with my semi colon. I could have a second. Let's call it be like a vehicle to three semi colon. Have another one called first name and we'll put the equal to Peter. You see, here I've used Camel case. I have mentioned that before where we start the first word in lower case on each subsequent word has a capital letter. Now, the 1st 2 variables, these were numbers. In fact, these are integers on this variable here is signed this string value I could have another one. Let's call it number on this one is 12.345 in this case, I have a decimal number. I could also have one. Let's say Call it debt. Put that equal to 2005 12 12. Now we will come all to look at debts and debt functions later in the course on, we are fortunate that PHP has some really powerful debt functions built in. So hopefully you can see how easy it is to assign values to variables. And just remember that we start a variable with the dollar sign and then we use a letter or underscore as the first character, and it is fairly coming to use camel kiss. 6. What PHP data types are there?: in the last lesson. We looked at assigning values to variables and I mentioned that there were different variable types. If you hadn't over to the PHP dot net website on here, I just in a quick search for variable tapes on its come up with a list here. Now we won't be using all of these in this course, but certainly the 1st 5 we will on the 1st 1 here is billions. Hannah Boolean is a true or false value. So in here it's got rid of am be here. If I set a to be one, that's the same as saying it is true it turned on, and B, if that was zero, that would be false. Now the other way I can actually say that is a is true and be let's false. That's the same way of doing the same thing the next time we had listed Waas. Imagers and interviews are just whole numbers. So in this case, length is an integer a is an integer and create another interview here. Let's call it D. I will pay for the equal to negative five so we can have negative values as well for integers we have floating point numbers on floating point numbers are decimals, So e 12.24 That's a floating point number. They're sometimes called devils are really numbers Do another 1/2 that could be negative. 1.4321 etcetera. Next type. Here are strings on these just characters text Here we have a string here. First name is Peter Onda. We could put Pizer is a man so that there is a string. The next one The list is raise and raise can hold multiple values. Now I'm not going to go into those at the moment because we do have that literal in the course, and it's a little bit more complex, So these ones, we're gonna be using billions into Jews, floating points with decimals and strings. 7. Echo and print - how to view PHP output: the last couple of lessons. We've looked at variable types on assigning values to variables, and in this lesson, I just want to very briefly look at how we actually output those variable values. So if you call in the hello world script, we use the term echo to actually output to the screen. Hello, world. And we can do that with our variables. Let me assign a variable name here called First Name. I don't put that equal to Peter. Now. If I want to echo back an echo first name, I'll save that. I go to my preview. I'm still on the hello world here, so I'll go back one and refresh on. Look for variables dot PHP. There it is. Click on that and then we can see Peter. Let's do another one. Let's have a equal to one. I don't echo a so on the screen. We should see a number one on. There we go. I want to put a break in between those. I need to echoed Elaine Brick, and we do that by saying echo. Then we have a break Tech that's hitch TML for a lane brick. So left angle bracket B our right angle bracket. And if I seven refresh there we go. Got Peter and one. I'm just copy that. I'm a paste it after this. A going to put it on in here. Be I don't equal to true. I'm going to echo be. And the question is, what do you think will appear when I do the preview? Three fresh, Never look. And there we go. We get one. Another word. True, but one, if you're called true, is equivalent to one. Now the other way we can print to the screen is well, actually using the term print So I'll get rid of Eco be and I'll put in print and I'll save gonna be fresh And there you go, we still get the number one. 8. How to add comments to PHP code: okay, so well done So far, we're just covering the basics of the moment, and we're about to move on to the next section on introduce some coding challenges. Now what I want to do here is introduce you to PHP. Comments and comments can be extremely useful, especially if you're leaving a day or so between coding or maybe a week or maybe even a year. It's nice to come back and say, Well, why don't actually do that? So to add a comment is really straightforward. Only do is put to forward slashes and then add your comments. Now the comment itself has no effect on the code. And if I go to the preview on refresh, you can see it's had no effect whatsoever. I can add comments to the end of lanes, but I can't add them to the beginning of lanes. So if I'd go here in other comments, it's what it does. You can see this has actually affected this whole line here. Now. Sometimes you actually want to do that. You want to be test in different parts of code. So you grabbed that and get rid of it. Let's say I'm happy that have got Echo A here, but I actually want to remove this from printing to the screen. I want to keep it for future reference, but a die if you wanted to appear, it's what I can do here is I can put a forward slash and a star and watch what happens as soon as I put the story in. The rest of the code turns gray. So all of this has Bean commented, hurt. Now what if I want to comment at just this section here? So let me get rid of this coming here. So let's say I want to get rid of this. Well, I can come into by adding a forward slash and the star you can see it's committed at these lines as well, so I want to end it here and to end it. We just do the reverse we put a star on, then a four slash. So this code here is not being commented at, and it won't actually appear on the screen. So if I save the hat and refresh that, you can see that that has actually disappeared. So comments are extremely useful as you get on to the more advanced challenges, it will be a good idea to have comments to your code. Just give you an idea of what you did on why? 9. Getting organised before we start the challenges: Hi, everyone on Welcome to Section three now this is where we start to have a little bit of fun because we start the coding challenges. Now. That's nothing to worry about because I do pervade full solutions. And if you check over in the right hand side, you'll find some downloadable resources as well. Now, before we dive into those, I just want to mention about being organized. No, I see students who are having difficulty with the code, and that's not because they can't do it is because they don't organize their files and folders. So I just want to stress organize yourself from day one, and life will be so much easier throughout this course. So here I am in my code anywhere box and I have two files here from Section two, So I want to drop those into a folder. So if I create folder I right, click on the box and create a folder, I don't call this Section two. Create that. That I could take those two fails and just drag and drop them into that folder whole. Seems OK so far. I want to go to the browser to have a look at this. Let me just refresh and show you They're section to all Seems fine when I click on it and then check out the your L. I have this percent to zero now that represents the space between section on day two. That's not good. That's what I really need to do when I'm naming my folders. Just rename this one again. Right? Click and rename. I like to use lower case letters. I put a dash in here. You can also use an underscore So we go every now and as section dash to I for go back and refresh Let me just go back and refresh their in his section dash to click on it And now check out the Ural Much cleaner. So that's what I want you to name your folders. Now we're moving on to section three. So I'm gonna go ahead and critical the folder. Call it Section three, click. It creates on there. So now we're ready to move on to Section three on to start some of these coding challenges 10. How to add and subtract numbers in PHP: in this lesson, we're going to learn how to add on subtract numbers, so fairly straightforward stuff. So in my section three folder, I'm going to right click on, create a file. I'll call this one position, took PHP and create that fail open the file and start my PHP code block. Now I'm going to declare a couple of variables, the 1st 1 being a and that's what people to five and the 2nd 1 Let's have be how hopeful are equal to 10. And I want to add these two numbers together. I don't want to put that into an answer variable. So I have this one called Answer. So answer is gonna be the any value plus the the value no need to do is to echo the answer to the screen. So it's gonna be eco answer. I will say that Good. My president still quit. Refresh on going to Section three. There it is. Addition. Click on there and then we have the answer off. 15. So fairly straightforward, similar thing for subtraction. Credit. You file for subtraction and we'll open that fail again pitch PICO block. This time we'll have number one that's equal to 20. Have number two, apply equal to 35. Now, put my answer. I'll do number two. I find number one. Lesson number one minus number two. So, me, Cohen and then just echo at that answer. Okay, say that I don't need to have in my edition here something to go back. Every fresh there subtractions or click on subtraction. And they're getting negative. 15 which is correct. So 35 20 minus 35 is negative. 15. So, addition, subtraction. Really straightforward. Now, the next video is gonna take care off multiplication. And then we got the one on division. Let's see if you can actually have a little bit of challenge here and see if you can work out. I had to do them before we start. 11. How to multiply and divide numbers in PHP: in this lesson. We look at multiplication on division and I did say a little bit of a challenge to have a look at this before you watch the video. So hopefully have on. Maybe you actually got somewhere with it for those that didn't. Here we go. It's the first thing is Kris and you file. I'll call this one multiplication. The PHP. I'll open the Fail instead of my PHP code block. I'll have into the number was Clinton, but one value of four. Number two. So you hate and then my answer. So my answer will be number one times. Her times is an asterisk. So my keyboard it shift on the number eight. So number one times and number two no need to do is to echo at that answer. We'll see if that my browser let's get back and refresh those multiplication. Click on it and there we go. Eight fours are 32 on that for division. So under the file course one division open the Fail and start the PHP code block. I will have number one this time. Avoids 100 number two up, 25 answer. So be number one divided by now. The division is thief forward slash number one. I got the one. Number one divided by number two. So we should get answer for. So save that I go at first. So echo, answer and save. Let's go back and refresh those division Click on division having an answer off four. So again, Addition, Subtraction, multiplication and division all fairly easy. So you're almost ready to accept your first challenge will just cover more things before we move on. 12. How to use the PHP modulus operator: in the last lesson. We looked at the division off numbers and I had a number here of 100. Divided by 25 Gives me a nice hole number. Answer off. Four. Well, what would happen if we didn't have a whole number? Answer for exemplified of IDA 100 by 12. I'll save that. Go back and look at division again. I'm gonna answer of 8.3 recurring. So the three carry on. Now, what have been like to do in some of these operators is trying to find out what the remainder is. So I know that 12 goes into 108 times 8 12 there, 96. Which leaves the remainder of four on this actually quite a powerful operation to use when we're doing PHP. So let me show you how it actually works. I've gone ahead and created a file called Module ISS that can open it. There we go, Onda. I've actually inserted two numbers already because you should be okay with me doing that. Now the answer that's gonna be equal to number one. Ondas, I said I want to find out what the module iss is so number one, the modular symbol is the percent. Sane might seem a little strange to use that, but it's number one module ISS number two. So what we're actually doing here we're taking 20 dividing by three on finding what the remainder is. If there is indeed a remainder. Well, 20 divided by three is six, which leaves a remainder off to. So if I echo at the answer, save that. Let's go back and click on module ISS. There we go. We get a remainder off to. Now you'll find out why we actually learn this on how it's actually gonna helpers in the challenges later on. 13. How to find the power of a number in PHP: in this lesson. I want to look at powers or experience in the words. I want to take a number and raise it to another number. Power. In this case, I got my best number as to on my power or excellent as three. So I'm doing two to the power of three, which is 82 times, two times two is eight. How do I see in the PHP? We'll start with my answer equals I want to take the best and raise it to a power. Now PHP actually has an inbuilt function to do this now. I wasn't going to cover functions at this point, but I thought, actually slightly quite nicely in since we were doing other operators. Such addition, subtraction, etcetera. So the function is P. O. W. And it says, I took that in. I get this little hint here and it says to do the power I need to take the best, have a comma and then I want the exponents. Let me do that. So I have my face number comer. I'm resonant to my power. So in my case, I'm doing to to the part three. How then echo at the answer. Say that. Gonna my preview exponents and have a look on there we go to to the power three is DEET. 14. Challenge 1: Back to school - TASK: Hi, everyone, and welcome to your first challenge. Now, in this challenge, I'm taking you all back to school. Now, I realize so even might still be in school And some of you, probably a long time since you've left school. So you might have forgotten one or two things not to panic. I'll go through here. The idea behind this challenge is to conflict the areas off different shapes. Now I'm gonna start you off with area off a rectangle so creative fail here, Harry, Dash right, tangled up. PHP open my code block on. What I want to do here is twic county at the area off rectangle. Now, the area of a rectangle, if you recall, is the length times the with so need a very little for length. Don't put mine equal to 10 my width. I'll put that equal to fire. Keep it simple. Then my answer is called rectangle on. That will be the length multiplied by the width. And I could just echo at that results. Say that and a quick preview area of rectangle on I get 50. So your challenge is to come up with the results for the area of a triangle, and the extension task is the area off a circle that's a little bit harder now. If you check over on the right hand side, you'll find some downloadable files over there or have explained everything in a little bit . More detail had also given you a couple of hints. So good luck with this one. If you're not sure, you get stuck poster questioning the discussion forum and then check out the solution below . And, of course, as always, all of the code is available for you to download. 15. Challenge 1: Back to school - SOLUTION: okay, so hopefully you didn't have too many problems with those. We've already been through the rectangle, so let's have a look at the triangle and see what you did. Now you approach might have been slightly different. That's fine as long as you got the same area. Answer Now for this one, the best was given as 12. The height is given his three and to work at the air of the triangle. It's the best times, the height divided by two, and I just echo at the area for these circle. It's a little bit different. So for this one, we've got a radius of five, and I suggested you had a value of pie off 3.14 Now the circle area is pi r squared into the words pi times the radius, times the radius and then echo at the answer. Now there is another way to do that. And if you recall we had the exponent or power function, so you could have used that as well. I'll just very quickly show you that so the circle would have been the phone. It's still have bean pie, and then we'd have to move, played by that by the power and in the brackets will have the radius comer two. So take the radius and raise it to the power off to the next one is the trapeze IAM and I said that one is an extension task and we had saved a being 10 side the being ate the height being for and then the calculation. Well, the calculation is, you add side A and B together first, and that's why they're in brackets. Then you multiply by the heights on divide by two, and then you just spit out the result and the answer for this one. Let's go across to here with trip easy. Um, and three is 36. Hopefully got that for the triangle. You should've got 18. And for the circle, you should have got 78.5. So hopefully got those. If you have any questions again posted over in the forums on, I'll be happy to answer them for you. 16. How to join words and sentences using concatenation in PHP: Hi, everyone. And welcome back to Section four. Now, in this section we start to Dover a little bit deeper into PHP. Now, one of things I want to look at is something called concomitant Nation on. That's the process of joining two or more values together. What you may have noticed in the previous section when we were doing the exercise on calculating areas of ships is that the final output was just a number and for an end, use of this really wouldn't make much sense. So I need to do is actually say something like in this case, the area of the rectangle is on, then have the value. Now what I've done here is have created section on its or any folder for section for and in here. I've got a file cold concatenation dot p h p. Inside this file, I have two variables. First in last representing first name, last name on. What I want to do is just to connect those together to get that put off John Smith. I only know I could do something like Echo. I could say John and I could echo Smith. I could do something like that I could put them both in here. So just say, John Smith also, the whole point of this is to use the variables and to learn about concatenation. So what I'll do here is all say echo first, and I want to join on or concussed in it on the last name. And to do that, we use a period or dot So first and last, if I say that my browser and preview that, what do you think might happen? That's a look. We get John Smith, but if you notice there's no space in between those names, so I need to come, captain it on in the middle. Here is a space. So to do that look put into the period in and in here, we'll add the space. So first name dot in the space and then the last name. If I say that, go and refresh. There we get John Smith. If I go to the area off a rectangle script, I'm just going to copy this section here, going to my concatenation page. I don't just take you in a break on pace that in If I say that preview on DRI fresh, I frankly hadn't saved etc. Yet Save now refresh. There we go. I get John Smith and 50 now when you read a it looks like John Smith might be 50 years old , so it doesn't really make a lot of sense for me looking at the effort. So what I need to do in here is to echo something like the area of the rectangle is and then rectangle that's quite easy to achieve. Um o in after quotes. And then I'm joining on rectangle and inside these quotes the area called the rectangle his . Then I will include a space and then I'm including the actual value itself. Still say that Henry fresh on the area of the rectangle is 50 Well, 50. What could be sent to me? Two squared and she's squared. I'll just put units squared. So what one in here is the area of the rectangle is than the number and then units squared . So after this rectangle I need to come captain on and space and units squared. Say that go back in the fresh and then the girl. The area of the rectangle is 50 units squared 17. Challenge 2: What's in a name - SOLUTION: Well, hopefully this challenge didn't pose too much of an issue. It's a fairly easy one. We have four variables and we have to form the following sentence. John Henry Green is 42 years old, including the full stop. Okay, let's head on over to code. Anywhere. As you can see, I've already created file called. What's in an m dot PHP? I've added my four variables. Everything missing here is theseventies Colon. On that lane on we need to do is to actually to echo at John. Henry Green is 42 years old. Okay, so let's have a look at that 22 echo. So I need to echo the first name. So first name that only two concussed innate on the middle name. But between the first name and middle name, I will need a space. So was popular space and can cath me on the middle name. I don't need another space, and then we'll need the last him. So John Henry Green A little deal. Just test that just to show you this working Teoh saving and what's the name when we get John Henry Green so good, so far. So I could actually go into the next lane and eco. It is 42 years old. We'll just carry on here just to show you. So hey, to concussion it on some saying, John Henry Green. Now the space is and then another space and they would go outside that and can Kathleen on the age. Then we'll contestant on space, years old, Full stop on finish off with the semi Cohen. So John Henry Smith, his 42 years old saving that. Okay, still saving. Then we go on me fresh and John Henry Green is 42 years old Now, if you take things steady, it could be fairly easy with all these periods and quotes and one thing another. It can be a little bit confusing when you do this for the first time, so a little bit of practice is really useful. How to practice when I recommend you do is go by two. Section three have look all of these areas and changer. The answer. Where you just heard the unit on actually put in the area of the circle is and then actually put that value in, and then units squared. They'll be good practice for you to dealing with those periods and spaces and quotes, etcetera 18. More concatenation: now everybody looked at concatenation, which is the process off joining to orm or values together on. I just wanted to point out that we can do the same thing with sentences. So here I have a sentence. The quick brown fox etcetera. Quite a long sentence. And if this was some code, perhaps a database command, then it could be quite difficult to analyse. Earned a break down. So what we can do? We can actually take parts of this sentence Andi concoct in it. Those parts together and here have done that's we've got sentenced to the quick brown fox with a space and then I'm joining on Jumped over the lazy dog. And the way I do that is with dot equals. So this simply means take the original sentence and add on this next part and keep doing that all the way through this sentence. Now, just to show you that those two things are equal, I've actually echoed at the results here. So that's saved. Let's head on over. We'll go back and refresh Section four Concatenation two. There it is. And there we go. The quick brown fox jumped over a lazy dog on that's being repeated twice. So this sentence here is exactly the same thing as doing this. Another said This is something to bear in mind for later in the course, when we come on to some other more challenging commands. 19. What are the comparison operators in PHP: in this lesson, I just very briefly want to touch on comparison operators. So here I am on the PHP dot net website. I've done a quick search for comparison operators on this is the first page to appear. We have a list here off the various comparison operators that we will be using these in the next section. Just go through a few of these for you don't A is equal to Door B. This is a test to see if this value is equal to this value. If I go back to code anywhere and just have a look at this sentence here, so here the variable sentence I'm actually assigning this sentence. Are this string to the variable sentence? We do it with a single equals so the single equals is an assignment operator. When we come to the double equals, this is a test to see if one value is equal to the other. Now a lot of students mix these up when the first learning PHP. So it's something to be aware off. I said, I just want you to be aware of these things. Have a look on PHP dot nets, some of them should be fairly familiar to you. If not, don't panic. We're gonna be using a lot of these in the next section. 20. What are the logical operators in PHP: just following on from Thekla Paris a non operators. We have the logical operators and again among PHP dot net of search for logical operators and commit with list here. Now again, this video is more or less just about pointing you in the right direction for further research. If you want to do it, the two operators will be using will be these two here Andi and or Onda abusing those in the next section so and is represented by this and or is represented by this. So the devil pipe again. As I mentioned, things will become a lot clearer when we move on to the next section. 21. Counting: incrementing and decrementing: one thing I'd like to touch on very briefly is how to count using PHP. And there are various ways we can actually achieve that. Now here I have Ah, variable. I have said it equal to zero, and I'm just going to echo at that value. We can see here on the preview. It's echoing at zero. Know what I'd like to do is to increase the value of I buy one on one of the ways we can do that is to say that the new value of I is equal to the old value of I plus one and then we can actually echo. At that result, I'll save and preview and then we have the original value of I and then the new value of I . Now there is another way we can achieve that, and we can say that the new value of I plus equals one. So we do the same thing as this. This is a shorthand way of achieving the same result, and again I'm going at the results. I'll save that go along on refresh and there we have 01 hand to now There is one more way can achieve the same results on its By doing the following, we simply have I plus plus, So we don't exactly the same thing. We're taking the value of eye on increasing it by one, huh? Go ahead and say that on refresh. And there we get 012 and three. Obviously, we could achieve the same thing if we wish to reduce our take away. Eso detriment. So we can a negative in there. You live in there and in here we have two negatives. Let me save that across and refresh. And there we are, reducing by one each time, so it's a little bit of a shortcut. Now, what you will find is that we use this method more so than any off the others. Obviously, Europe, you're free to use whichever method you wish. But I will be using this method here. 22. How to use an IF statement in PHP: in this lesson. I want to look at the if statement on the formats of the if statement is fairly straightforward. We have. If something is true, do something. We're using round brackets for the condition we wish to test that goes in between the round brackets. So if something is true, we want to do something inside the curly braces. I recommend that you always writes out your brackets like this first, before you start to put in the condition on the thing that you want to actually do the action, let me give you an example. Let's say we have number a being five. Let's say we have the is also equal to five on. All we want to, quite simply, is to test if these two numbers are equal to each other. So we start with If something is true, we're going to do something. The thing we want to test well, that's is a If it's equal to on, we have double equals. That's a conditioned test. If a is equal to be, if a is equal to be, then inside here, we want to do something, and the thing we want to do is just echo and we just echo truth. Let's just keep it simple, Okay? So if a is equal to be we want echo truths. When I preview this on the browser, I should get true and then we get we get true. Well, what if I put be being, say, six. In this case, A is not equal to be. So what will happen in this case? Well, we won't get to this. So in fact, nothing should appear on the screens. Let me refresh. And there we go. Hey is clearly not equal to be What if a is greater than the Let's test that so if a is greater than be unless proper a equal to it So it is greater than be so if a grill and B we want to echo True. So if you go back to here on will refresh and then we go A is greater than be now what if you want to test more than one thing So a being aids here on BB in six, which means that to 12 hand three. So let's say I want to test if a is less than or equal to five on the is greater or equal to 10 he felt. Let me change out to 10. So let's test the following. Then we want to test if a is less than or equal to five on and on and is the double M percent. So if a is less or equal to five on and B is greater or equal to 10 and if that is right, productive instead of m present. So we're testing if aid last week with a five on B is great records. 10. We're going to echo. True, Let's refresh how we get true. So yes, that condition is correct. Well, if you want to test if one or the other is correct, let's say want to test if a is less than or equal to two, which it clearly isn't. Yeah, or is the devil pipe? So if a is less than equal to two or b is great recalled to turn down again will echo True . So let's see if that is true this time and will refresh. And yes, it clearly is true. Let's go back and will change this to six. Well, hey is clearly not less and equal to three or two and B is not. Gregory called tend to. In this case, nothing should actually happen in the words again. We'll get a blank on the screen. Let's head on over to the browser and check that one refresh. And again we get a blank. So using these conditions within the if statement convey incredibly powerful. So it's something for you to have a little practice off. Just props and values in test. If one thing is greater or equal to the other test. If things are equal, test the end and test the or. 23. How to use an IF ELSE statement in PHP: So we're just taking a look at the if statement on, we're gonna move on to the if else statement. So the way this works well, in this example here we had Avian three and B V and six. We were testing if a was less than or equal to two, it's not or be being greater equals 10 again. It's not so we were not getting any effort to the screen. So let me go back and we'll go back onto this if statement on there we got with it didn't any output whatsoever. So what we can do is we can say else do something else. So if something is true, we're going to do something else. Will do something else. Must put these open, and we'll just echo it here. False. Okay, so in this case, we should actually get false to the browser. So if you go back to here and refresh, actually, let me go into the correct one. You want to be only if else statement. And there we go. We got false. Let's do this again with under the test. So start from scratch. So this time I want to test if a is equal to five and if it is also true house else a false . So if something is true, I want to do something. What do you want to test? I want to test If a is equal to five and if it is, I want to echo. Hey is equal to five. If it's not, I want to say else do something else and the thing I want to do is echo. I would just simply say A is not equal to five. Okay, so in this case, we're testing if a equals equals five, if it is, we echo this else, we echo this on, let's refresh and there we go. A is not equal to five. If it is equal to five, we put that in and we'll save on the refresh and there we go. A is equal to five. So hopefully that's fairly straightforward. We can build on this in the next lecture. When we look at else. If 24. How to use ELSEIF statements in PHP: in this lesson. I want to look at building on this idea off. If Andi else And in this previous example, we were testing, if a was equal to five, if it waas, we presented a message, and if it wasn't we give another message. Now we'll move on to next. Is multiple tests so else if statements. So here I have dollar a vehicle to five. What I want to do on this occasion is test if a is a multiple of three, tested its multiple of four or if it's a multiple of five on output appropriate messages, let's start with If something is true, we're going to do something. And the first thing to say I don't test if a is a multiple off three now, if you'll recall from a previous lesson we used the module ISS operated to do that. So if a module iss three is equal to zero, So what I'm saying is when I divide a by three, if I get a remainder off zero, then it must be a multiple off three. So in here I can say echo number is a multiple off three, then here I want to test if it's a multiple of four, so I can say else if something else is true, do something and there's something else that I'm testing is well, if it's a multiple off force or a model is four. If that is equal to zero, then it must be a multiple off four. I can keep building on this so else if something else is true hoops, something else is true. Do something on this time we're testing. If a model s five is equal to zero, split that open test that in and this time it would be a multiple off. Five. Lovely. This is making sense. If it's a multiple of three, we echo this multiple for IQ of this multiple of five. Echo this. Now, If none of these are true, then obviously it's not a multiple of 34 or five, so it can say else do something else. And in here we can say number is not a multiple off three for or five. We'll save that refresh and there we go. Number is a multiple off five and clearly is a multiple of five. So it's actually going through testing this condition test in this condition. It's hitting this one and it is true and therefore is echoing at that message there, so healthy that makes sense on we will be practicing this little bit later in the course. 25. How to use FOR loops in PHP: welcome to Section six. Everyone on and I've already gone ahead and created a folder called Section six on a credit file for Loop Stop Pitch P on. I'm actually inside that file, the americal. When we did the if statements we had, if a condition was true, then we did something in the four loop follows a very similar format. We start with the word for so four. Something is true. Do something So inside here, we need our conditions. I mean, here will put whatever action we're going to take. This is going to be our loop inside here. Now, the way we format this inside the brackets is we have three bits of information. The first bit is we start with an incremental or value variable. In this case, I'm going to put equal to one. So for this particular example, I just want to print to the screen the numbers 1 to 10. Some study with my incremental at being one. The second piece of information is the test. So in this case, I'm testing. If I less than or equal to 10. And while that is true, it will keep looping through whatever is inside thes curly braces. The third thing I need to do is actually to income. It are increased this value by one each time. And if you recall, there's a nice, easy way of doing that. I plus plus. So there's my three bits of measure, and I'm starting without being one testing. If it's less than equal to 10 and then in creamy in increments in by one and in here, I simply want to echo at the numbers 1 10 Well, the numbers 1 to 10 will come from I I'm going to come cabinet on this ah, lane break and we'll save that. So I hope that makes sense for something to be true. Do something I'll go across to here. We'll check on that, and then we get the numbers 1 to 10 at put on the screen. Let's do the one this time. I want to put all the even numbers between two on 20 so my incremental will start at two. That's the first even number I need to test. If it's last nickel to 20 and I need to well, no increase by one. I need to increase by two on hopefully recall how to do that, it's plus equals two. We'll save that. We'll go across and will refresh. And there we go. We get the numbers 22 20. So hopefully that makes sense. The format is fairly straightforward. We've got four something to be true. Do something on this is actually building our repertoire. It's adding more tools to our two books. 26. How to use WHILE loops in PHP: we never run to the while. Loop on the format is very similar to the for loop color, slight differences, but the end result is the same. So in this particular example, let's say I want to produce the numbers 1 to 10 again. The first thing we do the Y lip is we declare our variable are cancer, are increments her and I'll have the equal to one. Next thing we do is we say while something is true, we do something from here we'll while I is less than or equal to 10 from producing the numbers 1 to 10 and in here. This is what I want to do I want to echo. I handle a pen down here, a line break Now inside the curly braces I need to increase the value of I by one. So I plus plus semi Colon and will say that to finish. So we're declaring our value for I. We're testing. If I hopes I is less than equal to 10 echoing the results on an increase in I buy one. So this will actually go through until I is 10. It will test. It is I last nickel tenuous it is you lack of the results. It will still increase i by one. So I will be 11. He'll go back to here. Is I Lesnik all the 10? No, it's not. And therefore this loop will stop. Let's go over here and refresh. And then we get the numbers 1 to 10. Now let's say we want to produce the even numbers less than 20. So I will start off at two while I is less or equal to 20 will echo result this time instead of increasing by one. I want to increase by two plus equals two and will say that we'll go over well, refresh and then we get the results to 20. 27. Challenge 3: Multiples - SOLUTION: hopefully found this to be a fairly easy challenge. And the idea on this one waas to generate the multiples of six which are less than are equal to 60. But to do that with a for loop on with a while loop, I start off with the for loop. Okay, So start with for something to be true do something on the something which need to be true is the following will start off with I being equal to six. That's our first multiple and we're testing if I is less than or equal to 60 and then we need to increase i by six So plus equals six and in here we need to echo the values of I I'm walkin Catholic on a line break and I say That's the 1st 1 done We also have to do the wild loop and for a while loop we start with variable beginning So this time will do Jay So J equals six Then we have while something is true do something There's something that needs to be true is I r for NJ Got to be less than or equal to 60 I didn't hear any to echo goods Echo J and then have a break on the last money to do is actually increase J by six. So plus equals six. I'll save that. Go across to my prez. Er Onda will check multiples. There it is, the combat. And then we go. We get 6 12 18/60 and that's the while loop there. 28. Challenge 4: Let's Play Fizz Buzz - SOLUTION: Okay, let's have a look at the solution to the fizz buzz challenge. Now the idea here. Waas to look through the numbers 1 to 100. If a number is a multiple of three, then we say fizz. If it's multiple of five, that makes a buzz on if it's multiple of three and five, then we say Fizz buzz. So sounds old. Fairly straightforward on what we need to do is need to start with a loop. So I've created a fizz buzz file here, so I'll start with my PHP code block on. The first thing I want to do is to start a loop. So four something to be true. Do something. They're in here. I'm going to start with an income enter of I being equal to one. Some starting from one need to go to 100 semi colon. The need to test if I is less than are equal to 100. And then the last piece of information is Theo. The I plus plus so increase. I buy one. They're in here. This is where I am going to action the fizz buzz or just display the actual number. Now, anything carefully about this because if it's a multiple of three, I have to do fizz. If it's multiple of five, then buzz. If it's three on five, then fizz buzz. Otherwise, I just display the number. Now the way the logic operates, I would need to have a look at testing if it's a multiple of three on five first. So if something is true, do something. So for this one, if I is a multiple and that's the module ISS, so if it's a multiple of three, in other words, with R equals zero. So if that's true, owned marginalise five is also equal to zero. Then it must be a fizz buzz sunny to echo, echo, fizz buzz. I see append a break on there as well. Now, if it's not a multiple of three on five, then there is a possibility it could be a multiple of just three or just five. So we need to do else. If something is true, do something and in here, we're going to test. If I is a multiple of three by itself, and if it is, then we need to echo no need to echo fizz and again will depend a break on there now is not multiple of three. Then it could be a multiple of five. So else if something is true, do something. And in here, well, I could be a multiple off five. And if that zero there's no remainder is a multiple of five. And then I need to echo me to echo buzz and with penned a break as well. So if it's not a multiple of three and five is not multiple of three, and it's not more than full five, then it must just be that we just echo the number. So else we're going to do something and think I need to do is just echo. I I need to concomitantly on there a break. So let's say that well, head on over to the browser and refresh So going to fizz buzz. And there we go. 12 fizz, four buzz. So 123456 would be fizz. That's correct. 15 would be phase burst cause it's multiple of three and five. So golden. So a lot of if statements within that loop there, but hopefully you can see the power of actually using these things 29. How to use the PHP online reference manual: in this lesson, I just want to introduce you to the PHP dot net manual. Now this is an extremely useful resource and something you will need to refer to from time to time. Unfortunately, there's no way I can cover everything in this course to give you a couple of examples. I've gone to PHP dot net After a quick search for string length, it's come up with this inbuilt function here. Now the top of the page, you will find a description off the function. The first pit here is telling you what it returns. In this case, it returns an integer so a whole number. This is the name of the function itself. Then we have room brackets and then inside the round brackets will tell you what you need to place. In this case, we need to place a string. He first rolled down a little bit, some more details, and then you'll find an example. So in this example here is echoing a string length off A B, C D E f and gives a return value off. Six second example we have pie pie you may recall from school is 3.14159 etcetera. In this case, the return value is a float. In other words, a decimal that's the name of the function pie. On this time in their own brackets, it says Avoid. So in other words, we don't need to pass anything into this function. Scroll down a little bit. We get some examples. There we go, moving on to the third example, we're looking at some debt functionally. In this case, it's returning an array, and we will come onto a raise in a later section. The function name is get a ticket. We have the round brackets, but inside the round brackets unit is thes square brackets. Now, square brackets means that this is optional. If I scroll down, we get Plan C of examples here. So a variable day is get debt and then we print are we will talk about that when we come on to raise on what it does actually, prince an array of data and there's quite a lot in there. So hopefully as giving an idea of how to use the PHP dot net manual, I would urge you to refer to it from time to time. If you are taking on one of the challenges you may need to refer to it in terms of trying to find out how the the functions actually work. 30. How to generate random numbers in PHP: in this lesson, I want to look at random number generation. The random numbers are extremely useful. When coating, for example, we can use a random number in again. Let's say we want the computer to pick a number between one and 100 we're gonna try and guess it run. The numbers and random functions are also really useful when we want to select random values in a raise on will be completing a little challenge best on this littering the course in order to generate a random number, we need to know what the functions actually called so we can use the PHP dot net manual on performance search slights head on over. So I'm on the PHP dot net website. I've gone to the manual. I'm gonna search for random on. There we go run generating a random interred yourselves like that. Here's the description, and it looks like we've got two options. The 1st 1 returns and introduce a whole number. The function name is Rand, and in this case it's void. In other words, we don't need to pass anything in. The second option allows is to pass in a minimum value and a maximum value, and that's what I want. I'm going to generate a random number from a list off 1 to 100 so I don't over to code anywhere. I've created a file called Random Number on. I want to generate a random number, so I'll call my random number. Well, call it random number. I'm using Camel case, if you recall. So random number equals rand and then in round brackets I want to go from 1 to 100 semicolon on the end, and I'll just echo out that random number. I'll save that. Have never to my preview refresh. And then we go get a random number off 60. I'll refresh again 47 this time. And then Eddie so healthy you can see how it would be really useful if we want to use their anal of GAM or to pick something at random. Now there are the random functions, and we will come onto those later in the course 31. Challenge 5: Heads I win - SOLUTION: I hope you found this challenge fairly straightforward. It's the heads. I win challenge. And the idea is we generate a random number between one and two. If it's a one, Echo heads, I win. If it's a tales echo tales you in Let's go ahead and do that. So if credit my file had stopped PHP I've started my code block on. The first thing I need to do is to generate a random number. So number as my variable will equal rand and open and close my brackets. And in here I'm going between one and two. That will generate the random number. The next day I need to do is to test if that random number is one. And if it is echo heads, I win else. Echo tales, human. So if something is true, do something. And in here well, I'm testing if number is equal to one. If it is, I'm going to echo heads. I win. No, it is not equal to one that it must be equal to two. So I just say else and in here I need to echo tales you in now. Hopefully you've tried the extension section as well and in this one it says we need to loop 100 times and then to keep track of how many heads on and family tales. Let's have a look at that. Well, the first thing I need to do is to have a counter for the number of heads and the number of tales. So heads will start off zero, and tails also start off at zero with any to have a loop which goes 100 times. So for something to be true, do something. Now, there's something I'm actually doing. Is this section here? That's what I'm repeating 100 times. So I'll copy that. Could sit on, Just paste in here on. They'll just in dent this code here, There we go. Now, in here, I lead an incremental er. So I most out of that one. We need to go to 100. So we're testing if I is less than are equal to 100 on the last part of this is to increment I by one. So I plus plus. So what that is gonna do is going to test 100 times and echo this out 100 times. So being idea in here just to echo a a line break hand in here as well Now, because I've got heads and tails here said at zero. I need to increase these every time I get ahead on doffs, they increase the tales every time I get a tale. So in here I will have heads and it's increasing by once. That's just plus plus on in here. Do the same thing for tails Tales plus plus semi column. The last thing I need to do is to echo at the result we need to echo. There were you too can cabinet on the number of heads. So heads and then can cabinet on the word heads that will tell me there were on example 48 heads and need to Catherine it on the number of tales. And I just need to add a full stop and the semi car. Okay, so that's all done. Let me say that. Then we'll go over to the preview on to see what happens 32. Challenge 6: Who ate all the pi? - SOLUTION: for this challenge, we had to calculate the area off a circle on. I ask you to use the pie function with a radius of five, but then two round the answer to two decimal places. So what I'm trying to get you to do is to use the PHP dot net manual and then to find out these functions for yourself, Aunt, how to use them. So I have across and we look at the solution of this little challenge. The first thing I need to do is I would declare a radius very well and I'll put their equal to five. I don't want to work at the area of a circle of the area is equal to pi, which is P I with open and close brackets. And that's multiplied by r squared so I could do the radius times the radius. But it been nice to actually used the power function which goes like this power. And then in here I'll do the radius and I'm reason the radius to the power of to So there have pie times. The radius squared. Well, then just echo. It's the area is hey this one area So I'll go ahead and say that we haven't quite finished yet because I'm taking care of the around function. Let's go ahead and refresh and there we go. The area is 78.593 etcetera. I don't want to do to run this to two decimal places and I said there is a round function. So if we go to the PHP dot net websites and in search which type in round and their biggest , it's ah, round it runs a float off. So yes, that's when we want. This is the description looks a little bit complicated, but let's just break it down. It returns a float, a decimal, as we want. The function name is around then in between the round brackets. This is what we need to put in. We definitely need to put in a value which is a flow to decimal and then optionally. We can actually put in a precision. So in other words, how many decimal places do we want? Let's go ahead and do that. So what's created? Another variable here will call it rounded area just so we can see the difference and what I want to round in here is the area to two decimal places. Now I need to do is to change this variable here to rounded, rounded area. I'll go ahead and say that go back and will check out brother and will refresh. And there we go 78.54 And if you want, you could add on units squared, correct to two decimal places. 33. How to use the date function in PHP: we now move on to using the debt function in PHP on Do you may find yourself using this quite a lot in many of your scripts. And if you think about it, we use the date and time quite a lot. When we visit websites, for example, you may log into a certain website and if you haven't visited, say for 30 days, then they could actually have a little pop up appear on many websites, you will see the debt appearing. We could do things like how many days until Christmas? So a lot of power in these functions on this lesson really is just a introduction to some of them. The first thing we need to consider is the time zone that we're in. If you're using code anywhere like me, then all the code is action on a server somewhere in the world. Now I am actually based in Brisbane, Australia at the moment. So I need to do is set my default time zone and I do that as follows put in debt. And there it is. Default time zone set. Then in brackets, I put the location so puts in quotes. Australia stroke Brisbane Now, if you want a list off time zones, if you do a quick Google search, it will come up on Duthie addresses. PHP dot net forward slash manual four slash e n four slash time zones dot p Hedge p and then you can research your time zone from there. The next thing I want to do is to play around with the debt function. I'll show you two or three, and then there's a little challenge for you to accept on you. Get some practice using these. If I go to the pitch p dot net net manual on. Have a look at the debt function itself. We can see the format. It's a string. The function itself is called debt, and then we enter in a format that we want to separate on. Let's start with a very simple example. Let's say I want to echo it. The year is, and then the year. So if we scroll down here, we get a list of all the format characters on. The one I'm looking for is the year and there we go. It's a capital. Why, if I want the full year or if you just want to characters. It's a lower case. Why let me go ahead and do that? I want to echo. The year is and then I want to congratulate on on all taken cabinet on the debt. So put debt. I'm brackets. I want the year. I'll go ahead and say that. Go across to my browser and refresh and there we go. The year is 2000 and 15. I'll just echo in a break here I don't want to do in here is I want to say what the debt is in terms off the let's say the day of the week. So echo I'll fly Something like today is what's going cabinet on a date function. I'm looking for the day of the week. Let's go back here and see if we can find the day off the week. There we go. We can have it turned by a man and sense of the couple d. We can use the lower kiss. Allah's well think I'll use that one. So lower case l save that. Go across the browser and we'll refresh and then we go. Today is Wednesday. Now we can actually put more than one format code in here. So let me just echo another break here. Let's say I want to put in, Oh, the time. So put the hour, the minutes, a movie, am or PM So I want to echo at the following the time is, can Castanet on a debt function on? The first thing I'm looking for is the actual our. So that's how I look, then a little bit. Here we go. He's the ours in the minutes. So let's do Let's do a G little kiss G for the hour. So first of all, have a G. I'll put a coal onto hours and minutes, so the minutes we have is an I. And then once put AM or PM, so AM rpm is a lower kiss, a Arkan put toe and every case a other lower case. I'll say that. Go back and refresh. There we go. The time is 9 51 PM so hopefully you can see how powerful these are. Hey, it's just case of a little bit of practice on referring to the PHP manual. So next thing for you to do is to go ahead and try the carpet Diem challenge 34. Challenge 7: Carper Diem - SOLUTION: hi. Everyone hopefully enjoyed that little challenge. Hopefully, it wasn't too difficult. I know you probably had to flick backwards and forwards between the PHP dot net manual, but once you've used these a few times, you will pick up the characters pretty quickly. The first thing we need to do on this challenge is set up the default tame zone, and I've gone ahead and done that. I've also added a few comments. Some lame breaks on have echoed in the debt, ready to accept the characters in here that will save us a little bit of time. The 1st 1 Monday, the sixth of April Well, the day is given by a lower kiss Hell, the six is given by a J. And then the T Hitch like the six of the third of a second. Those two letters that's given by a cattle Hess the full month where remember, is the F fulfillment. And then the year is a capital. Why next one? Today is Tuesday, so today is and then we can cabinet on the debt. We've already seen that the full days given by hell next one is Monday, the sixth of April 2015 with a comma in there. Well, we already know that Monday is given by an L. The 06 that's given by the the film month F and then we need to come over on the year is couple. Why next one? All the year is kept a why four slash Then we've got the month lower case M on in the day. Lower case d Next one is Monday the sixth of April 2015 with a time on the end. Have already done this debt appear. So I just grabbed that. Copy it paste in here. There's no need to add his the comma and then the hour, which is a G on. Then the minutes, which is I and then a Capital A for the AM and PM That's the standard challenge done. Hopefully, some of you attempted the extension exercise, which waas to see if the current year is a leap year writing performing a test. It's an if statement. If something is true, do something. You know the thing where she testing here is if the year is a leap here and we do that with a capital l. So if date function with brackets and inside those brackets will need a capital hell. Now if that dead function is equal to one, that is a leap here and inside my curly Breck braces, I'll say echo on its own. Just saying Leap here, actually echo the debt. And I put in here Capital. Why? So we'll get the year. Welcome, Captain. It on is a leap here that will make a bit more sense. So if easily Pierre will do that health, we'll do something else. And the something else is. It won't really fierce. I'll copy that. Pissed it in and say is not a leap here. So hopefully that makes a little bit of sense. I'll go across the preview, cap it dim, and then we go. Everything is formatted, as we need now is a good idea to become familiar with these functions. They are used quite a lot in a lot of scripts on. It just adds another tool to your two bucks 35. Time functions: we now move on to look at some time functions on time. Functions are great alone because times are usually stored in databases as seconds since epoch. Now, this means the number of seconds since January, the 1st 1970 on calculations with times are performed on a regular basis. So being comfortable with these calculations is a big bonus. Now, one of the easiest functions to use is to simply eco time on. Let me go ahead and show you what I mean. So if we say echo time what, this will actually do it likely echo the number of seconds since Joanie the 1st 1970 If I refresh there we go got number seconds. I'll refresh again and you can see that that is increasing. So using this facts, we can actually cock lit the difference between two debts are times. In other words, convert both debts and times to number of seconds. Cockpit the difference and then comm bark, convert back into the number of days or hours or minutes. Whatever we need to do now, that's the shooting. We want to know the number of days until the first of January 2020. Now Unfortunately, I don't know the number of seconds until January the 1st 2020 So it seems that I can't use this functionality. Fortunately, there is a function to help us with that. Let me just show you what it is. Let me set a variable cold debt unless I want that to be January the 1st 2020 So I'll say first of January 2020 and obviously that doesn't make much sense at all. But what I can do, I can actually apply a function to this and the function we wrap this in brackets is string to tame. Put some quotes, or in this I'm finished with a semi colon. So string to time, we have a string we're gonna convert it to tame. And we've already used that function. So in other words, I'm converting this two seconds. No, I need to do is to perform a calculation. So let's say seconds. Well, seconds will be equal to the debt minus the current time on then I need to do is to echo seconds. I'll say that Huggel cross and will refresh on their regards the number of seconds between those two times Now that doesn't really make much sense to me. It make much, much better if itwas the number of days, number of minutes, whatever it is. So what I can do? I can take number of seconds. I can convert it to minutes. Minutes would be number of seconds divided by 60 60 seconds in a minute. I could then say, Well, one point to know the number of hours so hours would be minutes divided by 60. I came because there's 60 minutes in an hour and I could do days. Full days would be the hours divided by 24. And then here I could actually echo the number of days. Save that and we'll head across and will refresh. And there we go. We've got 1721 point whatever it is really this decimal parts here. I don't really want to see that. I want to round it up. It's what we can do here we can use into the function called Ceiling, says one called Ceiling on one called floor ceiling will rounded up floor will run it down on the function is C E I. L. For sealing and then we're up that in the brackets, we'll save that. We'll go back and we'll refresh. And there we go. 1722 days now. Obviously, I didn't need to do all of this. I could have just taken the number of seconds so I could have done something like this. I could have said that the days is equal to this conflation here. That's the number of seconds divided by 60 to give the number of minutes divided by 60 to give the number of hours divided by 24 to give the number off days. And when I say that, go across and re fresh, There we go. We still get 1722 days. The nice thing about this string to time is we can put almost anything in here so I could say something like next Friday. I can say that Go across and refresh and then we go. It's two days to next Friday, and it is cause today is Wednesday. I could put in a debt like 12 of the 12 2054. Say that go across to here and refresh. And there we go 14,486 days. So hopefully you can see the power in this, especially in this function here, extremely useful. And then obviously calculating the number of seconds between two times is again extremely powerful. And it's something you will come across when you come onto the database unit. 36. Challenge 8: Santa Claus is coming to town - SOLUTION: Okay, let's have a look at the solution for Santa Claus is coming to town now. Hopefully found this fairly straightforward, especially after the previous lesson. What we need to do is to convert the 25th of December from a string to a time and then take the current time and subtract that from the 25th of December. That should give us the number of seconds. Once we've got the number of seconds we can convert at two days, let's go ahead and try that. So that sets Christmas Day equal to well. We want a string to tame and we're converting the 25th of December. So I went to that as December 25 again that there's multiple ways you could actually enter . That next thing is the current time. Well, the current time is given by that. Then the number of days. Well, the number of days would be Christmas Day, minus the current time. I want to take that calculation on. I want to divide by 60. That would give me the number of minutes divide by 60 again. That would give me the number off hours and then divide by 24 to give me the number off days. And if you recall from the lesson when I do that, I will get decimal answer. So what I need to do is take that whole thing that in brackets and then take the ceiling of that So c e I l. And then for my semi colon on the end on the last thing to do is just to echo at the results. So echo the number of days until Christmas is space and Alcon cabinet on the end. The number off days. So hopefully that makes sense. Let's go across to the browser and we'll refresh and there we go The number of days until Christmas years. 253. 37. How to create a basic HTML page structure: Now that we've covered a few basic tools, we need to start thinking about user inputs on how we can deal with these in order to accept to user input, we'll need a form. But before we create a form, it's probably good points in the course to briefly review thestreet chur of a hasty male page. Now, for those of you who are comfortable with haste email, you may wish to skip this lesson. So here I am in my konitzer, I've already created a page on I'm gonna type in here my page templates. Now, the first thing we should do, although not essential for our testing purposes, is to declare a document type. And we do this with the following. So left angle, bracket, exclamation mark, then Doc type and hey, H T m l. So we used in the hay html five standards. And you may have heard of that If you haven't don't panic. The next thing we need to do is to open a HTML tag. They noticed as soon as I hit that right angle brackets, it automatically closed this tag for me. So anything in thes left and right angle brackets, this is attack, and this is my head. Html tag. That's my opening tag, and this is the closing tag, the only difference being that four slash. In between those tags, I have two sections. The first section is the head section on the second section after the head is the body section. Now in the head section, you'll find things like links to the CSS style sheets, meta tags and titles. So I just put a tight line here and for my title, Al just put test page now. Anything in here does not appear to the end user. Apart from the title, the title is what appears on the actual tab. So if I go across to here and refresh that you go, you can see Test Page has now appeared on this tab. The body section contends the elements that do appear in the main browser window and to give you an example, I'll create a paragraph tag and in here I would simply say Hello world and again y break with tradition. I want to deny, I'll say that I'll go across and refresh and there we go. We've got Hello world on the page and again very basic. This course is not about hasty, Mel. It's about PHP coating, so I won't be spend in much time looking at the Hoechst email cold unless is required as part of the task were actually doing. If you do need to know more about Hoechst email keeping out for my course on his team L. And again, that course is done through the completion, off challenges and a Siri's off many tasks. 38. How to create a basic HTML form: So this is where we left off. In the last lesson I had created a basic HTML templates her no sitter as hate. Email template dot h t m l. Now, whenever I need to create another haste email page on, we're gonna be creating a form in this lesson. I can right click on this and clone it. And this one, I'll just call Hey, html form, I will create And then we go, We've got hasty malformed. I'll open that. And there I have my template ready to go. Obviously, I could also have copied and pasted. I just want to things I want to point out here you Meghna leases little arrows down the left hand side next to the numbers. If I click on these, I can actually close the code segments, and that's really useful. As your scripts get longer and longer. I'm gonna go ahead and close this head section and I'm going to working in the body sections. I'll give myself a little bit of space to make a little bit clearer now because we're craving a form. We start with a form tank on. My form will have different inputs, so I'll go through a cup of these, the ones will be using in this course. Anyway, on there are plenty of courses online that show you how to use different form elements. So I'll leave you to go and research those the 1st 1 will be doing is the input. So you have an input and this will have a type of various types and you can see them here. The type well we're doing is text. We think of a name and then I'll give this. I just call it surname. Lesson much. We're creating a form where the user is going to input their surname. Now, what this will do. This will make a little box and input box appear on the browser window. The next thing we can actually do after that is where we need some way of sending the form to some sort of processing script. So we need a button again. We'll have an input this time. The type Well, it will be a submit bun. We'll give this a name and we'll call it submit. We can also give it a value. And if this was some sort of search for market taping here search Aiken tape in log in. It could be. Just submit. Now, if I don't put anything, it will actually default to submit. Anyway, I believe that. No, I haven't quite finished. But just to show you what's going on here and what we've got so far, I'll go across to my preview Hasty mild form. And there we go. We've got an input box and a submit button. Now you've got those things taken care off. The next thing we need to do is to give this form and action. In other words, when we hear the submit button, what's going to happen? So in here afterward, form public space and will say action equals and this unquote And in here we would place the u. R L to the processing script. After that, we have a method. We have two methods to choose from. We use it to get our post, will be doing post. So actually sending the data to a process in script via the post method. I explain those that live it or detail as we go through the course. So basically that is our script finished. I'll say that. Go cross. Some will preview again, but will be absolutely no difference in the way this actually looks. And there we go no difference whatsoever. So this is a very basic form. The input type I just want to look at is the radioman. So again we'll have an input. The type this time will be radio. I would give it a name this time will say gender given to the one will have an input. You're the type again. This will be radio. The name again will be gender. Save that. I'll go across to the preview and refresh, and then you can see I've got two buttons. The Earth Ian need to do is to add a level for each one. When I click on the there weren't the other one turns off and face versa. Now the reason that happens is because I have given them the same name. If I'd give him one different one, let's say gender one and gender, too. If I said that, I'll go across and preview. I can click on that one. I can also click on that one, and that's not what we want in this case. So leave that as gender and gender now in here. I could just put mail. And in here I could put female on for this one. I need to give them a value. So the value for this one is going to be male. You probably guessed the value for this one would be female. Say that Go back and will refresh. And there we go. We got mail and we have female. I don't noise like one or the other so I can turn my name here. Male or female. Hit submit and then that will be sent to the processing script. 39. How to use form data in PHP: So this is where we left off. We just created a very basic form and I'm going to use this as an example of how we can actually process inputs from the form. Now, before we go much further, I'll just go over the form elements here. Currently, we have an action off. Nothing. Now we need to send this form to a processing script. And I have credited processing script called Hate TML dash form, dash process er dot PHP So in here I need to put that name so hasty. Male form process er, don't PHP The method is post. My first input is thesis. Earn em on here. We see the name of this input is surnames. We need to remember that. Then we have the gender and we have a submit that all seems fairly Okay, Let's go across to the former processor. No, in here before we go much further. I just wanted to see what the posted inputs look like And what we can do. We can do something called print underscore are And then we say what we're printing Will Enron brackets I want to print the post data now if I save that I go across to here. This is my input form. Just that. Very fresh now that I have updated it. Okay, so just put my surname in the tomorrow and male and submit. And then we go get this array back. With all of this data in here, it looks a little bit confusing. So what will do just to make a little bit easier? That's going to hear I'll just echo Pre was like a pre format for a code will save this. Go back to here and will refresh. And there we go. That makes it a little bit easier so we can see the surname is Morrow. The gender is male, obviously the submit button as well. Not really what I want to do. I want to work with the data that's being submitted. So the first thing I can do I can say, is declare a variable off surname on the one that to be equal to post in square brackets and then tick marks surname. So this here refers to this year's That's the name of that input field. So I'm assigning that input field to my variable called surname. I could do the same thing with gender. So gender equals post square brackets, tick marks and gender. And I want to do here is just to echo something like your surname is. Then we can concoct in it on surname on your gender is members can cabinet on here. Gender can save that. Go out to hear him will refresh And there we go. Your surname is morrow on Your gender is male. Now we do have a bit of a problem here on your pro won't realize what it is. But what if I am visit this page without having submitted anything in the form into the words Don't just go to here. I'll just press enter and you can see we've got an issue here. It doesn't know what surname is and it doesn't know what gender is. So what we need to do we need to put a little test in here. So if something is true, then we're going to do something. So if something is true, do something. Now that's something we're gonna do. Is all of this from a cut that's on? Well, pissed it in here Most intent. That and the test we're going to do is we're going to test if he submit. Button has Bean pressed. There are other tests we could perform as well, but we'll keep it fairly straightforward for the time being. So if is set, then run brackets. So gonna test if something has been set. And in here we want to test is post submit. There's a lot of Barack it's going on here, so you need to be careful. But that's going to test if the submit button has been pressed. If this meant Button has been pressed, then we'll go ahead and will process all of this. Let me go back to here and refresh, and this time I should get a blank screen. There we go now. Obviously, I could put further tests in here that if it hadn't been submitted oppressed, that could echo, please fill in the details. I couldn't send them back to the form, etcetera. So what's actually happening here? We're testing if the button the submit button has been pressed. If it has, then we're doing all of this. And obviously we could perform further checks. And for any input form, we would actually perform more checks than this anyway. But it's the 1st 1 we're doing, so let's keep it simple. 40. Challenge 9: Temperature conversion - SOLUTION: This is the video solution for the temperature conversion challenge. Now the idea here was to design a hasty male form to accept the temperature on whether it was degrees centigrade or Fahrenheit. So we need an input form. We also need a process in script we don't do. Over here is our right click on my head steel mill form templates, and I'll clone that. Let's call this temperature form his team. Oh, and there it is, will open that. So there is my basic HTML templates and in here I need to create a form. So start with my form tanks. I'll need an input field off type on, not can use it a number here. So let's put a type of number and the name of this one. Let's call it temperature. I need another inputs, and this time the type will be radio. So I'm going to have radio buttons, one for centigrade and one for Fahrenheit. The name for this one. Let's call it units. So units with temperature. Now I need to duplicate this. So if I told them my shift command and D on the Mac, so it's probably shift control. The on a PC. Is there a duplicate that pulling there and here The value will have as C for centigrade and in here will have a value equal to F for For Nate, I would just put little label on here, See on deaf Now that said previously were not formatting these I would normally put some proper labels on here. Some CSS. I would stay little properly. This is just a quick and dirty form, because what we're interested in is theatrical PHP side of things. I also need a submit buns. Only another input and the type here will be submit and we'll have a name again Off Submit that we didn't lost one. In fact, let's change. Let's put in no lleva submit and sorry, The partial value well put as convert Next you need to do is need an action appear This is the name of the scripts were sending too. So where's the year? Or else it will be temperature form process, sir. Let's call it that. We know what it is. Don't PHP And the methods method is post Okay All looks fairly straightforward. It's a little tight. Lynn here, Off temperature conversion. It will save that. And now I need to do is create a script to do this processing. And it's gonna be called temperature form process. Er we couple that credit New fail. Untested in on create. That's pretty. My process of forms. Let me open that. Let's begin processing. The first thing isn't in my PHP code block. I need an if statement we're gonna test if that's submit. Bun is being pressed. So if something is true, gonna do something. So if is set run brackets go inside the round brackets post and then in square brackets, take marks and submit. So if that is true than in here, will have a variable cold temperature, which you want to be equal to the posted temperature. So again, this temperature here is referring to this temperature. Here's this input I'm taking that input on. I'm assigning it to my own variable off temperature and then I can use that threat. My script next one need is Thea units. So, units that legal post hand units I think I called him units. Yes, I did. So it a good idea to check semi Cohen. So I signed my variables. The next thing to do is to actually check what units were actually submitted. Was it degrees C or was it degrees F? So if something is true, do something. So if units is equal to see, then Degrees C has been selected and I'll need the formula for converting degrees C two degrees F. Let's say result equals now a convert from degrees C, two degree trees f. I need to take the temperature. I need to multiply it by name and divide by five and then had 32. So I'll give me the result. If it's see else. If it's not, say it must to be f so in here. In this case, my results will be as follows. In brackets, I need to take my temperature. I need to subtract 32 the two times by five and divide by name. Okay, It's another outsource all being done in here. I need to echo at the results. So echo, let's say something like he converted Temperature is let's have in here result. Okay, so that should all work may save that. We've got our temperature form. If it looks OK. A process, er everything looks OK. Let's to refresh on here and we'll find the temperature form. There it is that, say, 32 degrees C. I will convert that on. The converted temperature is 89.6, which is correct. Let's try another one. Let's go back. Try 100 F. It's convert. And again, that is correct. If the extension activity I did leave you to say whether you want to run this off to a degree of accuracy and I'll leave you to have a guy like yourself. 41. How to find the length of a word or sentence: When we interact with websites, we use words. We use numbers, we press buttons, we watch videos, we look at pictures etcetera. But when we want to collect to use the data, we use forms as we've already seen on When we deal with that input from the user, we need to be able to manipulate the strings. So this section is all about string manipulation using the inbuilt functions. No, one of the ones I want to show you is string length and you can go to the PHP manual. You can look at it for yourself, but it is fairly straightforward. Let me go ahead and show you what I mean. So start with a PHP code block and they'll have a sentence. Okay, this is a sentence. Keep it nice and simple. I'll put a full stop in there as well. Now I want to do is find the length off that sentence and I use S t r l e n for string length round brackets and semi colon. And inside here I put the string that I need to find the length off. In this case, it's sentence then only do is to echo at the result. So the length of the string is and I can concussion it on here. The length? No. I am going to show you how to deal with single and double quotes and how we can actually move this inside this string and under their lesson, but for the time being will just leave it like that. So go ahead and say that. Go across the browser and string length. There we go. The length of the string is a 19. Nothing to point out is it's actually counting the spaces and any additional characters such as Phil stops. 42. How to count the number of words in a string: counting the number of words in a sentence is fairly straightforward. If you refer to the PHP manual, you will find some more details here. I'll go straight ahead and they'll start with the sentence. My sentence is this is sentence straightforward and the number of words we'll have as well . We want to be with str for string and if you look down here, we have string word can't so use that on Enron brackets. We pass in the string, which is sentence in this case and then I just want to echo out words. Oh, had saved that. Go to my brother and refresh. So word count they were going to go Four words and 1234 So really straightforward. 43. Challenge 10: How long is a piece of string - SOLUTION: Okay, hopefully, you monies to solve that little challenge on the way I'm gonna do it is as follows. So the first thing I want to do is to create my input form. So start with my doc type. Lester is properly on. The next thing I want to do is to have my HTML tanks. I need a head section. I'll even put in a title in here. This will be calculating length of the string, then have my body section in my body section. I just need my form. I'll need an input. My first input, The type It'll be text and the name. I'll just call this strength. I have a second input and this one will be the submit button that a name off submit with my form. I'll need an action on what we've been doing in the previous forms. We've been sending the data to another page, and that's great. Will process the form. What I'm gonna do this time is I'm going to put the PHP within this page itself. So my action, I will leave blank the method. Well, that's still going to be post. That's the completed form. No need to process that form on actually cockle it the number off words, so we'll start with a PHP code block on. The first thing I need to do is to have a variable string, and that's going to be equal to the posted string value. So post hoops underscore post in square brackets with tick marks. I need string. So this string here he is going to be assigned to this variable here. Next thing I want to do is to work the length. So how many characters long is it? So length, and that's going to be equal to string length and then run brackets I need to pass in the string, which is strength. I don't need to just ECU it. The string length so echo the string is and how many currencies loves it. So put length characters along there we go and we just finish off that PHP code block. I'm going to go ahead and save that on grocery the error that will actually occur when we actually preview this so go ahead to the browser hand will do a quick refresh. There we go. We get this undefined index string and the reason that's happening is, the form is being up on the screen. But this is also being processed. Now, we don't want to process this until the submit button has been pressed. So we need a conditional statement in here, which is if something is true, we're going to do something now. That's something we're going to do, is all of this. So grab that. I will prop it in here. Justin Dehn this Here we go. Now. This condition is well, if we've submitted this. But if, in other words, have we pressed the submit button so if is set and then ran brackets, we put whatever we want to test. Well, we want to test if Post submit hither words. Has thesis admit button being pressed? If that's being set, then we want to perform this operation here. So go ahead and save that. We'll go back to hear hand will refresh they Miguel's now we could enter a sentence. So this he is a string and will submit. And the string is 16 characters long 44. How to replace characters in a string: one of the things we commonly have to perform with strings is to replace characters or words. And a quick example would be a bad word filter on a website so we might have a forum user comes along, they post their entry and they might contain illegal characters or words. We don't want to appear on the website. So what we can do? We can use a function to replace those words with words or characters that we choose. Another example would be when we get fails from an external programs such as A. C S V fail. That's comma separated values and sometimes we need to operate with those fails. Now the character separating words or sentences might be a comma. It might be a pipe. It might be a semi colon before we work without, we may wish to actually change those characters to something else. I'll give you a quick example. So here we have my string replace file and in here I'm going to have a string. That string is equal to that. So you see, I have a list of names with the pipe character. Now what I want to do, I want to replace the pipe character with a comma and also include a space in there as well . So my new string, he's gonna be equal to string. Underscore, replace. And there we see the function there, and we can reference what we can do on inside the ram brackets. I need to put the following. I need to put in what I want to replace what I'm going to replace it with on the actual string, which in this case, is your strength. So the first bit of information is well, what I want to replace when I want to replace the pipe character. What don't want to replace it with. I want to replace it with a comer on the space. No, I need to do is just echo at that new string. Okay, so go ahead and say that will go across the browser and we'll do a quick refresh. So we're on the string replace and there we go. We've replaced the pipe with a comer and space. So a very useful function to have in your toolbox 45. How to randomise a string in PHP: There may be times when you need to shuffle the characters in a string to rearrange them, so I'll give you a quick example. Let's say I have a string on this string is equal to basketball, my new string or my shuffled strength. Well, that will vehicle to string on down here. We can see we've got string shuffle. I have seen the round brackets I will place the strength. Oh, they need to do is to echo out that new strength. Say that. Go to my preview on Refresh and there we go. It's rearranged it. If I refresh again, I will keep getting a different versions for keys, randomize ing or shuffling those characters. It's another useful function toe have. It's also used in a raise. And when we come onto the array section, how also show you the array shuffle Very similar process 46. Challenge 11: Mission Impossible - SOLUTION: So how did you get on with Mission? Impossible. Hopefully it wasn't a mission. Impossible. And you did figure out what you needed to do. So the first message you had from hit coup waas this str underscore r o t 13 hopefully realized that was a function and that you went to PHP dot net on did a little bit of research and here it tells you what this function actually does. So it's simply shifts every letter by 13 places in the alphabet. Okay, so we need to have a look at this string here on decode it. So it seems that every character has been shifted 13 places. So copy that. Go to my file and I'll declare a variable called string and place equal to this. Now my decoded string. Well, I need to use that function. So you copy that. Paste it in and obviously in between the brackets, I want to have strength. Then last thing to do is just to echo decoded. We'll see if that go to the browser and will refresh. And there we go ensure that you become familiar with PHP functions as they are extremely useful. And yes, they are. So a little bit of practice goes a long way 47. Double versus single quotes in PHP: I mentioned in an earlier lesson about the use of double and single quotes on said it would produce a short lesson on this. So I've got this little script here Got a variable name with Tom on Edge 21 and I want to do here is to just Eckert Hello, Tom. Your edge is 21 years old now, this is the method have been using up until this point, have been using double quotes and then concoct knitting on the variables. And I like to do that because the variable is very clear. I can see exactly where it is not a problem underneath. This is a second method can use. We can actually insert the variable directly into the string and it will be processed. The third method is to use single quotes. So pretty much like this lineup here and the fourth method, you'll notice that the variable is not white. In other words, this one here will not work and I'll show you what will happen. Here we go. So the 1st 3 are perfectly fine on the 4th 1 The variable is not actually being processed. So using single quotes in that way, won't work. You will need to come Cassin it on. They will find arguments around the net about one being faster than the other. To be honest, you'll never notice the difference in terms of speed the computers of your so fast these days that these things are processed in microseconds. 48. How to escape strings in PHP: in this lesson, I look at escaping characters on dealing with strings in a little bit more detail. I've got a sentence here, and what I simply want to do is just to echo that to the browser. So copy, I've got double quotes on Lane nine and single quotes on Line 11 Studied pissed in here on Dhere. Now you can see straightaway. We've got a problem. We've got different colors here, and the reason that's happening is when this line here, we've got a double quote here. But we're finishing the string here, and that was actually the second string here. So what about this bit in the middle? What's going on with that? On the second lane, we start with a single quote and then, well, we finish it here says the first string. We've also got another string here, but then we've got single quote on the end on this, but in the middle is just in no man's land. So if we go across the preview and refresh, you can see we're getting errors. So in order to fix those errors, what we need to do, we need to escape the character. So here we need to tell it that this character here is not the end off the string. And we do that with a backslash. There we go. Now I still even though the backslash here before this one. And now we've got a correctly formed string for the 2nd 1 Hopefully, you can see where we put the escape character. We put it here just before they single quote. There we go. If I go ahead and save that good, my browser and refresh. There we go. That's the first sentence string, and that's the 2nd 1 49. Challenge 12: The Great Escape - SOLUTION: here we have the video solution for the Great Escape Challenge on your task was to take each of three sentences on output them to the browser. So I'm going to start off here by opening a comment block, and I close it How? Just paste in those three sentences. Ready for use. Now, for the first sentence, we have a single quote here, so it doesn't make sense to use single quotes for this drink. I'll use double quotes. So echo, double quotes and in here will place this string here. And there we go. That one's all good. Then echo at a brick. The next one, we have single and double quotes here, so it doesn't really matter which one to use. Grab that again. I use double quotes and paste that in because I'm gonna do a quote here. I'll have to escape this double quote backslash and I also need to escape this one right here. And then we got someone. Don't copy that. Break put in here. And for the last one again, we've got double and single quotes in here. So for this one issue single quotes just for a different example. Grab that copy. I'm pissed it in. No, because we've you single quotes. I'll have to escape every single quote in this sentence. So I got one there and no more. So I'll save that head on over to the browser and will refresh. And there we go, No errors, and we have our three sentences. 50. What is an array?: in this lesson. We look at a raise now you can think of an array as a box, and this box can hold multiple elements. What we can do here, Let's start off with a terrible cold Names on this will hold our way and we start with the word array. And then in round brackets, we're going to place the names in our array. Let's have Mary. 2nd 1 will have gems. We can use single and double quotes. I'll put John, put Sharon so we have the values in our array. We have four values now. It's actually preview. This array will actually show you what it looks like. We use something called Prince Oh, are then in round brackets will put the name of our ray, which is names. I'll save that. Go across the preview on Refresh and there we go. We've got the ray showing. Now it's a little bit difficult to understand what's going on here. So what I can do is go back to here and will echo a pre tag that's pre formatting ready for output saving. Go back refresh. That makes it a little bit clearer in position zero. We have Mary Position one, James, etcetera. So an array position starts at zero, and that's what you need to remember. It's when you're looping through in a way. Honore, remember that you need to start at Position zero now. The other way to do an array would be something like this. So have cities equals now. We don't need to start with the name array. We can start with square brackets. I'm here. We can put the cities. London, Paris. Let's have a New York. And that's before we can print our run brackets on in here cities. See if that good, my brother and refresh. And there we go. Linden Price, New York with London being in position zero. Now we can actually mix the values in the array types, so let's have a look at mixed. Then we'll put that equal to Ray Room brackets, and this time will have number five. We'll have the color red. We'll have Joan. They will have Lady Three, see if that if it would have printed first. So Prince are They were printing mixed. Save that. Go to the browser and refresh. And there we go 51. Looping through arrays: when we retrieve data from a database, it comes to us in an array. We also have forms which post attitude is as an array. And if you want to use that data on, actually loop through it, then we have to have some way of doing that. So here I am on my editor. I was going to paste in under way here, so it's a rare four names now. What I want to do here is to look through each of those names and to list them in an ordered list. In other words, number one Mary to James, etcetera. So what I need to do here, I need to echo. Oh, hell. And for those of you who don't know hits TML this is an ordered list tag because I've started an order list tag any to end it as well. So let me down here and that tag. This is my opening tag. This is my closing tag. And in here I need to have some list items. So in order list with list items. So the way I do that I need to loop through this array and echo each of these names as a list item and where do that is with a four each loop and then in brackets. This is what I'm going to loop through. And then I need to do something. So for each something to be true, do something. So it's a bit like the if statement, it's a bit like the four loop. So in here, I need to say for each names, In other words, go through the names array as name. So go through the names array, take each name and inserted into this variable here. So the first time we do it will have marry in here than James. Then John, then Sharon Now in here. This is what I want to do. I want to echo a list item open list item. I want to captain it on the name and then concussion it on the closing list item. So we're going through the loop first time round, will have Mary will start a list item and then finish the list item with Mary. Go through the loop and we'll do that for each off. The names in the array may say that Good, my brother, and we'll refresh. And then we have the names in an ordered list, so finish it forward to loop through things in a raise. You'll get plenty of practice of this on, and there are some challenges ahead where you'll have to use this as well. 52. How to split a string and place into an array: the retains will mean need to take a string and to split it on through the split it into individual characters are into certain lengths. And when we split string, we actually place it into an array. So give an example. I started my PHP code block. Let's say I have a word, which is Christmas. If I want to split this into individual letters, I can do the following I can do letters equals string split on. Then here I'll place the name of the string I always to split, which in this case is word. Now, while this is going to do is going to take this word here and split it into individual characters. I'm not sure what I mean by that by printing the result. So prints are because we didn't with an array and I want to print letters and just so I can see things a little bit clearer, I'll echo a pre Tagen as well again. We've done that in a previous lesson that just formats this into a much more readable format for us. I'll say that go to my preview and will refresh and there we go. We split it into individual letters. And obviously I can work with these after that. So might want to work with electric Position six or Position three etcetera. Now as well, splitting this word into individual letters, I can split into certain lengths. Let's say I want to split in lengths off three. I know I can save that. Go back to my preview and refresh and they're ago. I split it into three sections off three. Now the next challenge is actually best on this. So review this video again if you need to, but have a go at the next challenge. 53. Challenge 13: ISBN checker - SOLUTION: So how did you own with E i ESPN checker? It's probably one of the more difficult challenges you've had so far. So what I want to do in this lesson is take you through some of the thought processes I go through when solving a problem. Now, the first thing we need to do on this one is we need to create an input form. So I've opened the haste email template we created in a previous lesson. I'm going to copy this. I'm going to credit you fail ill call its I spn form. Could actually put hates team l on there. It doesn't really matter. This is my spn foremothers pasted my templates under the title. I'll just pop in here I sp and checker, then my body section. This is why I need to put the actual form. So starting my form tag in here. I'll even inputs with a type of my type. Even though I mean putting a number, it will be a text field on with the name. I just called mine ESPN Next I need another inputs with a type on. This type will be submit and we'll have a name on this one off submit? I think so. I put a value in here is well off check. That is going to be my button on. I'll click on check, and it will check if my eye ESPN is valid in the form section here. I'll need an action on my action. Is where am I going to send this to? I'll send it to I s B and dash process dot PHP on my method. My method will be a post method. Okay, so that's my form completed. All looks OK. Think of everything in there need I'll save that. The next thing I need to do is to crit this page here. Copy that, Creative fail piston that name and create. I'll open that fail. Don't start my PHP code block. Now, this is gonna be quite a lengthy process. So on my longer scripts, what I tend to do is just add Cem comments and then I use this to actually structure my code. The first thing lead to do Well, I need to check if the former is being submitted, and then I want to sane my toast. Very bowls. Now, the way this script actually works are the ice bin checker ESPN Checker works is it takes each digit off the ice bien and then multiplies it by one thin two than three. And as those up to get a total. So I need a total variable and I'll need some sorts of incremental next only to split string, because when the string comes in, I want to actually work with each individual digit. So it makes sense to split the string, turn into an array, and I can actually work with each digit individually. I once have got that are a only to loop through the array. And then when I'm looking through the array, I'll need to update the total. And that total is gonna be equal to politic each digits and then multiply it by one and two than three etcetera. And that's gonna go in sequence. So digit one by one. Did you two by two, etcetera. Once I've got my final total. Any to check if divisible by 11 now, I just need to echo at some sort of message. Okay, so that's my thought process here, and I can actually use this to stretch of the code. Okay, so the first thing here is to check if the former is being submitted. So if something is true, do something. The thing of doing in here isn't doing. If is set run brackets inside the Rome brackets. If is set, post square brackets, tick marks and submit. And then in here, Well, in here we put all of this. So cut that, then. Well, pissed it in there. Well, just intend that now I need to assign my post variables. Well, I only got one, and that's going to be the I ESPN. That's going to be equal to post of playing capitals just to maintain. And that's going to be I s bien. It says here, I need a total. So have that. My total started zero. I'll need an incremental er Well, just used dollar I for that, and that's going to be equal to one. Then the two split the string when I split the string it like reading array of digits. So just call less digits and we'll do a string split on what we splitting? We're splitting the I s P m. Okay, we're getting there. Okay. The next thing I need to do is to loop through the array. So we know how to do that. We do for each something. Do something. Okay, now, that's something I'm gonna be doing is really up in the total. Yeah, just this bit here. So grab that and put that in there. Okay? For each. For each. What? Want to look through the array? What? I'm a looping through him looping through these digits. So for each digits as digit, So go through the digits of Ray. Take each value on a sign it to this variable here. Okay, So now need to update the total. So total equals. Well, we wanted to do here. I need to take each digit and multiply it by one, then two, then three. So this is where my incremental comes in. But I need to keep updating this total, so it's gonna be equal to the previous total plus than you answer. And then keep doing that. So I need to do a plus equals. Then it's in my digit. I need to multiply it by the incremental. So the first number through I'm gonna make the total equal to the first number times one. Then the totals going equal the old total Because of this, plus the second digit times to etcetera, etcetera they're thing left to do Here is I have to increase this incremental by one each time. So it's dollar I plus plus. Okay, nearly there. Check if it's divisible by 11. Okay, so we've got an if statement going on here. So if something is true, do something. Well, if it's true, true, I'm going to echo valid. Oh, else I'm gonna echo invalid. So is probably in there. So if the total module ISS 11 is equal to zero now, we've done that a few times that of the previous challenges. So hopefully you happy with that would take the total we divided by 11 and we check the remainder. So this gives us the remainder. If that remainder is zero, then it is a valid number. So any to peco valid else. Do something and that's something we're gonna do. Is echo invalid? Save that. Just close this applet. Okay, there we go. That all looks to be okay. Can't see any errors, so we'll save that Will goto our preview Refresh. I asked. Be informed. There it is. Let's see if I've got a number we can check. I've got one there. Copy that and pissed it in. So this should be a violent one check, and it is valid. Okay, That was a fairly lengthy process, but hopefully understood my thought processes. And you can see the way these comments actually really helped to structure the code. So hopefully found that useful. If you needed to watch this video before trying it yourself have ago now, and see if you can come up with the similar sort of solution. 54. Challenge 14: Mind Your Language - SOLUTION: Hopefully that challenge didn't pose too many difficulties. All you had to do was take a sentence on remove the bad words. So what we're doing, we're creating a very simple bad word filter. Now, here I am, over in my pitch here of credit, my PHP code lock. And the first thing to do is really creative variable called string. And in that string I want to actually pissed in the sentence. So we gol fish with my semi cola. The next thing is a lead. An array of bad words so set up a bad words array put the equal to an array. And in my array, I'll put my bad words. Let's have bad word one. And we need bad words too. Okay, Next thing I need to do well, any to loop through each of these banned words and go through the sentence and replace each of these bad words with four asterisks so looping through the rays, Freddie straightforward we have for each something do something. Okay, so in here we have each of the bad words as a bad word. So take each of these in the bad words of Ray and to sign it too. This variable here now in here. This is the action that I'm going to take. Well, I want to do I want to update this string by removing the bad words. So the string hoops the string becomes equal to Well, how do I replace anything in the string? I use strength. Replace now in here. I have three conditions or three items in here. The 1st 1 is the thing I'm going to look for. Which is a bad word. The same going to replace it with which is the four stars or rust risks. And then what? I'm actually searching. I'm searching string. The last thing to do is I'll just echo at the new string. So say that that looks fine. Go across my browser and refresh. And we've got undefined variable, bad word online. Eight. Let's have a look. Laying it on. I should have a capital did living there. Some of you say that Go back and refresh. And there we go. We've replaced the too bad words with the asterisks 55. How to sort data in arrays in PHP: when working with the Rays, we quite often need to sort the values in the array before we present them to a user. So let's have a look at how we do that. I'll start with my PHP code block. I'll have an array with some names in. So let's have in here. We'll have John Mary gyms. The sound of him. Me, no, I don't need to do is a need to sort this names array. And there is a sort function. So sort on one of my sorting. I am sorting names. Okay, So only to do now is need to loop through each of those names in the array on what I want to do. I want to present them as an ordered list. The first thing I'll do is I will echo an O. L tag. Now, this is a HTML tag for ordered list, and I've opened the ordered list. I need to close the ordered list. Okay. And now in here, this is where my loop will go. So for each something, do something. Hi, I'm here. Well, I'm taking each of those names in the array as name. So take each the names in the array. Undersigned, to this variable here called name No, I need to do is only to echo only to echo a list item can captain it on the name and then close off that list item, Finish the lane with a semi colon on and safe. Go to my brother. Refresh. And there we have the names in alphabetical order with the ordered list. Now what I can also do I can also reverse sort those names. And that is very easy to do. I just do on our sort so reverse sort I can save Go to my brother and refresh. And then we have the names in reverse order so fairly straightforward and hopefully something you'll find useful. 56. How to explode and implode strings: now, at this stage of the course, hopefully you're seeing the power off array on it. How actually connects quite well with use off strings on, especially when we deal with CS. V fails. Now, for those of you that are unfamiliar, these are comma separated values. And let me give you an example of how this works. So start off with a string and in my string, I'll just pops, um, names in here. So have John. Sarah can Ban and Amy. Now those names are separated by Commerz. So, in a very simple way, this is actually a C S V fail. Now, what I need to do I want to do is to convert this into an array and my array will be called names. And to convert this into an array, I explode it. No, I exploded at a certain point, and the point at which I explode is called the D limiter. And the deal emitter in this case, is this best on the commerce. So comma space. And then what am I exploding? I am exploding. The strength now I want to do is I want to print that to the screen. So prints are. It's an array Sui's print are, and we're printing names on just to make things a little easier to read again. L echo a pre tag Say that good on my president and a refresh, and there have my array off names. Now that I have exploded these names, I can actually implode them again to bring them back into a single string. On the way that's done is as follows. Let's call this imploded, so it's just a variable him, and that will be equal to implode. Now I need my glue on. My glue is whatever character I'm going to use, So my glue will be a pipe character and I'm going to implode the names array and again I'm going to echo and I'm going to Echo imploded. I'll say that. Goodbye, presser. Refresh! Now you can see we've got the original Ray Ah, now I've imploded that with the pipe character 57. How to add elements to an existing array: When working with the Rays, we often need to add values to an existing array, and we can do that quite easily with a ray push on array on shift and let me show you how those work. I'll start with an array cold cities and in my city's array. I'll have Lyndon and we'll have Paris. I can actually echo these to the screen, so I'll start with a pre tag and then prints are on one of my printing and printing cities . Say that good, my browser and a refresh. And there I have the two cities in the array. Now, if I want to add anything to this array, I can use the following. I can use array, push, Andi. It takes the cities argument. And then in here I can put the names of any of the city's I wish to add. Let's have New York and we'll have Berlin and then we'll doing here. Our prints are again off. The cities say that go to the browser and will refresh. I know we have Linda and prices before, and now we varied those to you cities. Now that I did this, it is to the end of the array. What if you want to add cities to the beginning of theory? Well, this is where we use array on shift and since for matters before we start with the cities and then we say, Well, what do you want to add? Let's have Amsterdam colonel spell there, and we'll also have Let's have Dublin. No prints are again there were printed in cities. Say that's good. The prez er and will refresh So ago. Those two cities have been added to the beginning off the array. 58. Challenge 15: Around the world in 80 days - SOLUTION: This is the video solution for the around the world in 80 days Challenge on. Hopefully you managed to complete this one successfully on We'll have a look at the solution here. Now what I've done, I've actually commented at two lines here. I've gone ahead and just added those city names in just a save a little bit of time. So the way I approach this was as follows I created my array on called its cities on what we can do. We can call Marais by using the word array and then round brackets. Or we could just go ahead and use square brackets. So just to be a little different this time, I use square brackets. I'll copy this first line Onda pasted in. It's enough credit, Mireille. The next thing I must to do is to sort these alphabetically, and I just sort on one of my sorting. I am sorting cities. The next thing I must do is to actually display the cities as an ordered list. So to do their only to echo. It's on O l tag for ordered list, and I'll close that as well. Okay. And I'm here. I need to display the list items. I need to look through each of these cities here, so user for each loop. So for each something do something. And in here I'm going to flip through the cities as city. So take each of these ascending to the variable city and then we'll do something with them in here. I don't want to do in here. I want to echo echo a list item cabinet on the city and then have a closing list item. Go ahead and save that. Could my presser and refresh and then we've got the list as required. The next thing I must do is a must to add these names here to the array. Do that with an array push in. Here I start with the rate Self cities and then I add the city names. Copy that and paste in. After that, it says to sort the city's alphabetically. So let's sort them again. So sort in round brackets. I have cities and then it says to display them in order to list. So what I want to do, I want to do all of this again, so I'll copy that. Oh, pissed in there, I'll see if that and refresh and there have the two less the originalist and then the one with the extra cities added Now, obviously, this isn't the best way to achieve this. I would probably put this into a function. We haven't covered functions yet, so hopefully see, when we do core functions, what we could do to rip less this block of code. What we want to try to avoid is repeating ourselves. So we use that technique dry, don't repeat yourself. And obviously we have repeated ourselves here on and here. 59. Removing elements from the beginning / end of an array: There are times when it's useful to remove one element from an array at a time. And we can do this from the beginning of the array or from the end of the array and let me show you how that works. I'll start with my variable cities on a popular equal to an array and all pissed in some value. Some cities. Let's say I want to remove the last item in the array To do that we use array pop. So I'm gonna call this city. Let's call it the last city. Let's put that equal to array pop on in here. We just placed the array that we are doing their own another. I've done that. I can actually echo at that values. Let's echo at last city. I know I could in a break, but break tag, Andi, I'll also print the array as well, so you can see that that value has actually bean removed. Okay, we save that. Let me go to my browser and refresh. And there you go. We've got New York is being removed. We can see the right here with the three values. So New York here was removed from the end of the array. Let's say I want to remove the first value from the array. To do that, we can say, Well, it's college, the first city I will put that equal to array shift. So that's the function we need to use to remove the first element from the array on. We're removing that from cities. Let me echo the first city just to show that does work. Well, I co a brick, and then we'll prints are off the cities. We say that I'm just echoing a break here as well. Okay, say that. Go the brands it and will refresh. And there we go. So we removed New York from the end when we printed the array, We remove London from the beginning and we printed the ray. And we've only got two cities left in the array. So array pop, Onda ray shift to really useful functions to use 60. How to use array_slice() to split an array: we've already seen that we can remove on element from the end of an array on the beginning , off an array. Well, we can remove multiple elements from the ray using array slice. Let me show you how that works. I'll start with an array of letters, pace and values in here. No, what I want to do here is I want to remove the 1st 5 elements from the beginning off this array and then print the result. The way I do that, let's say the result will be equal to array. Slice. So that's the function in here. I pass in the Aram working with which is letters on How many elements do you want to remove ? I want remove five. So all I need to do now is just print are No. I just need to print out that result, and what we're doing here is Well, I'll just echo in a pre tag. There we go. I will say that Go to my brother Henry fresh, And then we get a list with the 1st 5 elements that have been removed. So don't it this way removes the elements on leaves the remainder. Well, what if you want to remove the five elements and actually print those to the screen? What we can do this time, we can say the result is equal to a race slice. We still pass in letters. Now, this time I want to start a position. Zero. I want to remove five elements from the array Now, then print the results. You copy that? Pissed that in save and I'll refresh. There we go. We've got the original Ray, and then we've removed those five elements from the first array we worked with, So remove those five. Okay, Well, what if we want to remove the last five from the array? Well, the results will be equal to race slice again. That would pass in the original letters array. Now, this time, we don't know what position we're starting at. We don't know how the letters are in the array. We know. Position zero. So if you went back, one would end of a position minus one, which would be equivalent to this one. So if we go to position minus five and then remove five from the array, that will actually return those last five values from the ray so again. Let me dio. Prints are of that results we will save. We will refresh and then we go. We have M and L P and Q, the last five letters from the array. So that's a race Life's. We can remove multiple elements from an array on. We can remove them from any position on any number off characters. So have a look at the PHP dot net manual. You'll find some more examples in their Onda really useful little function to know. 61. Challenge 16: It's getting hot in here - SOLUTION: Hey, everyone will. How did you deal with that challenge? Health. It wasn't too difficult on you. Nice to get some way through it. If no, all the way through it. If you did absolutely fantastic. If you didn't not to panic. Here is thesis a Lucian. What I'm gonna hadn't done here is I've got this commented Lane with the temperatures in ready to go. So I started off with an array called T to represent my temperatures, and I just pissed in there. Those temperatures, the first thing I must do is to find the average on in order to find the average l need the some of the temperatures. So at the mall up, I need to know how many there are so need to count them. So let's start with some and some will be equal to the function array some, and we find this, um, off the temperatures. The next thing is, I'll need to know how many there are, so I'll have a variable called current again. We'll use an inbuilt function called current, and we're actually counting the temperatures. From then on. It's quite easy to work at the average because the average is going to be the sum divided by the count, then only to do is to echo it a little message, so echo the average daily temperature is, and then just concussion it on here. The average. Now what about to do in the task is to actually round the average to one decimal place, so we'll need the round function. Have a place that like that with one decimal place, so that takes care of the average on the next day. Only to do is to work at the five lowest temperatures on five highest. And in order to do that, I will need to place these temperatures into ascending order. That's quite easy. I just sort one of my sourcing, no sorting T now to find the lowest temperatures. Let's do that. So the lowest well need an array slice on one of my slicing. I'm slicing the temperatures. I'm starting a position zero, and I want five of them, so that's going to return an array With five elements in and what I really want to do, I'm going to be echoing a message to say the five lawyers temperatures are well, I'll need that right into a string that can work with. And to do that we actually implode, so the lowest will be equal to implode. The first thing is the glue. So need a comma in a space. And then I'm doing this on lowest. So what I'm doing here, I'm taking the lowest array. I'm imploding all the values with a comma in a space, and I'm assigning that back to lowest. Okay, Next thing I need to do the highest. And that's going to be a ray slice something. I'm taking tea on this time. I need to go to position. Negative five on return. The five values again highest, will be implode. On what? My imploding. Well, I need my glue, which will be the calmer and space and then highest. Okay, so beginning. That's the last thing to do. Is Teoh echo out a little message? So the five lowest temperatures are Can Captain E on lowest on a little message to say, the five highest temperatures Har Wilken cabinet on highest and just in between their pop a little break, echo pr and save go to the browser and will refresh okay, so that the average daily temperature is 30.4. Let me put a little break in here. Save the hat, and we'll refresh again on Dhere. We go. We've got the average daily temperature. We've got the five lowest on. We've got the five highest. That was quite a lengthy little exercise. That one. I hope you might get it done, but if you didn't, don't panic. I would recommend you go back, review some of the previous lessons and then have another go. 62. How to select a random element from an array: selecting a random element from an array is relatively straightforward. And let me give you an example. We'll start with our cities array again. And in here I'll just pissed in those cities. If I want to select one myself on de quitter, I could do something like this. And in square brackets, I just like the position. So position 0123 and four. If you call in the Rays, we always start from position zero. So if I want to echo at position one, let me say that good. My browser. I should get Paris and there we go. If I want to slights an element at random, I can do the following that conflict array, rand. And in here I just tend to the array, which is cities. Let's say that this provides me with the following, so I'm assigning this selected one at random to this variable here. Look, and then do I could just echo out City. Let me just comment this one. Okay, so if I go ahead and preview that one, it returns a number. That's what it's actually doing. It's returning the position. I've already seen that to get the actual city name. I need to do something like this. So if this is returning the position, it's a same. Is this here? So, in order to get the name of the city, I need to do the following. We'd have cities and then in square brackets, I have city. So if you're called, this is returning the position. Let me say that and refresh. And there we go. We've got New York, I'll be fresh again. So it's selecting a city at random. 63. Challenge 17: Pick a card, any card - SOLUTION: This is the solution for the pick A card, any card challenge. And they actually thought this was a nice little challenge. It's got the makings off a little game you could produce, and I'll show you how I actually solve this. Now the first thing I did was to set up on Array on. I had a suits array on did in here. I just placed in the different suits. So we have hearts and then we have clubs. We'll have diamonds. The last one is to have spades. There's the four suits in a pack of cards, then have the values of the cards. So another array on the 1st 1 we can have would be an s. Let me have a 23456 9 10 in the last three. Jack Queen, The last one is king. That takes care off Theseus on values. Now I don't want to do is I want to select a suit on a value at random from each of these rays. I do that with array Rand, and we'll do the suits first. So suits now what this will do, it will actually return the position. I want the actual value. And we saw in the previous lesson or one of the lessons that we do the following take the suits and we put this into square brackets. So in other words, I want you to retrieve this position in this array and give me the value on Dalla saying that too Suit, The next thing I want to do is to do a similar sort of thing for the values. So I was going to copy this, paste it in. I'll choose this to value Mr Values and this 12 values as well. Okay, so that we should retrieve a random my suit and a randomized value on the last thing I need to do is to just say what the card is. So the card is going to equal well, value Cassin it on little message off and canned. Catherine it on then the suit. Okay, so that's the actual card. And I could just echo that. So eco card say that Go the browser and will refresh. So we get the king of spades. King of diamonds, Ace of diamonds, queen of spades, six of diamonds, etcetera. So it's actually picking a card at random on health that you can see how we could actually develop this a little bit further into a little guessing game. 64. Challenge 18: Let's go shopping - SOLUTION: This is the video solution for the Let's go shopping challenge, and we're probably saved the best to last will certainly the hardest toe last. But let me just show you how actually tackled this. The first thing I've done is accrediting a shopping form Pidge, and have also created a processing script, not run through this in a second. I have also opened the haste email template that we created in a previous lesson on one of words Didn't going to copy that. I just go ahead and paste it into my shopping form page for my title. Let's have credit card checker in the body. This is where I need to create my form. So start with a form tank. And here Lee didn't inputs with a type being text on the name being listens for my credit card number. So I'll just put Sisi for credit card. The next input type will be for the submit button. The name I'll just have us submit. Last you need to do here is indeed an action for my form on the action is where my sending it to. I'm sending it to shopping dot PHP. My method will be a post method. Okay, so that forms all done and everything looks fine. Let's head across to the processing script on. I'll show you often hear. So what I've done is I've actually planned at TheStreet Chur off my solution on. I have done this in a previous example as well, So I started my PHP code block and then just added a Siri's off. Comments on these comments Lift Lee are my thoughts. My process is wana intend to do in the solution Now I suggest you do something similar whether it be a little diagram, a flow chart or even something like this where you write in a few comments now what I do is I change each of these comments into a lane off code. So the 1st 1 here is check if the form has been submitted. So if something is true, I'm going to do something. And in here I need to check if is set, then round brackets. And in here I'm checking if the post square brackets tick mark and submit and then split this open. So that's going to check if the submit button is being pressed, what I need to process is all of this, So I'm going to cut that and paste it in here. The next thing I need to do is to assign my variables. And if you have a note in these brackets, have said, I also need a total and an incremental so probably dollar I. Let's start with the actual credit card number that would make sense. So number equals post and then square brackets, tick marks and CC. So what this means is take the user inputs under Sign it to this variable here, and that's going to be a string. The next thing is, I need a total one of things I need to do in. I think it's step five or six is I need to check if that total model is 10 equals zero, so definitely the total. And I'm also going to have to look through all the digits in the credit card, so probably need an incremental as well. So I'll have I in there the last spring at the message. Whether the card is valid or not, the task actually acts asked me to include the last four digits off the card. So before I start messing around with this string. I want to retrieve those last four digits. I'm gonna call this last four. Gonna put the equal to a sub string off on my sub string. Well, it's going to be number. I want you to go for from the end and retrieve four numbers, so it takes care of that. Next thing I need to do is to split the string into an array. What's fairly straightforward will take the number, and that's going to be equal to string split again. This is something we've done before. So string splits And in here when we split saying we're split sing number they think I need to do is to actually to reverse this array. And that's step two of the process. So that's a look. Number well equal. Teoh Array, reverse. We'll be reversing. Reversing number. Okay, the next thing I need to do is to loop through the array and performed the calculations, so this could be a little bit tricky. So take the time on this. So I need to have a for each something. Do something. Okay, Now, there's something I'm going to be doing is all of this so copy that pop it in here. Okay, what's my check going in here is going to be for each number as let's call this digit. We're taking this number array, and we'll break it down into digits. Okay? The next day I need to do here is check if it's going to be an even digit or an odd digit on the where do that is through my income. Enter. So in here, I need to say some. Like, if something is true, do something. That's something we're going to do. Is all of this? Yep. And put that in there and in here. I need to check if this incremental I is an even number. So don't I Module iss to is equal to zero So if that is true, it must be an even number. I've got no remainder when I divide by two And if that's true, then I'm going to take the the digit Onda Multiply it by two But I'm gonna keep track of that. So did you equals digit times by two on the shore hunt for doing that is digit ah, times equals to Okay, that's very straightforward There. Now, when I have taken that digit on multiplied it by two. I need to check if the digits is greater than nine. And if it is only to subtract main. Okay, so in here, if something is true, do something. Okay. So if the digits is greater than nine, then I need to subtracted Lane. Let's do that. I need to say digit minus equals lane. Okay, so we're getting there. So the next thing I need to do is to find the total. So in here, I have total and that's going to be equal to the old total. Plus the digits on the way I do that is plus equals digit. Next. I need to increase the incremental by one. So that's dollar I plus plus semi column to finish. We nearly finished. So the next thing I need to do is to check if that total is divisible by 10. So if something is true, do something on this. Something I want to do is to display a little message. So put that in there. Now the check. If the total is divided by 10 I have total module. ISS 10 is equal to zero. And if that is true, then I want to display this message on If you're called the message we have to display. Here's the last four digits off the credit card and whether it's valid or invalid on, Obviously, in this case it would be valid. So will echo and something like your credit card number ending in, and we'll concussion it on here the last four. And in that case, it is valid. Els. We'll do something else, and if it's not valid, then it must be invalid. So copy that. I'll pissed it in here and just change that to invalid. Okay, so now the script has actually finished. The last thing for us to do is to check if it's working. And if you go to the actual task itself, we have some valid and invalid card numbers, So let me take a valid credit card number. I'll copy that. Go to the form, pissed it in and submit. And there we have that the credit card is valid. I'll go back. I'll take an invalid card number. Copy that paste that one in. That's admit on this one is invalid, so hopefully you got the same results. If you have any questions, please feel free to either send me a private message or post them in the discussion forums 65. How to understand the structure of an associative array: in the last section. We were looking at basic arrays on. We worked with some of the functions on a raise and in this example here I have an array of cities and I can print out those cities and get a preview. Something like this. We now move on to look at associative arrays on. Let me give you an example of how they work. In this example, I'll use students and I'll turn that into an array. And again, I could use square brackets, as in this example Oregon Type in the word array and use round brackets. So, in this example here, I'm going to start off with a student name of Sam. And Sam is going to be 12 years old, then have Holy and Holly is gonna be 13. Then we'll have John. Unless that Joan is nine years old. Now you can see the difference Troy straightaway between this array and this one, what we've got here is we have a key Andi value and in between we've got this equal sign on greater than so we're assigning this value to this key. Here we have another key and another value. And then under the key and under the value. Now it's open this array. We can do the same thing. We can print our, and this time I'm going to be doing students. Let me save that, go across the browser and preview. So in the basic Array, we had this zero index 123 etcetera in the associative array. This is the key on. This is the value, and we have a series of array functions that we can actually use to work with these arrays , and that's what we look at in this section. 66. How to loop through an associative array: in order to output values from a standard array, we would do something like use a for each loop. Let me give an example on this one here. We would probably say something like for each something. Do something, then here would have cities as city. And then here we just echo out City. I want my even depend on their Elaine break. Okay, Just let me, ah, comment at this section here. I'll save that. Go to my browser and I'll refresh and they would get a list off the cities. Now, if I want to do the same thing with an associative array, I'll need to do something like this. So again, I can use for each loop. So for each something, do something. And here I can put students as there was before I put city here. I have two pieces of information. I have the key, and I have the value. So I could actually say key value like that. But because I know these are students on these ages, I can actually name these appropriately to be students. Andi age. So as you can see, it doesn't really matter. I call these 30 miles. Will be something appropriate and I'm here. All I need to do is to echo at the results. So I'll say something like the students have taken this value here. Some of the words first will be Sam. So Echo Student is let's have edge on and here's old and then he I just depending on the break, Aziz. Well, no, save that good, my brother and refresh. And then we've got some 12 years old, etcetera. So to look through values in an associative array, the only thing you need to remember is to include the key and the value. 67. Challenge 19: Capital cities of the World - SOLUTIONS: Hey, everyone, This is the video solution for the Capital Cities Challenge on hopefully found this one fairly straightforward. There was a little bit of research to do on the sorting functions, but apart from that fella, straightforward, the task itself actually give you a list of Capital Cities and the country's. So I'm going to copy that. Gonna go into my page here on the first thing I'm going to do is set up in a ray called Capitals. It's going to equal in array, so put in square brackets on piston those values and finish with a semi colon. Now, the next thing I need to do is I need to sort these by the capital, I'm sure, is what it said. Let me check. Yes, So yes, or sort by the capital city. So to do that, we use a sort and then Rome brackets, we place capitals that will give us this array sorted by the capital city. No, we need to do is to actually loop through these aunt to echo at the results. So we need a for each loop. So for each something, do something. And in here for each capitals as well This is the country and this is the capitals. Let's use that So country Simon, operator and then capital And in here we just need to echo a little message and the message waas the capital city off and we put the country and then the capital. Let's do that. Echo the capital city off. Need to Catherine it on the country and then we need half is and then can capitalise on the actual capital. I'll put a lane break in here as well and finish off my semi colon. I will say that Go to the browser and will preview And there we go. We've got the capital city off Netherlands is Amsterdam. So so works really well. So hopefully you nice to do that. Didn't pose any difficulties. If you do have any questions, then just go along. Pop them in the discussion area 68. How to create a function in PHP: creating a function in PHP really is straightforward. Let me give you a quick example here. Let's start with the word function. So all functions start with the word function. Then we give our function and name. I'm going to call my function Hello and then we have run brackets. We then have curly braces on inside the curry braces. I placed the actual function itself. So for this function, all I'm going to do is echo Hello world. So echo Hello World and semi cola. So that is our function finished. If I was to say that good of the Preiser on a preview, it's refresh. But nothing appears on the screen. And the reason for that is I haven't actually called the function. So to call the function, all I need to do is type in Hello. In other words, the name off the function and I use my round brackets as well. I'll save that. Could the browser and refresh and there we have hello world. So we start with the tone function. We then have the function name on inside the curly braces. I have what I want to actually do in the function and to call the function. I simply call the name off the function 69. How to use variables in PHP functions: in this lesson, we cover the use off variables in functions. Let me start with a very simple example. So functions and our function is gonna be called ad round brackets and curly braces. And we split the curly braces, open guns, have a variable called a put a equal to one very well, be equal to two and then I'll have, ah, variable some which is equal to a plus B and I'll simply echo some. I will say that so now I need to do is to call the function, and we do that by simply having the function name round brackets. Semi cola will save that. Gonna my preview and refresh. And there we have the total of three. Now the variables we've used within the function are only available in the function itself . So, for example, if I said something like Echo a semi colon, if I save that, go to the browser and refresh, you can see we have an error. Verbal A is undefined. It's undefined because it's only available within this function here. So let me go ahead and remove this line here on Let's say we had a variable see, which is equal to five. So this variable is defined outside off the actual function itself. Let's say I wanted to add a and C together so some equals a plus c Let me say that unless refresh and he see we've got an undefined variable See Because C is defined outside of the function, the function cannot see the variable. See, that is unless we do the following we say global, see another function will look out side the function itself on Actually look for variable See it will see that C is equal to five and therefore it's available anywhere else in the function. So now if I save that, go to my preview and refresh we get a total off six So men points from this If we have a variable defined outside the scope of the function we must include the global term if we wish to include inside on any variables defined inside the function are not available outside of the function 70. How to pass arguments to a PHP function: in the last couple of lessons we've seen how easy it is to create functions and also how we can call the functions now what we can also do. We can also pass values to those functions, and he's called arguments and it works a little bit like this. We start with the word function on. We'll have our function name on will call it, multiply round brackets and curly braces. So in this example, what I want to do is to multiply two numbers together on rather than declare my variables here. What I want to do is actually to pass those values to the function. Now what I need to do in here in the wrong brackets is actually get ready to receive those values or arguments. So my first argument is number one on my second is number two. Now what I want to do with those two numbers is to multiply them. So let's say the result is equal to number one most played by number two, and now I want to do is just to echo that result. Now I need to do is actually call that function, so we have multiply from brackets and semi colon, but this time inside the room brackets. I need to pass along to values and I'll use 12 and five. So I should get results off 60. Okay, if, say, if that let me go to the browser and will refresh and then we get my results off 60 now the nice thing is I can echo in a break here and I can actually call that function again. So multiply on this time I can choose to different numbers. Let's have three on nine. I'll say that refresh. And then we get 60 and 27. So two summaries. We have the arguments placed inside the run brackets and we pass along values via the function. Call itself. So here I have my two values being passed along their picks up here and then used within the function itself. 71. How to return a value from a PHP function: as well as passing values to a function. We can also return a value from a function. So in this example here and let me start with my PHP code block on a function and call it a per case. Now what this function is going to do is going to take a string. It's going to turn that string into uppercase letters, and then we're going to return the string. We'll have room brackets and my curly braces and in their own brackets. Let's have a name in the current presses. What we want to do is we want to take that name for you and make it uppercase. And the way we do that is we use the function string toe ever. So let's have the result equal to string to a pair on. What are we taking? We're taking that name string. And then what we want to do is we want to return the result. No, we have to. Do we have to call that function. So the function name is a per case and we want to pass along a string. So my string is going to be Smith, and clearly we need to capitalize all of these letters. We've got one capitalized beginning, but I want all of them to be capitalized. So currently, what's going to happen here? We're going to hit this page. Nothing will happen until we get to hear. We'll call the function passing in this string. Smith. The function is being called. So we go to here. The function of a case will pick up that string. Well, take the string and make it uppercase, with the result being stored in this variable here. And then we return that result. But nowhere have we actually echoed the result or done anything with that result. So what we can do here in front of here, we can simply say echo, help! A kiss, Smith. Let me say that. Go to the browser and refresh. And then we get the result off Smith. It's all being capitalized as we intended. Now what we need to bury mind is that within these functions, we can only return one result. Let me give you an example that sad uppercase Smith and I had a 2nd 1 which is Jones? No, my function. I can have name one Onda name too. My result one could be strength upper of name one. Don't you pick it that and we can result to And then to now it seems logical that I could return result one and also return results, too. But that simply will not work. So if I say that, go to the preview and refresh, you can see it's only returned Smith. In other words, it's returned the first value and has not returned this second value. That might seem a little bit strange to you. And why can't we do that? What we would need to do is to actually build up an array and then return the array and then work through the array and echo the results that way. Now that is beyond the scope of this lesson. But I just wanted you to be aware that we can only return a single value from a function 72. How to return multiple values from a PHP function: we saw in the previous lesson that it's only possible to return a single value from a function. But what if you want to return mawr than one value from a function? Let me give you an example of how this might work. I've created a file called Returning multiple values dot PHP have opened that file, and I'll start with my PHP code block. And in this example, I'm going to get a function called Calculate on this function called chocolate. I'm going Teoh take two values in the past two values in the 1st 1 Being number one on the 2nd 1 is number two on what I want to do. This function is to add those two numbers together and to subtract two numbers and then return that those values. So the first thing is to add that would be number one plus number two, and then I want to subtract. That's going to be number one minus number two, and then I want to return both of those values. Now, as we've seen, we can't do this. We can't say return ad and return Subtract. That doesn't work. The only thing that would be returned would be this add value here. So the question is, how do we return mawr than one value? Well, we already know that we can have multiple values stored in on array. So what we can do here, we can actually return an array and we can return Honore holding those two values. So add and subtract. No, I need to do is actually call that function. So my results will be equal to calculate so calculate and have to pass in two values. The 1st 1 will be 10 2nd one will be three. So what we're doing here we're calling the function were passing in 10 and three, 10 and three will be picked up here by the function. We will then add those two numbers together. Still the result in ad We also tracked the two numbers store that result in subtract and then return an array with those two answers in that returned array will be stored in this variable here called results. So let's see what this result array actually looks like. We can Eckert a pre tag and then we can actually echo out. Or in fact, let's print are off results so put results in here. Say that. God, My preview on returning multiple values. We can see a in position zero. We have the result of adding the two numbers and in position one in the array, we have the results off subtracting. So to access those two results, we simply need to echo, weaken, echo results and position zero that will give me the addition. Results are then echoing a break and then we can echo at the following week. Knicker results and we want position one. So if I saved that, remember, we've got 10 and three, so we should get results of 13 and seven. Go back here and refresh. And there we go. We've got results off 13 and seven. 73. How to create a cookie: in this lesson, I will show you how to set a cookie. Here I am in code anywhere. I'll right, click and credit. You fail on this file, I will call set cookie dot PHP. I'll go ahead and open that fail on. The first thing I want to do is to start with my PHP code block. Now there is a built in function called set cookie. Let's call that run brackets and semi colon. Now I need to pass in two arguments here. The 1st 1 is the name of my cookie on In this particular example, I will use could he called fruit. So let's say I'm storing my favorite fruit. The next bit of information I need to store is the value, and in this particular case, my value is orange. So let's say my favorite fruit is orange. I will say that I'll go to my preview, do a quick refresh. There's the Fail. I'll open that file. Another cookie has bean set. Now, depending on your browser, you can actually view your cookies in chrome. I can go to this address here So crow Coloane double four slash settings for slash cookies . A press enter. This brings up a list of all of my cookies. I can do a quick search for the cookie that I'm looking for. Well, I know this came from this year. Ellis Preview your l So I just do a quick search for a preview, and there it is. Now click on that and I can see that one cookie has been set. The name of the cookie is fruit. When I click on that, I can see the content off that cookie. In this particular case, the content is orange. I can see the u R L that it came from, and I can also see the created debt on the expiry. Now, in this particular case, the cookie will expire when I end this browsing session. Now, one thing I can do in my cookie when I set it is to pass in. 1/3 argument on that will be the expiry time of the cookie. And the way I do that is as follows. I take the current times. That's a function, and I add on the number of seconds until the cookie expiry expires. In this particular case, let's say I want the cookie to expire in 10 minutes, 600 seconds. So get the current time and add on 600 seconds. I'll say that now. Just quickly. Refresh this to reset a cookie that's been set. No, I can actually refresh. This is my preview as the cookie fruit. I want to click on that. I can see that it expires in 10 minutes. Now there are other arguments you can pass into your cookie, but I'll leave you to explore that in your own time because this is an introductory course will keep things fairly simple. If you want to know more, you can go to pitch p dot nets on. Just do a quick search for cookies. 74. How to read a cookie: Now that we have set a cookie, we need to be able to read the data from that cookie. So here I am in code anywhere, once again and I'll credit you file. I'll just call this read cookie dot PHP and I'll open that fail. I'll go ahead on start my PHP code block on. All I need to do to be able to read the cookie is to use the global variable dollar underscore cookie in square brackets, tick marks. I put in the name off the cookies, which is fruit. I'll face that, like this semi colon I wouldn't want to do here is I want to actually echo the value of that. So put in Echo Amel echo at the value off that cookie, I will say that good, my preview and refresh. And there we have the file. Now, before I actually read the kooky Let's go ahead and set the cookie once again so the cook has been set. I'll go back and then we can read the cookie and there's the value from the cookie. Now let's go across to the actual cookies. Just refresh this page here on. We see there's the cookie fruit. I can click on that. We can see the value is orange and we can see the 10 minute expiry. Now, what if this cookie has not bean set? I'm just remove this cookie that's gone. I'll try and read the cookie again on I get this error message. Now, that error message occurs because the cookie has not bean set, but we are trying to actually read it. So we can do here is we can pop in a little if condition. So if something is true, well, do something. What we want to test in here is, if is set, so has something being set on. The thing we're testing is the cookie and the cookie called fruit. So if that cookie has bean set in here, we can actually echo at that value something. Grab that and paste it in there else we can do something else. How we can just echo at something like Cookie has not bean set missive that go back the preview and refresh that we can see the cookies not being set. Go back. Let's set the cookie on the let's read the cookie and then we get the value orange 75. How to update a cookie: Now that we know how to set on read a cookie, we may be required to actually update value in the cookie. So credit new file. I'll call it update cookie. Stop PHP. I'll go ahead and say that I'll open that fail. I'll start my PHP code block. And this is where I want to update the cookie. Now we don't have an update function in PHP to update a cookie. What we need to do is actually rewrite the cookie. So I will do a set cookie and in my set cookie command again, I'll name a cookie fruit, and this time I'll pop in a different value. The value being apple. I'll set an expiry time again off 600 seconds as 10 minutes. I'll save that. Now go back and refresh and you can see I've now got this update cookie file. Well, let me go ahead and just set the original cookie that should be set as orange and we'll go ahead and what I'll do here, I'll just refresh this. I go to the cookie. There it is on. Just check this fruit cookie on the content is currently orange. Let me go back and go to update cookie. That cookie should not be set as Apple Game will refresh. Here we'll check the preview. Go to fruits on now we can see the content is set as apple. 76. How to delete a cookie: Now that we can set read on update a cookie. The last remaining thing to do is to be able to actually delete a cookie. Let me go ahead and credit you file. I'll call this one. Delete cookie. Stop PHP and I'll go ahead and open that file. I'll start with a PHP code block. And obviously, in this file, I need to actually remove or delete that cookie. Now, there is not a delete cookie function. So what we need to do is try flee set to the cookie again. Which cookie are we setting or deleting? Is the fruit cookie I don't want passing a value. So leave as no. And the third piece of information is the time. And we subtract a number of seconds and I'm going to go for an hour. So I'll do 3600 seconds. That's one hour. So you see what I'm doing here? I'm resetting the cookie, but I'm telling the browser that this cookie should expire an hour ago on the browser should therefore remove this cookie. Let me say that Go to the preview on. We'll just set the cookie. Well, just make sure that that has been set, so go to cookies. There it is. The fruit cookie is set at orange on the expiry. Time is in 10 minutes. Now go back, and I actually just refresh on. Delete the cookie. Now let's check if that's actually being deleted, so just do a quick refresh here. We'll check if it's six in existence. Preview on it's actually being removed. So by setting the cookie with a time in the past, were actually deleting or removing that cookie. 77. Challenge 20: All about me - SOLUTION: This is the solution video for the all about me task on in this task, you had to create three separate fails a form process and retrieve in the form dot PHP file . You had to create a simple input form for a name, age and favorite color with then had to process it and then retrieve that data. Let's go ahead and have a look at that. I've created a folder cold all about me, and I've created those three fails We've got form, we've got process and retrieve. Let's go ahead and will create the former first of all. So start with my doc. Type html html tag my head tag. Help a tight Lynn here that might tyto all about me under the head section We have the body section. Now we start to create the form. Then we needed inputs and the first input type. Well, that's going to be text on the name. Well, this is the name of the person, so just call that name. I Would you pick that two more times? The 2nd 1 is going to be a number for the age, so we'll put in here number on the name of this is age on the 3rd 1 Waas favorite color. So get is gonna be text and I just pop in here color within need another input this time is gonna be submit for the button and I'll call that submit. I need an action on my action. I'm sending this to this process dot PHP script so process dot PHP my action so of my method, rather is going to be a post method. So that's the form actually completed. I'll go ahead and save that when the process side of things. The first thing we do start with my PHP code block and then we'll do a little test. If something is true, do something. And if you recall, the first thing I want to do is check if the's submit button has been pressed. So again, we've done this a few times. So if is set something on the something that should be set is post submit. And inside the curly braces, Well, the first thing we want to grab are those three bits of information, so we'll have the name that's gonna be equal to post name. Well, then have the edge which is equal to post edge, and obviously we have the color as well. That's sad. Post color. Okay, the next thing we're actually asked to do and we process this is to store the dollar into three separate cookies. Let's go ahead and do that. So, to set a cookie, we used the set cookie function. And the first thing will be the name of the cookie. Well, the 1st 1 we have called name the value of this will be this variable here, so we'll put that in. And if you call, the third thing is we need an expiry. So we'll put in time plus 3600 that will set the cookie for the name we need to do edge and color. I just know so spelled that wrong. So So I just want to duplicate this twice more events at the age cookie, which will be the edge. And we also need color on that will be color in here. So we've set the three cookies. That's all done on. The next thing to do is to actually retrieve those three cookies and to display the information in this format here. So I'm going to copy that go across to here going to be retrieved, fail on and start with my PHP code block. I'll just pissed in those three there. So I need to do is actually to echo out these. So I'm going to echo your name is name on de Current your edges. And finally, this one here we like about this one as well. Michael. A single echo on that. I just want to break it down a little bit, just so you can see what's going on now the actual name. Well, this will be the cookie. Let me just grab that and welcome continent car so I can captain it on the cookie. On which cook is it? It's going to be the name Cookie to put that in there. Onda Well, actually append a brick on here as well. In fact, I just do it on the next lane, Alecko in a little break, but that I also need a break down here on will take that age one as well. And we need to put a cook in here instead of edge. So again, we're going to catch in it on. I'll grab that pissed it in here and change that to edge, then we'll do a same similar sort of thing for color. Then we'll just come cabinet on their the cookie for color on change that There we go. I'll say Those it's not only to do is go to my preview on Have a look at what's going on. Well, pop up to the preview on. The first thing I want to do is go to the actual form earned in the form I can set a name. Let's put Peter his age. Let's put 21 favorite color it will put as blue. We'll submit that that's gone to the processing script, and I need to go to the retrieve script. Just type in their retrieve percenter on. There we go. Your name is pizza. Your age is 21. Your favorite color is blue. Now, what you could do to extend this is actually do some error checks. So see if the cookie has actually bean set. If the cookie hasn't been set and also you would get an error, so you might want to put a conditional statement in here. If the cookies been set, then we can actually echo at this statement. Else we could echo it. A little error message 78. How to create a session in PHP: we know move on to the creation off sessions on Creating a session is a relatively easy process. Now, before I go ahead and show you how to do that, the first thing I'll do is to actually do a quick preview off my cookies. And obviously my preview page has this year l here so starts with preview. So any cookies or sessions should actually start with preview, so refresh. Unless you can see there are no sessions or cookies. Actually, in my browser, let me go back to code. Anywhere on our credit, you fail. We'll call this one create session dot PHP. Don't go ahead and open that file. The first thing we'll do is to start my PHP code block on 20. Create a session. Other said It is really easy. We just use session underscore start from brackets and semi column. I'll go ahead and say that we'll go to my preview. Just do a quick refresh here. Hand will open this create session dot PHP fail, so that should have actually created a session. I'll go to my cookies, let me refresh, and we'll look for preview. Indeed, it's there, and you can see we have one cookie on this cookie actually contains the session. I d. That's this unique number Here. I'll go back to code anywhere. Now 30 drops him down it into that session we do. The following is dollar underscore session. I mean square brackets and tick marks an insight. Here we put the name or the key for this session data. Let's say I want to store my name on Let's say my age. So we're putting him. My name is Patrick. So what? Patrick, If I can spell my own name there we go. Semi Colon. Let's say I also want to store my edge. Let's not give the game away. Let's say that my age is 21 a little bit older than 21. But there we go. I can say that. Go to my preview. I can refresh this session fail. Go to my cookies. We'll refresh here. I will see what happens. I've still got my preview. I still have one cookie. Click on it and I still see that unique Identify her. So the dad of I've actually stored in that session. This here is not stored in the browser cookie. It's only this unique identify air this session I d. The actual data is actually stored at this server end That makes the use of sessions a little bit more secure than cookies. The actual user has no way to actually amend this data. Here. It is done from the server end. 79. How to read a session in PHP: Now that we know how to start our create a session, the next thing we need to do is to read values from that session. So Chris and you file, I'll call it Read session. Don't PHP again. Go ahead and open that Fail on start with my PHP code block. Now, in order to read that it from the session, the very first thing we need to do is actually to start the session. Session starts. We did that in the last video. I know. It might seem strange that we're actually starting a session. What we're actually doing is we're checking if a session has already been set here. And if it has, we're just getting that session ready. If there is no session set, then obviously we would be creating a session by invoking this command here. Now, in order to read the data from the session, we can simply do something like Echo double underscore session and the name of the session . Waas. Actually, Nim, go ahead and say that. Go across the preview to quit refresh. And there we go. Read session and they would get Patrick. Now, there's a little problem here because if I go ahead and actually remove the current session to let me delete that that sessions now gone across my preview and refresh. We get this undefined index name. In other words, it doesn't know that that session does or does not exist. Let me go back to here, so yes, By calling this page, we are starting a session. But we haven't actually put any doubt it into that session. We're then looking for the session called name. Well, that doesn't exist because we just removed that session. So what we need to do in here is we need to put in a conditional statement. So if something is true, we're going to do something well in here. We can just echo at that session. So we'll put that in there and then the if statement, we check if is set something I want to be checking. That's been set. I want to check if the session name has been set. If it has, then I'll just echoed that value. We go ahead and save that across my preview on refresh, and they were getting nothing this time to manage to get rid of that error. Let me go back now and actually reset that. Sessions will create the session. So by doing that, what we've done, we've created a session. We've put in the session data of, ah, name on Patrick so I can go ahead and read that session. Read the session, and there we get the name Patrick. 80. How to update a session in PHP: So far, we've seen that we can easily create a session using sessions start. And then we can add elements to that session, for example, about it. My name on an age. We can then read elements from that session when we always have to start with sessions. Start now, we can just echo out which ever element we wish to actually retrieve. We also put in a conditional statement here just to check if the session has been set for the session. Element has been set and then we can retrieve that data. Now, we also might need to update the session, so I'll credit new page here. I'll call it update session dot PHP. I don't go ahead and open that and in here I'll start my pitch PICO block on whenever we use in sessions, The very first thing we must do is actually use session Start now to actually update the session is very similar to actually what we did in the create session file. We had these two elements here. It's let me copy those a problem across here, So to create the session, we use session starts. We then added these elements to the session. We got name and age with these two values. And if I want to change those, let's say I'll change my name to John on my edge to say 50 and save that. So here we have the create session fail How? Just refresh that. Well, then go to the read session fail. Refresh that. And there we see. Hello, Patrick. And that what I'll do, I'll go back hand free. Fresh here on will update this session. So by running that script there, we should have changed the session element name to John on the age to 50. So go back to my read session and I should see here. Hello, John. Let me refresh. And there we go. It's Hello, John. So, to update session, all you need to do is session starts and then just change the elements with the new values . 81. How to delete a session in PHP: in this lesson. We look at destroying sessions. Now. We've already seen that we can create a session. We use session, start to actually create a session, and then we can add elements to the session array. And here I have two elements name on age. We then learnt her to read a session on. I've just changed. It's a little bit. We start with session starts. I'm checking if the name session has been set. And if it has, I just echoed in them. And here my quit in the age again, checking. If the age element has been set on, then just echo at that. So that actually looks like this name and age. Well, we can. Then do is we can update the session. And here I'm updating the session to John and 50. And finally, we can actually destroy the session. Now again, it might seem odd, but we have to start the session before we can actually destroy it. And we just use session underscore, destroy a round brackets and semi cola. Let me show you how it all works Here. I have the create session, so go ahead and refresh that. So that's now created a session with the name of Patrick and an age of 21. I can read that session. I'll refresh. There we go. We've read that session with an agent on em. I can then a dead that session. So we refreshed the hat. That should have set the name to John and the age to 50. So we go to our read session and preview that. There we go, John 50 on. Finally, we can actually destroy the session, so refresh that should have destroyed the session. I go to my preview and have a look on that that has actually being removed. Now, already done. We've actually destroyed the whole session. So if I went to my browser and checked for that cookie, it wouldn't actually be there Now, though, is useful to be able to actually destroy the full session. It would be nice if we could actually remove individual elements on. We can actually do that by using the unset functions. So we say onset on. What do you want to insect? Let's say I want to unset the session name. Can we save that? I'll go back and I will recreate session. Let's preview it. So I should get Patrick in 21. Now go toe destroy the session where I am unset ing the name. Unless preview that should vanish when I refresh. And there we go. We've unset the name. We can also obviously unset the edge. Copy that and paste that in and change that to edge Say that we'll go to the destroy session page again Refresh good in my preview and refresh And obviously the name on edge has been removed. However, in this case, that cookie would still be remaining in. My browser on that session is still active, although I have removed the individual elements. 82. Challenge 21: Hit Me Baby One More Time - SOLUTION: in this video, we look at the solution for Hit me, baby. One more time on this really is all about the use off sessions on What we have to do is to create a very simple hit counter. Now we can see from the task that we need to track the number off times a user visits a page during a browsing session and then print out a little message such as this. Now there's various things to consider such a zoo. What will be due on the first page visit, how we're going to store the cancer. How do we increment the hits, etcetera? So let's go across and have a look at what we can do here. I've credit folder cold Hit me, baby. And within that, I've got a hit counter dot PHP And here it is. I'll start with my PHP code block. Now we know that when we're creating a session, the very first thing we should do is use session underscore. Start on. What I want to do here is to actually create a hits session. Now what I need to do is to check if it's already being set. If it has then I'll increments the number of hits by one on Echo. A little message. If it hasn't been set, then I will actually create the session hits and set that equal to one and then print out a little message. So in basic pseudo code, I'm doing something like this. I'm saying, if the session has Bean set, then we want to increase by one, and we also want to echo the message. If it's not set, then we want to set the session on. We want to put the session equal, toe one, and we also want to echo a little message. So that's my thinking behind this. Let's go ahead and code this. So if the sessions being set than increased by one, So I need a little if statement here, So if something is true, I'm going to do something now. What about testing? I'm testing if is set. Something on the something I'm testing that's being set is my session called hits. So session square brackets, tick marks and hits. So when I used to go in here is this little bit here? We need to increase the number hits by one on Dakota Little message other ways are else. We need to do something else. And that's something we also need to do. Is this little bit here? So put that insight. Okay, So if the station has been set, then we need to increase that hit counter by ones. Let's do that. We need to take the session called hits. Not really. Teoh. Increase that by one with only two echo. A little message. So echo. And the message we had to say was something like You have visited this page, her Milken captain it on the session. It's on. We put on the end times. Okay, so it takes care of that bit. So we are increasing the session element hits by one and then we reconnect your visited this page, That will be a number Andi times on the end. Other ways. If the session hasn't Bean set, then we'll actually need to set it, and we need to put it equal to one. So we'll have session hits how we just need to put that equal to one on the money to echo. Had a little message on. We'll just put something like this is the first time Do you have visited this page. We'll save that and I will go across to the preview and we'll click on that. This is the first time you visited this page. Now if I refresh, they should go to two visited two times. Refresh again three times four times, etcetera.