Stop Doing This in PHP... | Avoid Nesting If Statements | Better Coding Habits in PHP

  Рет қаралды 15,674

Dani Krossing

Dani Krossing

Жыл бұрын

In this video I will show you how to write clean code in PHP, when it comes to conditions. You will learn how to avoid nesting if conditions, to get better coding habits in PHP. Better coding practices is something we should all continue to get better at, so hopefully this tip will give some of you something useful.
Better Coding Habits in PHP, php nesting, php nesting level too deep, avoid nesting if statements, clean code, if else statement, guard clauses, else statement, clean code in php, how to write clean code in php, php tips, php tips and tricks, php tips and tricks advanced, php pro tips, better coding practices, clean code horrible performance, clean code php, php conditions best practices, php best practices coding standards, php less messy conditions
➤ GET ACCESS TO MY LESSON MATERIAL HERE!
First of all, thank you for all the support you have given me!
I am really glad to have such an awesome community on my channel. It motivates me to continue creating and uploading content! So thank you!
I am now using Patreon and KZfaq Memberships to share improved and updated lesson material, and for a small fee you can access all the material either from my memberships or Patreon, depending on your preference. I have worked hard, and done my best to help you understand what I teach.
I hope you will find it helpful :)
Memberships: / @dani_krossing
Patreon: / mmtuts

