How to optimize your PHP code || 3 Useful Tips

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there this is Kemal and I just wanted to say that a few days back I was trying to create a project in PHP which I wanted to share with you guys on YouTube and when I was trying to create that project I had encountered a bug and it took me some time to actually find the bug and rectify it but at that point as real as the fact that it was actually quite easy for me to find the bug and rectify the error which would have been quite impossible for me the year back when actually started web development and the reason why I was able to clear out that bug and figure out the reason why that was happening was because of the things that I've learned for the past year and things that aren't implemented to make my book flow easy and optimize my code as much as I could so I thought why not create a video explaining the three main things that I'd learn for the past year which might be helpful for you as well so here's a video on how to optimize your PHP code so let's get this started so the first tip that I can give you guys is write a lot of comments I know you've heard it many times and I can't the stress on the fact how important it is to write comments see the thing is that when you're trying to create the project yeah everything in your mind you know what to do and you know what you have planned to do next so just execute that and you you are able to create that project successfully but let's say you're done with the project and you keep it aside fill us a few months or years maybe and if you come back and if you look at the code it will be a mess like literally and you won't even be able to understand why you have written that function at that point so at this point comments are quite useful as they make you understand the reason behind why you had written that particular function for that particular logic at all so writing comments was like a best practice any programmer can have they make your code more understandable when not just you but for everyone else who tries to use your code or who tries to understand and implement your code as well so writing a comment does like the essence of a programmer so make sure to write a lot of comments alright so the second tip that I can give you is to optimize your code by dividing it into paths what I mean by that is that let's say you have a project and in that project you have three files the first one is the index file the second one is the noggin file and third one as the about five and in these three files you have the header the navigation bar and the footer which are common so it's not writing the header navigation and footer individually for three of these files so many times what you can do is that you can just remove that from that file and place it in individual files so what I do generally was that I pick the header and the navigation and put that inside a file called as header similarly I take the photo and place it inside a file called a scooter and I just include that the top and bottom of each and every file that I want to use it in and automatically by doing that I just reduce the duplicate code and I've just written it once and I can just use it as many times as want and one another benefit that you have by doing this because that let's say you have to do some changes in the navigation bar like I have to change the links or something else you just have to go to the header file go to the navigation section and change the specific thing that you want to do automatically once you're done with that it will be applied to each and every page which will have been quite impossible to do in the previous project structure as you had to go to each and every file that has the navigation and we had to change that logic so by doing this you're just essentially removing the duplicates and you're just trying to have one set of component and you're just trying to include that in each and every file all right so the third tip that I can give you is to use the MVC architecture so the MVC pattern is a design principle so design principles are like a set of rules that you generally follow when I'm trying to create the project and in that design principles MVC is a type of quality logged way of naming your projects and it's files in a particular order so that whenever another person tries to identify your project and tries to understand the project structure she will be able to easily identify as you trying to follow a set of norms and that norms does the MVC architecture so that's not complicated so MVC has three main parts the first one is the M that is model and a model generally counsels of all the files related to database so specifically for PHP the files we have which are related to database our my database connection file which is named as DB dot PHP which has a particular function that just connects the database and returns a variable connection that's it that's the only thing that I have and that is placed inside the model and some of the projects might have other files related to database and all of those files are generally kept under the model folder and the next one is V it stands for view so view is generally the part which is visible to the user so all the pages on your website are generally placed under the view category so you just keep your index file you log in file your eyeball a file and all the other files inside the view folder but the problem here is that if you don't have your index file inside the root folder and you try to open the website what happens is that the browser doesn't know what is the homepage so it doesn't have that index location right so what you have to do is you have to write another file which Maps the URL to the index file path so when you're trying to access a project inside the browser you just have to redirect the browser to the view folder and inside that view folder you have to redirect it to the index file so it's actually quite a bit of tiring thing to do so what I do is that I don't keep my index file inside the view folder I just keep that in my root folder and all the other pages inside my project are generally kept under the view category so the third one is the C which stands for controller okay so before going further let me first clarify a few things I am NOT an expert in MVC architecture and I have no idea about all the other design principles and and I'm not that good at NBC as well I just have a broad idea of what embraces is and how the project structure is defined and there is something that I've developed on my own for the past year trying to implement things and trying to categorize things work they are easy for me to understand and I think it is pretty easy for any other person who tries to look at my project for the first time I think they will be able to understand the structure that I'm falling right now ok so going back to the controller so this controller is like the brain of a project so in the controller I generally have four types of files the first one as the functions dot PHP and in that functions dot PHP I generally write all my functions which are being used across my project so if I have to write a function to retrieve the data from the database I destroyed a particular function in the functions PHP file and I just include that file in whichever file needs that particular function and I just executed so that's the use of the functions file the second one is the retrieve file so what does retrieved what PHP file does is that when the data is being passed in the PHP it generally happens to the URLs right so when the data is being passed through the URL this retrieve file takes that data and assigns that data to individual variables which I can access across my project so that's a use for the rotary file so all my retrievals are being managed by a single file so I just have to go to that file if I want to change something related to the you are in retrievals at all so the third type of file that I have there's not a file actually it's a folder and it's named as logic so all the logic for all the pages that have in the project are being written in this logic folder so what I mean by that so let's say I have my page called as login and have to verify user right so all the details are all the logic regarding the authorization and authentication of a user is being written inside this logic folder so what I do is that for every view that is every page that I have in a project I create a subsequent logic file so let's say I have login dot PHP so for that inside my logic folder I have a file called as login logic dot PHP so this is the logic for the login file similarly for the index file as well I have index logic and for the about page have the about logic so each and every file inside the view folder has a corresponding new logic file inside the logic folder under the control of holder so a last file that I use there's the includes file okay so let's keep the include file aside for now and let's try to implement this so what happens is that for the index page that is the main page I just wanted to have the functionality of functions the logic and all the other pages as well so if you want to include each and every file inside my index file have to write multiple include statements connecting to each and every individual file and increases the number of into old statements inside my index file and that just makes it look a bit more messy right so what I do is that I just take all the in clothes that I want and just place it inside the includes file and just include this particular file inside my index page so it looks something like this so inside my index page I have include quotation include dot PHP and inside my control dot PHP I have all the includes that I want to be there inside my index page okay now so let's just understand the control flow inside this project so let's say I have a pH coitus login and in that I type in the username and password and this click on log in and what happens is that the data is being passed through the URL and the Retiro file this digs at URL data and parses it and gives the data to two variables coil as username and password since that username and password are now said what I do is that inside my log in logic I write the code that so the data is being parsed and it has been given to the user name and password variables I'll just execute a particular function which retrieves the data from the database and just validates the database values with the data being given from the free 10-part just to check whether the user username and password are correct or not and if the data validation returns true that means the username and password are correct so I'll just redirect the user to the index page with the data of his personal profile and the function here that I'm using inside the login logic to retrieve the data from the database is being given to me by the functions dot PHP so that is a flow of control in cinema project so this is something that I've developed on my own so if you wanted you can use it try to implement on your own projects and I can assure you that that's going to make it a lot easier for you to identify the errors since you don't have to go to each and every file and search for that particular error all the code has been modularized that means each and every section are in their respective positions you just have to go to that file and check the file that's it so this cried out see if it works for you or not and I hope that you have an idea of what I generally do inside my own projects so that's it for this video guys I hope you liked what I watched in now if you like what you're watching now and then please in the like button I'm going to subscribe as well thanks so much for watching and I hope to see you in the next video [Music]
Info
Channel: packetcode
Views: 8,259
Rating: undefined out of 5
Keywords: how to optimize php code, efficient php code, php pro tips, php tips, packetcode tutorial, how to optimize your code, packetcode, kamal teja, php, php tips and tricks, web development, tips and tricks, pro tips, programming tips, file organizing, php file organizing, how to organize php, how to organize php project structure, how to organize php code, php project structure, ideal php project structure, clean code, Learn to code
Id: TgrpEb-8qSY
Channel Id: undefined
Length: 11min 4sec (664 seconds)
Published: Tue Apr 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.