PHP & MySQL Beginner’s Guide: Part I - Introduction To PHP | Ismail Raji | Skillshare

PHP & MySQL Beginner’s Guide: Part I - Introduction To PHP

Ismail Raji, Passionate Online Instructor

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
16 Lessons (1h 11m)
    • 1. Welcome

    • 2. How Does a Website Work?

    • 3. Installation

    • 4. Getting Started With Vagrant

    • 5. PHP Basic Syntax

    • 6. Variables

    • 7. Conditional Statements

    • 8. Conditional Statements: If Variations

    • 9. Conditional Statements: Switch

    • 10. For Loop

    • 11. While Loop

    • 12. Arrays

    • 13. Combining Arrays And Loops

    • 14. Functions

    • 15. Commenting Your Code

    • 16. PHP Quiz


About This Class

Welcome to PHP & MySQL Beginner’s Guide. In this course, I’ll guide you as you take your first steps beyond the static world of building web pages with the purely client-side technologies of HTML and CSS.

Together, we’ll explore the world of building dynamic websites, and discover the amazing array of dynamic tools, concepts, and possibilities they open up.

Whether you are a designer looking to integrate a new front end into an existing site that uses PHP, or a student looking to move toward a development career, PHP can help you on your way.This powerful language, used by more than 80% of websites, is not just for small projects, many major sites run it as a core language, such as Facebook, Yahoo!, and Wikipedia.

What are the requirements?

  • HTML: Basic knowledge.

What am I going to get from this course?

  • This first part will give you a gentle introduction to the syntax of the PHP language and basic programming topics as they apply to PHP. 

What is the target audience?

  • If you are new to programming and embarking on your first interactive website, you are in the right place. 