Пікірлер: 39
@TuncayKaptan
@TuncayKaptan Жыл бұрын
Dani I guess you forgot to delete the exclamation mark(s) on line 12
@Dani_Krossing
@Dani_Krossing Жыл бұрын
Yup I did 🙂 🫣
@TuncayKaptan
@TuncayKaptan Жыл бұрын
@@Dani_Krossing hey i love your videos btw, thanks for teaching!!
@hewrchelsea6622
@hewrchelsea6622 Жыл бұрын
I wouldn't recommend using empty as it takes falsly values as empty (0, 0.0, null, false, etc..) but I get what you're trying to show of course.
@cryptoknight7256
@cryptoknight7256 Жыл бұрын
Great video, as usual! It could have been a great opp to introduce shorthands tho - unless you already made a video about it already. Anyway, thanks for what you do! We need to keep PHP alive :)
@samnako
@samnako Жыл бұрын
Back to PHP. You are a great teacher sir! Well done sir
@millennia
@millennia Жыл бұрын
Early returns are great! ❤
@Izkawi33
@Izkawi33 Жыл бұрын
The best php teacher thanks alot mr dani complete please and upload more videos
@MyCodingDiarie
@MyCodingDiarie Жыл бұрын
Your videos are always so informative and well-presented. Thank you for sharing your knowledge with us!🚀😎
@scott-richardson
@scott-richardson 17 күн бұрын
Obviously there are sometimes cases where a small amount of nested conditionals are probably going to be perfectly fine, but for the most part, this method works really well.
@greenzone1127
@greenzone1127 Жыл бұрын
Next level, thanks a lot!
@UrduHedef
@UrduHedef Жыл бұрын
This advice is so helpful my friend! Thanks for sharing this. I don’t get why ppl are complaining about your „errors“, as this video is about explaining a concept, not a specific piece of code. 😅
@Lyrik-Klinge
@Lyrik-Klinge Жыл бұрын
As usual very interesting, THANK you!
@Dani_Krossing
@Dani_Krossing Жыл бұрын
Thanks for the comment 😊
@fulgorth2661
@fulgorth2661 Ай бұрын
Hi Dani, once again a great video - I know I'm late but I'm reviewing what you did the last years and it is a pleasure!!! I am one of those nesters indeed and I wonder what to do when "if-elsing", what I commonly do when my nesting "happens". My workaround commonly was to extract such nestings into decision-functions. Often this can be useful as similar decisions can be made at several spots inside a project. Is there a way to jump right to the else condition? From my old C-programming times I remember people telling me that jumping points are no good way to programm. I guess thats only if you jump forth and back, do you agree? Best Christian
@Alexifeu
@Alexifeu 11 ай бұрын
3;48 From what I have read from multiple Website, Die and Exit are basically the same
@samnako
@samnako Жыл бұрын
Dani, can you do us a file / image upload, retrieve, delete and update system in php on a live server? I think this almost doesn't exist on youtube. Consider it please. You are a great teacher again.
@mmelimahlobo7656
@mmelimahlobo7656 11 ай бұрын
Awesome❤
@mortwain1809
@mortwain1809 Жыл бұрын
Thnx for this amazing video , Can you explain us that how can we do an image and a video upload , insert , delete , select from database
@ITMarat
@ITMarat 11 ай бұрын
Nice
@seeker3794
@seeker3794 Ай бұрын
You are amazing teacher. Please make a video of how to build php for backend and javascript for frontend together as a fullstack, so we could know how frontend and backend integrates .... There are very few videos out there, that have both js and php included project.
@normbograham
@normbograham 11 ай бұрын
Most nesting, in C/C++, is actually from maintance mode, where the desire, is to change the code the least, but have the new condition. After a few years, you'd added 20+ more conditions (lol). There is one piece of code I remember, that had no validation. The validation occured before it was called. But then things changed. After three years, I'm looking at 3 years of trivial changes, thinking, I would not have done it this way from the beginning. I added a java validation external program, and left the damage in place. But, it got weird. One week at a time. 600+ companies exchanging files, and someone can argue the definition of zero.
@VTULab
@VTULab 10 ай бұрын
Thanks ❤
@PicSta
@PicSta Жыл бұрын
Keep your code lean and mean. Negation is key here 🙂 I always say to friends & family, don't be so negative, but if you're actually in coding mode, you want to be very negative at times.
@basicbeaver
@basicbeaver 11 ай бұрын
Is it a good practice to use so many global variables, though, since all the variables are now in the global scope and not inside an if condition?
@darvinde
@darvinde 9 күн бұрын
3:48 No, exit and die are exactly the same like aliases. In other languages they differ, but not in php.
@towndwarf
@towndwarf Жыл бұрын
I have a legacy project built using a "true Indian coding guidelines": with a nesting level up to 6, and files length up to 5000-8000 lines (20-40K). I hoped, you'll show some ingenious "hack" to solve the nesting ifs' problem, I can utilize, but unfortunately, I know "the catch" which is on your video for quite a time :)/
@user-bx7yk7yn5b
@user-bx7yk7yn5b Жыл бұрын
Dani, there is an error in the code: we need to check if empty, not if is not empty
@Dani_Krossing
@Dani_Krossing Жыл бұрын
Yes 🙂
@dimitridovgan6364
@dimitridovgan6364 Жыл бұрын
In the line 3, shouldn't you use another set of brakets after the exclamation mark? if(! ($_SERVER ..... === "POST")) Or simply use !==
@puwned
@puwned 6 ай бұрын
I don't even understand this code... I think it will cause redirect loop (in case of GET request), because the script is already runs in index.php file.
@Stoney_Eagle
@Stoney_Eagle Жыл бұрын
Sir, do you have something against birds?? If I'm not allowed to nest then I can not make mini 🦅 😢 Callback hell is just as worse 😂
@patelnawada
@patelnawada Ай бұрын
But by this implementation we introduce duplicates in the code for each curly block about the header and exit. It will be very helpful if you give me another solution for the duplicate removal for sem implementation
@Dani_Krossing
@Dani_Krossing Ай бұрын
The duplicate is done on purpose, since it allows us to include separate error/success messages into the URL, when we send the user to a page.
@missulu
@missulu Жыл бұрын
Tomato, tomahto!lol! BTW if it's '!empty', why would you want to exit!?!
@Dani_Krossing
@Dani_Krossing Жыл бұрын
Forgot to change that after I changed the example 🙂 Should just be empty()
@missulu
@missulu Жыл бұрын
@@Dani_Krossing I figured that's what you meant!lol! Love the channel, keep up the great work!
@kdydhncsjfiekdfrhgsdgdfa8987
@kdydhncsjfiekdfrhgsdgdfa8987 7 ай бұрын
code is clean but not secure. nest is more secure.
Каха ограбил банк
01:00
К-Media
Рет қаралды 10 МЛН
The joker's house has been invaded by a pseudo-human#joker #shorts
00:39
Untitled Joker
Рет қаралды 10 МЛН
КАРМАНЧИК 2 СЕЗОН 7 СЕРИЯ ФИНАЛ
21:37
Inter Production
Рет қаралды 252 М.
How Senior Programmers ACTUALLY Write Code
13:37
Thriving Technologist
Рет қаралды 1,4 МЛН
Don't Model the Problem
14:32
David Hart
Рет қаралды 9 М.
PHP 8 3 Released
11:03
ThePrimeTime
Рет қаралды 100 М.
Would I still use PHP in 2023?
9:51
Stefan Mischook
Рет қаралды 31 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,7 МЛН
Why You Shouldn't Nest Your Code
8:30
CodeAesthetic
Рет қаралды 2,6 МЛН
Should You Learn Object Oriented PHP?
13:48
Dani Krossing
Рет қаралды 10 М.
Is PHP the Secret King of Code?
6:42
Stefan Mischook
Рет қаралды 53 М.
Каха ограбил банк
01:00
К-Media
Рет қаралды 10 МЛН