Learn PHP in 15 minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the purpose of this tutorial is to start from the beginning and very quickly get you to a point where you understand PHP and can write your own scripts that will be useful if you have at least some basic knowledge of HTML I conveniently have a video not if you're interested so let's just take a quick look at what we're going to be learning today first we need to install some software if you want to execute PHP files on your computer if you already have a web server with PHP you can use Anstead then we'll take a look at an actual PHP file and how to our programming in one the first bit of code we'll look at what I'll put a string now if you're not familiar with data types and what I'm trying to teach you as we go a string is just a sequence of characters another data type is an integer which is just a number a whole number and an example of where this matters you can do a rithmetic or integers but not on strings you can manipulate strings in other ways though this includes concatenation which we'll look at after a quick introduction to variables then we'll look at some operators if statements forms arrays and loops so before you can execute PHP files on your computer you'll need to install some software if you are using a Mac you want to go ahead and download install mump from ma MP dot info if you're using Windows you will want one from W a MP server comm once you've installed that you'll find a folder has been created in OS 10 it is under applications / month / htdocs I believe in Windows it is just the root directory C / 1 / HD Docs the htdocs folder is used as the root directory of the local web server you've just installed the control panel you get in OS 10 has a button to open the start page which will take you to this page you see here similarly you can get to a similar page from the icon in the system tray in Windows you can then find your own projects by going to localhost colon then the default port 8080 is in this case slash then the name of a folder you've set up in htdocs' directory in a text editor notepad will do in Windows I'm using sublime text 2 in OS 10 we want to create an HTML file so I'm going to quickly create a very simple structure so we've got the age all tags the head tags we can have a title tag and then the body tags so normally we would save this as a file with the the dot HTM or dot HTML extension this will not be the case anymore we're going to save it with the dot PHP extension so I'm going to call this file index dot PHP now call an index we'll make it a homepage web browsers will automatically load that when you go to the directory and I'm going to save this in the PHP tutorial folder that I made in the htdocs folder going back to the web browser when i refresh this PHP tutorial directory instead of just listing the content it is going to load our new file which says hello world the PHP comes inside a new tag in the HTML and what you want is the opening tag a question mark and the letters PHP and then a bit later on we need to close the tag with a question mark and the normal closing HTML tag now inside of these tags we can write some PHP code first of all I am going to show you the echo command so typing echo and then in quotes to make a string we can type it's drink so I'm going to stick to hello world so this is going to output the words hello world to the document now when I use a low two web page the user doesn't see the PHP it's it's not possible to see the PHP code the user can only see the output and lines of code in PHP must always end in semicolon if you get an error check your haven't missed semicolons so when we save that nothing is going to change because we're just outputting hello world now if we view the source code of this page it's as though nothing has changed the user can't see these PHP tags they are executed on the server that output is put in HTML and then sent to the user so echo outputs something to the screen very useful command don't forget the semicolon so I mentioned variables in the introduction a variable is just a way to to store a piece of information and give it a name and variables are denoted in PHP with a dollar sign so we can write dollar call it my Val for variable and then we can make a string alternatively that could have been a number again don't forget the semicolon we can then output the variable so we can type echo dollar my bar refreshing the page we will see our string has been outputted PHP is very strict with datatypes in the same way I can create another variable and just set it equal to 5 then I can create a second one and make that 3 we can then have a variable sum which is number plus number 2 and then we could output by typing echo some 5 plus 3 which will as expected give us 8 if on the other hand I tried to add this is my variable to the number 3 instance going output 3h can't add a string to an integer so how do we manipulate strings we can concatenate them and that means just put them together to form a longer string let's say I have a variable called name let's make that check if we wanted to output hello name assuming we don't know what name is and that set dynamically we can say echo and have a string hello close the the quote to close the string and then use a dot a full-stop a period whatever you wanna call it to concatenate that string to the string name don't forget the semicolon on that will output hello check so then of course we could easily change the name without changing the second line and it would have put hello button instead we've already seen a few operators plus sign is the addition operator which can take two operands for example five and three and add them together equally you can do subtract s-- forward slash four divide and an asterisk for multiply equals is the assignment operator the variable on the Left will get set to the expression which is on the right comparison operators if we want to check that two variables are equal we use a double equal sign so if the variable X is equal to the variable Y this expression will return true otherwise it will return false if we want to check that they're not equal we can put an exclamation mark in exclamation mark and PHP means not we have logical operators so we can say x and y which will return true if X is true and Y is true in the same way we have all which will return true if X is true or FY is true or if both are true and of course there many more operators but we're not going to look at those now instead we're going to use some logical operators and take a look at if statements so just as an example I'm going to create a variable called logged in and I'm going to set this equal to true I can then say if logged in is equal to remember the double equals true then we use curly brackets we can output into a string so remember the quotes you are logged in on the semicolon on the end after the closing curly bracket type else and open some more curly brackets this is what will execute if logged in is not true we can type please log in refreshing our page it says you are logged in because I have logged in set to true if I change it to false it is going to execute this section of the code and say please login such a basic if statement we start with a word if we have an expression in brackets so we can say something along the lines of variable the the comparison operator for equality and then true or false or we could we could have a string in there if if logged in was set to yes for example then we have curly brackets code which will execute if this expression returns true and then else code which will execute if this expression returns false remembering semicolons at the end of lines and curly brackets for the eighth and else so you now know how to output something to the page using echo we're now going to look at how to handle HTML forms again I just have a basic HTML file set up here and we're going to use the the HTML form tag so in our form this could be a login form for example but we're just going to put an input and we're going to have the type as text and then we're going to have another input which will be submit refreshing our page we get a text box and a submit button so the when is form submits at the moment it won't do anything if however we add an action to the the form tag and we the action equal to processed our PHP it will send all of the information from the form to a file called processed our PHP which we will create in a few seconds and we can either post the information or to the page or we can put the information in the URL and post is what you will probably want for most scenarios so sorry we're under the method equals post visually nothing will change but when we submit it will try to go to processed our PHP so we're going to post the data to processed our PHP and to be able to access this data we need to give our input some names let's give this the name name because we want the user to input their name so we can say enter your name so the user enters their name and press submit we then create processed our PHP so I've created a new file normal HTML tags saved it with the name processed our PHP in the same directory as index table PHP you want to insert the the PHP tags open and close and inside we can now get the data which has been posted to this page so we can create a variable called name and to access the posted data we need to use a special variable dollar underscore then in capital letters post now this is an array now we haven't looked at the Rays yet but don't worry all you need to do is use square brackets and inside the square brackets with a string with the name of the input that you want and remember we called we call the input name just then if I typed asdf there instead we would be typing asdf here that's not the case we called it name echo just as we did before a string hello concatenation operator and then the variable name so now when you enter your name and click Submit the process dot PHP page will be loaded the data will be posted to it the PHP script will be able to get the post data from the dollar underscore post variable and it can then output that data so I mentioned the data was in the form of an array so let's just have a quick look at what an array is let's say we wanted to store details about lots of different people we could have lots of variables person 1 equals Alice constant 2 equal Bob etc this is creating lots of different variables with lots of different names what we can do instead is create an array so we can maybe call this a red people and set it equal to the word of Ray and then some normal round brackets inside this array we can we can add Alice in quotes comma then we can add Bob then we can add Katherine and then we can add the semicolon of course at the end of the line so now we haven't the people array this array contains three strings Alice Bob and Katherine first of all let's out for the whole array we can do this with the print underscore R function so print underscore R is the name the function open and close brackets and inside give it the name of the array so go back to index table PHP and we've got an array the the zeroth element of C your best is Alice the first element is bulb and the second element is Katherine so instead of outputting the haul-away we could echo a particular element so let's echo the second element so just as we did with the the cost variable we use square brackets and we can say 2 for the second element this is going to be carefull remember because it starts with the number 0 so now we just guess they've been eight cuffin so that's a simpler read an array could have integers instead inside a set of strings but let's just stick to the people away for now and have a look at in loops now I find the most useful loop in PHP is they for each loop so we can type for each and this is news to move through an array so we have for each I'm going to type a people as dollar person open and close curly brackets so what this means is for each element in the people array execute the following code but let's the variable person be equal to the current element in the array so we can output constant part in a space we can use single or double quotes for Strings every but it as a string with a space inside this is just going to output each name so what it's doing is it's taking the people array it goes through it until there are no elements left so the first time this code runs person will be said to Alice so I put a l'instant space in loop through the code again and set person to the second element or element number 1 which is Bob and then loop through the cord a third time and sent the person variable to Catherine and then there were no more elements in the array so it will continue executing normally so if we if we create an array of numbers and in this array we have the numbers 5 3 and 7 let's say we want to add up these numbers first we can set a variable called sum and equal and set it equal to zero then we can say for each numbers as number now you can you can call this variable anything you want first variable is the name of the vector consists some unsettled equal to the current value of sum plus the current number the current element in the array which we are processing then afterwards we can echo sum so we should expect 15 if this answer correctly and yes we get 50 some start to zero a loops first it loops through and finds five and it does zero plus five which will give us five system is now equal to five it'll loop through again and find three it'll do a five plus three and give us eight sum is now equal to a 2 the loop through a third and final time and find seven undo 8 and 7 which gives us 15 summers are equal to 15 we echo 15 the user sees 15 and as I mentioned right at the beginning if we view the source all the user sees is 15 it's as though we've just typed 15 straight into the HTML document so that's a really quick basic introduction to PHP I hope you've understood and I hope you have fun practicing stay tuned for some more advanced tutorials i'm swatching
Info
Channel: Jake Wright
Views: 2,276,774
Rating: 4.9006848 out of 5
Keywords: php, tutorial, learn, simple, basic, quick, easy, program, programming, code, web, app, application, script, windows, mac, os x, pc, internet, language, website, jake, wright, howto, how to, write, site
Id: ZdP0KM49IVk
Channel Id: undefined
Length: 15min 0sec (900 seconds)
Published: Tue Jan 15 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.