1. Welcome: Welcome to PHP. ID my scale. Beginner's Guide. In this course, I will guide you as you take your first steps beyond the static world off building webpages with the pier lick line side technologies off its Jamil on Sanchez. Together we will explore the world of building dynamic websites and discover the amazing array off dynamic tools, concepts and possibilities they open up. If you are completely neutral programming and embarking on your first interactive website, you are in the right place. This first part will give you a gentle introduction to this syntax of the PHP language on basic programming topics as they apply to PHP. Whether you are designer looking to integrate and your front end to an exist inside that you the speech be for a student looking to move toward a development career, Ph B can help you on your way. This powerful language, used by more than 80% off websites is not just for small projects. Many major sites run. It is a core language such as Facebook, Yahoo and Wikipedia. All right, enough talk. I'm so excited to share this course with you. Let's jump in 2. How Does a Website Work?: Have you ever wondered how our website works? When you open your Web browser, whether it's Google chrome, Mozilla Firefox safari or another browser and you enter the website, you l Have you ever asked yourself, How did that webpage get to you? An aspiring Web developer like yourself must know the basics off how a website works. As you may already know, the Internet is a huge network that connects millions of computers together. Globally, these computers can be classified into two categories. Clients. These are the computers off Internet users like you and me. Each client represent a visitor to our website servers. These are powerful computers that store and deliver websites to Internet users. That is your clients. Most people have never seen a server in their life. However, servers are essential for the proper functioning off the whip. The question now is. How does the client and the server communicate? Before we answer this question, we need to know the types off websites that exist. There are two main types off a website static and dynamic static websites. AH website spell choosing Onley, HTML and CSS, although they work very well. But their content doesn't update automatically each time there is something you to add. The website owner or the Web master needs to edit the code, which is not practical if the website needs to be updated multiple times a day. A good example off a static website is a showcase website usually used by companies to showcase their products and services, which doesn't require any interaction from visitors unless there is a contact form, for example, in which case the website becomes a dynamic website. Dynamic websites are called dynamic because their content can change without intervention from the Web master, you are currently watching this course on the dynamic website. In addition to HTML and CSS, dynamic websites use other languages like PHP and my scale, which makes them them more complex than static websites. Now that we have an idea about the two main types off a website, let's answer our original question. How do the client and the server communicate? The answer to this question depends on what type of websites we are talking about. For static websites, the communication happens in two stages. The client asked the server to see a webpage the server response by sending the requested page without any modifications in the form off HTML and CSS. For dynamic websites, there is an intermediate step. The client Ask the server to see our webpage. The server prepares the page specifically for the client, using a language like PHP, the server sand degenerated page in the form off HTML and CSS again because the client can Onley understand HTML and say, says an example of how the server prepares. Our webpage specifically for client is when you log into a website he was in your email and password. The website then displays your profile picture and your name on the job. For example, this is something specific for you. For another visitor, the website will display their profile picture and name, which are different than yours. I've mentioned Ph. B in my s scale a few times. Let's take a look at some definitions off this true pH. B is a server. Cite Scripture language With BHP, the Web server is able to do more than just sense static Borden webpages by running a little programs called PHP scripts to make your website dynamic. Pitch be becomes really interesting when combined with my scrap. This is a relational database management system that can save pieces of information in an efficient and organized manner so you can find them easily later. You can use my scale, for example, to save the list off your website members. In summary, there are two types off websites static sites made in each Daniel on San Cess. Their content can only be updated by the Web master dynamic sites made with other tools like PHP and my Skrill, in addition to html consensus to make the website come alive by allowing visitors to interact with it. Website visitors are gold clients. They asked the server hosting the site to send them the webpages. PHP is a language executed by the server as personalized the webpage. According to the visitor, my scale is a database management system. It is responsible for storing information. 3. Installation: building a website is just like making a cake. Think about it before following the recipe, you won't need the ingredient. First, building a website as much the same. We will need a bunch of tools and packages, and I'm going to show you how to download them and set them up. We know by now that PHP scripts are executed on a server in order to generate HTML code. That browser can understand, but how do we get a server to run our PHP code? Well, there are two ways you can use a Web host that supports PHP during your scripts on their server, but this can become teaches really fast. You will need to write the PHP script in your editor, upload the script. This is didn't server the navigate to the U. R I, off the final created to sit results. If there is a nerve to correct, you'll need to go back to your editor, changed the code. I upload the file again to the server, then reload the page. As you can imagine, this method would with a lot of time that you could be using to write coat. The second way is to set up your own Web server by running your own server. It would be able to save a file in your editor and viewed the changes in your browser by simply refreshing the page. No file uploading s require. The best part is you don't need any additional hardware your computer. We will just find this is really a game changer. So how do we get the Web server running on your computer? We first need to choose one on example. Off. A Web server is Apache. It's an open source project available for free. It's secure, efficient and highly customizable, which makes it the most widely used Web server for our purposes of executing peach. Because a Web server alone is not enough, we'll also need to install PHP for this course. It will also want to install my scale to interact with data basis. It can be a real struggle to manually install each of these components individually. Apache PHP in my Skrill each have dozens off configuration options. Even an expert will have a difficult try trying to set them up and have everything working . Luckily for us, developers recognized the struggle over the years, and they put a couple off options employees. For us. One of thes options is the pre built packages. These are Web development environments that installed automatically Beach be Apache, my scale and other well of in software all brick and figured with appropriate sittings so you can start developing Web applications on the go. Some examples packages are exempt. The X stands for operating system. The rest of the acronym is Apache, My scale, PHP and Birth. We also have one and Lamb. The second option is using a virtual server using virtual ization software like Virtual Box and M, where that allows you to run on operating system inside another operating system. We can run a Lennox Web server with PHP, Apache and my scale from our Windows or Mac computer and connect to it as if it's a really Web server. So which option is the best? Although the rebuild packages might be tempting because it's quick and easy, there are a few drawbacks to this method. Given moment, pre built packages are not always guarantee to have the latest components versions. This means you may face problems with features that have been depreciated, and you won't be able to try the newest released futures. The pre built packages are also operating system specific. If you use Wamp on Windows, for example, your code might not work. Lennox. This is a big problem, since the majority off websites are running only next servers. That's why using a virtual server is a far better approach. Allows you to test with several versions off Apache PHP in my scale on a Lennox virtual machine by downloading pre configured virtual machines. It's a best of both worlds approach. 4. Getting Started With Vagrant: In order to run a virtual server, we will need to install some pieces off software. First, we will need get. This is a free and open source distributed version control system. To put it simply, it allows you to download other people's goat. We would also need virtual box. This is a cross platform virtual ization application that runs the virtual machines and finally, vagrant. This is the tool that allows you to configure your virtual machines. It's just with the virtual box or other virtualization applications to create the server. Once you download this pieces off software and install them, it's now time to download our virtual server. It's called Home State on official prepackaged vagrant box from Laura Fell that provides you with a ready to use development environment that runs on any operating system. Whether it's Windows, Mac or Lennox. Well, we first start by adding Homestead Box Toe our vagrant installation. He was in the command vagrant box ad lauraville slash homestead. It will prompt you to choose a provider. This is the virtual ization application that will be using which is, in our case, virtual box. It will take a bit of time to the all of the books, depending on your Internet connection speed. Once the download is finished, we will move on to install in Home State. Earlier I told you that get allow us to download other people's goat, and that's exactly what we're going to do just in the command. Get clone. We're going to download or clone home state code from the get have folder. I'm going to call Home State and will be in my home directory. Now I'm going to navigate into the homestead directory where we cologne our repository. Then, as recommended in the official documentation, I'm going to check for a tag version off home state to make sure I'm using the latest stable version, which is, at the time off this recording. 8.0 point to But using the command get check out, followed by the version number. Once that is done, I'm going to run the command bash in it that it's age to create home state configuration file. That we will be editing later home said, also requires basis each key a special file used to establish secure connections. So I'm going to navigate back to my home directory, then run the following command, which you can simply copy as well. That would prompt her to enter a pass phrase you can simply hit Enter to Skip that earlier we used to command bash ended that its age to create the configuration file. This file is Homestead that Djamel and can be found in the homestead directory that we created. He was in a text editor. I'm going to open this file. The first line contains the AP address where Homestead will listen to requests. The next line contains the maximum amount off memory that will be consumed by it. It is it 22 gigabytes. The third line says it will use one CPU and the last line says the provider is the virtual box. I'm going to leave the default values for these lines. Next, we have the authorize and Keys properties where we are supposed indicate the locations off the S H files. We have already created ssh keys in our home directory, which is what the chill design refers to. So I'm not going to edit this files as well. Movie known. We have the folders property. This property will list all the folders we want. Share with our homestead environment. The map keyword refers to the Projects folder, which will be located when our computer you can change it to anywhere you want. I prefer putting my projects in my second hard drive. And for the purpose of this course, I'm going to create our first folder called Hello World. The jokey word shows the folder which will be present on our homestead environment and sink with our local machine folder. I'm going to give it the same name. Hello, world. Next we have the sites property. This property allows you to map a domain to a folder on your home state environment. This simply means that if we type the euro homestead that test in our browser vagrant will send request to slash home slash vagrant slash code slash public. I'm going to change this as well to reflect the hello world project we're going to be working on. So instead of home homestead that tests, I'm going to say, for example, hello world that tests and changed the second line as well. Sitting have the sides. Property alone is not enough. Home state doesn't know yet about other world terrestrial. Earlier, I told you that home state where the lessening to requests from this I p address. So instead of type in this I p address in our browser, which can be very hard to memorize, we're going to redirect requests for hello world that tests to that I p address. This way we can simply visit a lot World that tests on our browser and the request will be sent to our homestead machine. We can do this by editing the hosts Vile on Mark and Lennox. This file is located at forward slash 80 c police slash hosts on windows. It is located at the following directory. We will be out in the following line. We haven't created our hello world project yet by navigating to the refractory we entered in the folders property off our configuration file. I'm going to create my hello world folder and also create a sub folder Cold public. In my case, the final path s the slash code slash Hello, world slash public. It can be different for you depending on the value of fearful this property. Finally, it's time to start our server. He was in the command ignorant up. Remember? You need to run this command from your homestead directory. It will take a few minutes to load for the first time so vagrant can board the virtual machine and automatically configure our shared folders and sites. So we have our server up and running. You can stop it at any time. He was in the command Vagrant Holt and broached again reason vacant up When you need it, we can also use vagrant suspend, which suspends the virtual machine rather than fully shutting it down. This generally requires extra disk space to save the exact point in time state off the machine. But you will be able to run the virtual machine much faster when you resume later. He was in the command vagrant resume. 5. PHP Basic Syntax: we have our development environment ready. But before we start writing code, we need a code editor. A code editor is a must for every developer, whether they are new to the world of development or an old hand. Now, a quick disclaimer. When it comes to code editors, everyone has their own opinion off, which could editor you should or should not use. My advice is, if you are already using a code editor and you feel comfortable using it, by all means, stick with it. If not, I recommend using sublime text Tom or visual studio code all available for now, cost for the purpose of this course, any of them will get the job done. Personally, I will be using visual studio code. Okay, so you've chosen your code editor. It's now time to get our hands dirty and start writing our first PHP script. Start by open in the whole World project inside the public folder, I'm going to create a new file that I will call index dot PHP. The PHP extension is our way off indication that this is a PHP file and needs to be interpreted as such. This extension also helps the text editor provide the correct code. Hence, for example, I'm going to pissed in some plain HTML code. Nothing special. But then, inside our P tag, I'm going to write an example off PHP code that this place hello world to the screen. To do this, I first need to mark the start off my pastry script. You can do this by right in less done sign felled by question mark, then the PHP keyword. Like many other languages JavaScript, C, C plus plus Java and others pitch Peace Index consists off a Siri's off statements. A statement makes up the logical, such of instructions that tell the Web server what to do. I want to display the next, tell the work to the screen. So I'm going to use a page. The statement called Echo felt by my text and put a semi colon to mark the end off my statement. The Echo statement will take the text it's given and insert it into the page is html code, Uh, this success position finally, to mark the end off my PHP swept, I'm going to write the question mark followed by greater than sign. Now if I open my Web browser and navigate through Hello World That test We should see our hello world message critical, right? So what exactly happened here? If you right click inside the browser and choose a few page source, we can see our index dot PHP file coat. I noticed that there is no sign off PHP code here. Instead, it's just the standard. HTML. This is because our Web server took everything between the Ph, made the limiters and converted to regular HTML code before sending the webpage to the browser. Because browser's can't understand beach be good. We can also include a chairman text in our string of text. If I go back to my text editor, I can bold my hello world message by adding the strong tank. Okay, so we print in some text using the echo statement. If you noticed the Texas surrounded by quotes, bitch Be will create an intense surrounded by quotes as a string, which means Ph B doesn't process it all, but it just passes it to whatever command you called in our case. It was the echo comment. We can also give PHP an instruction to follow. I can't, For example, tell Ph B to print the current time using a built in function called date. You can think of a built in function as a reusable pace of code already defined or created that you can directly call to start using it. Maybe I can add a little text hair that size of the time Now is now. I can't put the date function inside the quotes as BHP won't try to run anything that's inside the string. Instead, our recon coordinated with my existent message using a dodge. Then write the keyword date, opened the parenthesis and give it the following string so it cannot put the Korean time for us. Don't worry if this doesn't make a lot of sense now, we're going to see this in more details later on. For the moment, I just want to show you that PHP can do a lot more than just sprinting text. Now go to your browser and refreshed the page. And just like magic, the current time is displayed. Amazing, isn't it? If I were first a pinch again, you can see that the chime is updated automatically, just like before. If you refresh the page source, you will see that there is no PHP code there. Instead, when we called the date function, it returned the try and that time value was inserted into our HTML code. 6. Variables: Like most other programming languages, BHP statements involved using valuables ah, valuable issues to store information that your program manipulates While it runs. It can be thought off as an imaginary container that hold a value which can be used the world Your program In PHP variables are denoted by a dollar sign followed by the valuables name. I've given a name off my valuable here, but this is just for the sake Off demonstration. When you are naming a valuables, always make sure that the name your son your valuable is accurately descriptive. This will make your script clear, easy to follow and easy to the bug To assign a value to a variable used the equal sign known as the assignment operator followed by your value. I will give it, for example, the string of text. Hello world. Let's use the echo statement again. But this time, instead of passing in our hello world text directly will pass in the variable my valuable. If I go to my browser and fresh the page, you can see that the end result is the same When our PHP script runs the text. Hello world will be shown when naming your variables. There are some rules to keep in mind. Variable names. My only include uppercase or lowercase basic Latin letters digits and underscores the first character off. A valuable name is not allowed to be a digit variable. Names are case sensitive. This means that variables named var var and War with Capital letters are separate and distinct. Pitch be is what called a loosely typed language. This means that a single variable may contain any tape off nature again. For example, change my hello World String off text to a number on Ph. B will not complain about this. I can also perform mathematical operations on values using arithmetic operators like the addition operator using Plus sign, this obstruction operator hears in mind sign the multiplication operator. He was in the hysteric and the division. He was in forward slash 7. Conditional Statements: each day we make hundreds of decisions the I cook today or just order pizza I put on the Red Sox or the blue socks. You take a look at the weather outside. If it's raining, you take your umbrella with you. If it's a sunny day, you don't. All of these actions have something in common making decisions about whether a certain logical condition is true or false. Decision making is also present in programs using what is known as conditional statements. These are the digital equivalents off the decisions we make where your code that's something different, depending on whether something is true or false. The most common conditional statement you will use in your code is the F Els statement. Let's take a look at a simple example. Here we are evaluating the variable signed in because I initialized that to true. The true part off our statement will kick in, so if I run my script, he will see the message. Welcome. You are a member off this website. Now if I go ahead and change the value off science, Inter falls the message. Hey there. Consider signing up will appear instead because our expression, our expression evaluates to falls. In most cases, your expression will really be a simple valuable that is set to true or false like in this example, it will usually need to compare between two or more things to establish a true or false outcome. This can be then, using what is known as conditional operators. Let's get back to our code editor and write down a simple program that lets you know if you received and your friend request. First, I need a valuable that will hold the number of requests I receive. Let's call it friend requests, for example, and initialize it to 10. Then I'm going to this. This new variables value if it's greater than or equal to one. I want to print a message saying You received your friend requests Els. I want to print a message that say's you don't Oh, here is something really interesting. That just happened because the word don't have an apostrophe in it. Pitch for you thinks that this is the end off our strength to tell PHP that this is the apostrophe. Doesn't market end off our string, but it's actually part off it. Weaken. Simply add a backslash just before it, and we can continue our strength. You don't have any new friend requests now if I go to my browser and fresh to page conceded message you received new friend requests show in because our friend request valuable was a naturalized retain. So it's greater than one off course. The great and then or equal to sign is not the only operator you can use greater than less than less than or equal to double equal signs with this equality. So here, be careful not to use a single equal sign to this equality, because a single equal sign assigns a value. We also have the exclamation mark fault where the equals sign to test that two things are not equal. Inside our F parentheses, we can have more than one expression. For example, I can just that our valuable friends request the value is different than 10 and his list than H years in the double ampersand sign. So on Lee, window to expressions are true that the message you received new friend requests will get printed to the screen. If I only require one of them to be true, I can change the double M per sign sign with the double pipe sign, which means or hair. If either the first expression or the second explosion evaluates to true, then this first measure message will be printed. There are many other operators, but these are the main ones that will be using most of the time. 8. Conditional Statements: If Variations: before moving on from the if statement, I just want to show you its variations. Here is what the original example look like if we were saved one friend request form, or then print the first message else Whip rent. The second message I can totally get rid off the else block on my script will work just fine, since my expression here evaluates to true my script. Well, print our message. If I change, the friends request valuables value to zero. My expression is now evaluating to false, which means my code will skip over the Echo statement and moves on to execute the next statement. Because we don't have any other statement in our program, we will simply have a blank page. The Elsa Block is completely optional. As you see it's under the changes and get back to our original example. You can see in my first message you received your friend requests. I put the snt and between parentheses to say that's either one request or more. Well, I can be more specific if I want, by chaining F statements together using the else F key work, for example, you can say that if friends requests equals to one. Remember always the double equal sign echo. You received a new friend request else. If friends requests, it is greater than one echo. You received new friend requests and our last ounce of look. We'll stay the same if I change the value or Friends requests toe one. The first statement evaluates to true. So we get the message off you received in your friend request. If Friend requests value is greater than one, the else F statements evaluates to True, and we get the message he received new friends requests. And finally, if Friends Quest Value is zero, the block else evaluates to true and we get the last message. 9. Conditional Statements: Switch: while the f else and else F statements can be used to deal with any situation that requires condition. ALS. There is still another statement that can be used. Called switch switch is particularly useful when you have a single variable with a fainted number off expected values and depending on each value, you want to execute a certain block off coat. To illustrate this, let's take a look at this example. You're currently watching this course on an online learning platform. There are several times if users interaction with this platform, which conveyed generally grouped into three categories. We have administrators. They take care off the whole platform, managing courses, teachers accounts, students, accounts, etcetera. We have teachers. They can create courses and interact with students. And finally, we have students. They can watch courses and interact with teachers. In this example, I have a variable cold use, a role whose value is set to administrator. This user role valuable is what gets passed into our switch statement. Our switch statement contains a group of case blocks Onley, one of thes block when get hit with their code getting executed. The way this chosen one gets picked is by matching a blocks case value with the result of evaluating the expression in our case, because our expression evaluates to a value off administrator. The code inside the case block was case value is also administrator. Gets executed. Allow major digress for a moment here, if you haven't noticed yet, have used double quotes in Case May strengths in this example and our page because it was still executed as normal. This is because PHP supports both single quotes and devil coats to encase drinks. Although there are some minor differences between the two quotes for most purposes, they are interchangeable. Okay, back on track. If we alter the value off the years a role valuable toe undervalued values, you wouldn't see that the other case blocks get executed. Near the end off our switch statement, you will notice that there is a day fourth block. This block specifies a default behavior when no case blocks value, matches your valuables venue. For example, if I change my user role value toe unknown, the default block will kick in. There's also an important key word here called Break that ends our case block when your code hits the brake, keyword it exits the entire switch block and continues executing the code that lies below it. If you did not specify the break, keyword, your code will just move through every single case block until it reaches the end. You almost never want that. 10. For Loop: when you are called in, there will be times when you want your program to do something repeatedly. For example, let's I want to bring this message. Welcome their students 10 times. One way I could do this is we're simply copying and pasting this eco statement another nine times so that I have Chen echo statements. In total. If I head into my browser, you can see that it accomplishes what we want to do. What we did is we duplicated our coat and this is really never a good idea. Let's say I made a mistake in my script and forgot at the single quotes signed at the end of my string off course, BHP will complain about this and show an error message. What happens if I was not aware off this error and I started duplicating my coat? If I want to correct it, I need to go over each line and add the missing quote. Now imagine if I heard this repeated a few 100 or 1000 times, it would take me forever to correct the error. As you can see, maintaining duplicate coat can quickly become very tedious. We need a better solution for repeating coat, and that's where Lopes come to the rescue. One of the most common ways to create a loop is by using the four statement to create what is known as a for you. A four loop allows you to repeatedly run some code until an expression you specify returns false. To clarify what I mean, let's translate our example. Start by right in the four key word, followed by parenthesis inside the parentheses, we first need to indicate a starting condition. This usually involves declaring and initializing a valuable I will create a new valuable called count and initialize it to one that put a semicolon at the end. Once we define our starting point, when you to the German, how long to keep looping in order to print our message 10 times, We need to keep hoping wild count is less than or equal to 10. So I will simply right count is less than or equal to 10 and put the semicolon idea. This conditional expression will return true if our count variable is less than or equal to 10. As a result, our love continues to run. If our valuable becomes greater than 10 the condition is false on our lope terminates. Now you may be wondering what can cause our count valuable to change from its starting value off one. And that's exactly what the last part off our four Luke does at each iteration off our loop were increments, our count valuable by one by writing the double plus sign after count. This way, each time our look run our count, value will increment Finally opened the curly brackets and put the echo statement inside. Now, if you're hit into the browser and first a page, you will see our message granted 10 times. Okay, Now that we looked at each part of the four Lope in great detail, let's run through it again. One more time to make sure we totally got this. When our follow begins, the count variable is created and initialized to one next hour. Code checks at the value of count is less than or equal to 10. At this point, whatever code is inside, the lope executes. In our case, that is the echo statement. Once the Echo statement inside the Lope have been executed. The last part off the four statement kicks in. The controllable is incriminated by one. Now the loop starts all over again, except the valuable count is not re initialized. Its value is set to the incremental value off, too, and that means it's still passes. The count is less than or equal to 10 test. As a result, the echo statement is executed again on the value of count is incriminated. Now the count valuables value is three. This whole process repeats until the value of count is greater than 10. A disappoint that this expression will fail and the loop will exit after having successfully brain tid our welcome message 10 times. 11. While Loop: the four Lope is not the only Leb structure we can use. There are two other variants. The 1st 1 we're going to see is the while loop. I'm going to stick to the same example so you can see how it translates for the low variance. I will start by the Clarin and initializing the count valuable to a value off one. And then I will write the the wild keyword found why parentheses inside the parentheses were on Lee going to put that this condition which is Count Liston or equal to 10. Then inside the curly brackets, I'm going to put my echo statement and finally increment my count valuable so that the Lopakhin stop executing. If I had enjoined browser, you can see that we have the same results. Now, the last of Orient I want to show you which is pretty close to while is do while instead of having the conditional expression at the beginning, do while has it at the end. So I'm simply going to cut this part here and based it a dent at the beginning. I'm going to add the the keyword do, and that's it. This is how a doorway loop looks like if we execute our script, we will get the same result. There is, however, a main difference between a while. Loop on I do. Why loop? The difference is that the accountants, off a while loop could never get executed if it's conditional. Expression is false from the very beginning, but for a do y loop because the conditional expression is evaluated on Lee. After one iteration, your lips continent are guarantee to run at least once. So if I change my count, valuables value to something greater than 10 our look will still run and our message will be printed to the screen. 12. Arrays: earlier, we learned that a valuable isn't named container that holds a value. Now we're going to see a special kind of variable that can contain not just one value but multiple values. This special valuable is called Array and PHP. There are two types of a race index it raise. These are raised with numeric index and associative race. These are raise with named Kees. Let's first see the index it rates if I want to create a fruits array that contains three values apple, banana and orange. Well, I have to do is open the square brackets, put my values inside and separate them by Calmus. I can also use the ray keyword followed by parentheses, and put my values inside the tune. Additions are equivalent. I personally prefer to use the square bracket as it short and concise. Now you may be wondering, why are they called Index in a race? The reason is simple. Each value in our array has an index, and in order to access a certain value, we first need to know its index the first value or element. Often, Ray has an index off zero. The second has index one, the third has index true and so on. Therefore, the index of the element off an array is n minus one. Once you know the index of the value you're interested in, you can achieve that value by placing the index in square brackets after the array valuable name, for example, I can bring the message. Stay in I love Apple, but using the index zero. Since Apple is the first element off our fruits array, they can do the same for the other values. By printing banana using index one and orange was an index to a true value. Stored in an array is called an element. If I want to add in your first element to my fruits array, I can added directly to my array initialization. Or I can use an index in square brackets toe. Add the new element. Remember the index of the end element. Often, array is n minus one. I can use the same syntax to assign new values to our existent elements. For example, I can replace Apple with avocado was using index zero. Now it's time to see the second type of race, which is the associative arrays. Associative raise followed the same principle. But instead of using numeric indexes, they use named Kees here, for example, I haven't a raid that store some information of a customer. His first name, last name, address and city. I used to square brackets notation to define my ray, but I can also use the array keyword, just like with index in a race inside my customer information array. I have a comma delimited list off key value pairs with the key and the value separated by an arrow, which in fact is just an equal sign felled by greater than sign. The advantage hair is, I don't need to know the index off an element in order to access it. If I want to bring the name or the first name off my customer, for example, all I have to do is indicate the correspondent key between the square brackets. I can add a new element to my array by indicating the new key between square brackets and assigning a value to it, and I can use the same syntax to assign a new value to an existing element 13. Combining Arrays And Loops: one of the most common things to do with an array is to love through which to say, for example, what elements it contains or to process each element in some way. We can iterated through each element off an array using the four low. Our starting condition will be in that zero. Remember, the first element of an array has an index off zero. Since we have four elements in our first away, we want to stop index tree. So I'm going to say index is less than four on the last part. Off our four lope is to increments our index so we can actually go through each element of our array. I'm sure you're familiar with the full lips in text by now. If not, you can always go back to the fore lope lesson to refresh your memory. If I ran my script, you can see that each element off our forts Ray it's printed to the screen an easier way to get the rate through. Each element of an array is using another tribe off hopes that we haven't seen yet, which is the for each loop. The forage law is geared towards array and lets you run a code block once for each element and an array to create this slope, start my type in the four each keyword opened the parentheses. Why the name of your array felt where the ask you work. And finally we will need a temporary valuable that will represent each element throughout our loop. I'm going to college element. For example, if I ran miscarriage, I will get the same results. Forage allows that reiterate through associative array as well. Looking back at our customer information array, I can print the first name, last name, address and city off my customer. The only thing that I change inside my forage loop is the name of the Ray. I can tell me for each loping that I want to achieve the keys as well, by adding it after the as keyword like this. Of course, the key valuable here can be replaced by any name you watch. Then I will add it to my echo statement as well, so we can see it on our browser For each element in customer information, the for each loop copies the key into the key variable and the value into the element valuable. Then it runs the code inside the curly braces 14. Functions: so far, all of the code with Britain was made off a few statements. In the real world, though, this would not be the case. So you will be looking for ways to make your work as easy as possible. And one of those ways is reusing code you've already written. So I let this, let's say we want to multiply two numbers and display the result. We have to a valuables, multiplication and and much applied ER and they each store a number. The Echo statement. This place result off multiplying the values stored by these drove variables. They asked X conductivity wind that your numbers indicates that a multiplication needs to take place. Now, let's say want to calculus multiplication for more values. Here's in Onley What we've seen so far, we need to duplicate our code as follows. Maybe a renamed the valuables, then a sign the new values. As you can see, our code, is unnecessarily verbose and repetitive. As I mentioned earlier when we looked at loops, repetition makes your code harder to maintain. The solution is called reduce, and functions are the key to this. Ah, function is a name set off statements that you can execute just by invoking the function name instead, Off retyping the statements this saves time and prevent errors. Plus functions make it easier to use code that other people have written, like the built in function data that we used at the very beginning off this course, the best way to learn about functions is just to dive right in and start using them. So let's start off by creating a very simple function that Brents welcome to the screen. Then we will come back later to our multiplication example and see how we can translate it using functions to create a new function. He was defection cure, followed by a function name and parentheses and then inside curly braces. The function body function names followed the same rules as valuable names. They must begin with a letter or an underscore, and the rest of the characters in the name can be letters, numbers or underscores. If we're on the script, you can see that nothing get displayed. That's because just creating a function is not enough. Our function needs to be actually cold. Author. Your function block right down the function name, followed by parentheses and don't forget the cynical and I didn't. Now, if we're on our script, you can see welcome the incident. Display it. Our say. Well, confection is a simple one, we call it, and it does something all functions follow this same principle. But instead of doing the same thing, like in this example, where our say welcome faction always displays the same message when in skulled we can make our functions more interesting, They playing with some details and the first such detail we are going to look at involves what recall arguments. Looking back at our multiplication example, I want to have a function that can take a neutral numbers I want and multiply them to do this, a first need a function. So let's create one and call it multiplication, for example, then to let me function know that I will be given it numbers. We leverage the power off arguments by passing a first argument did school it multiplication and a second argument multiplied, for example, inside the parentheses. Then for the function body, I will simply multiply the values that will be stored in the straw arguments. Now I can simply call my function and specify the values. We want to pass to it inside the parentheses when the multiplication function gets called, three corresponds to the multiplication argument, and five correspond to the multiplier argument, and that mapping is entirely based on order. I can multiply other numbers by changing the arguments. Values and my function will do all the work for me. In addition to inaction such as Britain, data to the screen, a function can also return some data back to whatever called it to illustrate this. Let's say that instead of having our multiplication function calculates and display it, we actually want to start the value for some future use. So we want to do something like this. The result valuable will start the results of the notification done by our function. There are just a few things you need to know about being able to do something like this. The way you return data from a function is by replacing the Echo statement by the return key word. We are still much playing the multiplication and much supplier. But instead of displaying the results using eco statement, we return it. When the multiplication function gets called, it gets evaluated and Retton's on numeric value. That then becomes assigned to the result valuable 15. Commenting Your Code: in the world off Web development. You will offer me working with other Web developers. You will be looking at your teammates code and they will be looking at yours, so ensuring that your code makes sense when someone look at it is very important. Even if you're working alone, you're great script that makes sense to you. Today might be gibberish when you look at it one month later, one of the best way of avoiding this problems is but using something known as comments comments, our special lines you write in your coat that communicate something to humans. Let's take a look at this example. In this example, the comments are marked by the double slash character they exist on Lee for US humans to help understand what a piece of code is doing. Beach be totally ignores them so they don't run and get executed like the rest off your code. Comments have another purpose as well, marking lines of code that you don't want executed. Let's say I want my script to ignore my current echo statement because I want to use another one. All I have to do is add the double slash character in front of that echo statement line, this line becomes what we called a single line comments. This is great if you want to comment a single line. But what if you're writing a long comment or commenting out a large piece of code? Well, there is a more convenient way by using multi line comments to mark the beginning of a multi line comment. He was the forward slash fouled by the asterisk character. And to mark the end, you simply inverse the order by right in the asterisk first, then the forward slash. Now that we have a good idea off what comes our with several ways we can write them in Ph. B. Let's see some tips on how to properly use comments to make your coat easy to read. Tip number one. Avoid commenting, procrastination. When you're writing code, you will be tempted to leave writing comments for a later time. But that's a bad idea. If you don't come into your code as you're writing it, you will probably just skip it entirely. Writing comments may be boring, but it is much more time efficient for you and anyone who will be reading your coat toe understand what your script does from reading a comment as opposite to going through hundreds off lines off PHP. Tip number two. Don't complicate your comments. Be clear and concise in your comments. Use English or whatever language you feel comfortable in. You don't need to write a novel. Just simple sentences and words are enough to convey your message. Tip number three Don't comment obvious things. If a line of code is self explanatory, don't waste on explaining what it does. Instead, invest that time incumbent in the less obvious parts off your coat. This tips will help you write properly. Can interest gold? Take some time to remember them so you can make sure you're coding. Experience is much enjoyable for you and for anyone reading your coat. 16. PHP Quiz: all right. Time to test your understanding off this course. Using a short quiz, make sure to grab a piece of paper and the pen dried down your answer before seeing the correction. All right, let's get started. Which the limiters surround PHP scripts. Is it the first choice? The second choice. Order third choice. Okay, correct answer. Is that their choice? Next question. What character is used to end a PHP statement? Is it one a dot to a Cullen three is semi Cullen. The correct answer is three is semicolon. Next question. How do you print hello World to the screen in PHP is it first alert? Hello, World. Second Echo. Hello World or three Print. Hello world. The correct answer is echo. Hello, world Next question Variables in PHP Start with. Is it one? The var ki work to dollar sign for three. The 1% sign The correct answer is the dollar sign. Next question You can use both single quotes and double quotes for strength in PHP. Is it one true or false? The correct answer is Chu. Next question What key word is used to repeat an instruction in PHP? Is it one the if key work to the switch. Keyword or three. The four key word. The correct answer is the four key word. Next question. Which conditions Saiz. If distance is greater than or equal to 30 is it the first condition? The second condition or the third condition? The correct answer is second condition. Next question. PHP arrays start by default with an index off. Is it one on index off zero to an index off one or three on index. Off to correct Answer is an index off zero to create a function in PHP. We used this syntax, one new function, my function to my function or three function my function. The correct answer is three. We used the key word function, then the name off our function. How to add a multiplying, commented PHP. Is it the first choice, second choice or the third choice? And the correct answer is the first choice. All right, that's set for this first part off PHP in my scale. Beginner's guide Stay tuned for part number two