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

Поделиться
HTML-код
  • Опубликовано: 24 янв 2025

Комментарии • 50

  • @TuncayKaptan
    @TuncayKaptan Год назад +11

    Dani I guess you forgot to delete the exclamation mark(s) on line 12

    • @Dani_Krossing
      @Dani_Krossing  Год назад +7

      Yup I did 🙂 🫣

    • @TuncayKaptan
      @TuncayKaptan Год назад +5

      @@Dani_Krossing hey i love your videos btw, thanks for teaching!!

  • @phpcoderforever
    @phpcoderforever Год назад +3

    The best php teacher thanks alot mr dani complete please and upload more videos

  • @cryptoknight7256
    @cryptoknight7256 Год назад +4

    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 Год назад +1

    Back to PHP. You are a great teacher sir! Well done sir

  • @millennia
    @millennia Год назад +3

    Early returns are great! ❤

  • @greenzone1127
    @greenzone1127 Год назад +3

    Next level, thanks a lot!

  • @Lyrik-Klinge
    @Lyrik-Klinge Год назад +4

    As usual very interesting, THANK you!

  • @UrduHedef
    @UrduHedef Год назад +1

    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. 😅

  • @MyCodingDiary
    @MyCodingDiary Год назад +3

    Your videos are always so informative and well-presented. Thank you for sharing your knowledge with us!🚀😎

  • @scott-richardson
    @scott-richardson 7 месяцев назад

    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.

  • @javabeanz8549
    @javabeanz8549 4 месяца назад +1

    I know it's just a quick demonstration of the concept, but I would think that you would want to test if the SERVER variables were empty, as trying to assign them when they are not there would get errors. I know, because I have had that issue with my own code previously.

  • @seeker3794
    @seeker3794 8 месяцев назад

    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.

  • @PicSta
    @PicSta Год назад +2

    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.

  • @Hefss
    @Hefss Год назад +5

    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.

    • @EfeAsika
      @EfeAsika 4 месяца назад

      What do you recommend using then please?

    • @Aantonya
      @Aantonya 3 месяца назад +1

      ​@@EfeAsikause isset or ?? operator

    • @EfeAsika
      @EfeAsika 3 месяца назад

      @@Aantonya thank you

  • @mortwain1809
    @mortwain1809 Год назад +2

    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

  • @normbograham
    @normbograham Год назад

    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.

  • @fulgorth2661
    @fulgorth2661 8 месяцев назад

    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

  • @mmelimahlobo7656
    @mmelimahlobo7656 Год назад +1

    Awesome❤

  • @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.

  • @Alexifeu
    @Alexifeu Год назад +2

    3;48 From what I have read from multiple Website, Die and Exit are basically the same

  • @darvinde
    @darvinde 7 месяцев назад

    3:48 No, exit and die are exactly the same like aliases. In other languages they differ, but not in php.

  • @KwonDo-Hyung
    @KwonDo-Hyung 6 месяцев назад

    Would it make sense to create external validation classes for certain conditions, that could eventually move to a parent class to make them accessible elsewhere?

  • @ITMarat
    @ITMarat Год назад +1

    Nice

  • @Rabbaaniyy
    @Rabbaaniyy Год назад +2

    Dani, there is an error in the code: we need to check if empty, not if is not empty

  • @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 :)/

  • @VTULab
    @VTULab Год назад

    Thanks ❤

  • @jasonl9630
    @jasonl9630 2 месяца назад

    How about create a different function that pass those arguments, I think that is better for readability and inclusivity of the code, you could also reuse those functions

  • @Stoney_Eagle
    @Stoney_Eagle Год назад +1

    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 😂

  • @patelnwd
    @patelnwd 8 месяцев назад

    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  8 месяцев назад

      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 Год назад +2

    Tomato, tomahto!lol! BTW if it's '!empty', why would you want to exit!?!

    • @Dani_Krossing
      @Dani_Krossing  Год назад +3

      Forgot to change that after I changed the example 🙂 Should just be empty()

    • @missulu
      @missulu Год назад +3

      @@Dani_Krossing I figured that's what you meant!lol! Love the channel, keep up the great work!

  • @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 Год назад

      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.

  • @Trendi_Vibes
    @Trendi_Vibes 2 месяца назад

    Not making value really because this is not valid to brock the code flow like writing exit or die you should end you code in end of function 😮

  • @captaincavemonkey
    @captaincavemonkey Месяц назад +1

    you are wrong, you need more nesting.
    if {
    if {
    if {
    foreach( ifLoops as ifLoop) {
    while(ifLoop === 'true') {
    if {
    }
    }
    }
    }
    }
    }

    • @Dani_Krossing
      @Dani_Krossing  Месяц назад +1

      😮 I have been blind, but now I see!

  • @kdydhncsjfiekdfrhgsdgdfa8987
    @kdydhncsjfiekdfrhgsdgdfa8987 Год назад

    code is clean but not secure. nest is more secure.

  • @chandandsa
    @chandandsa 3 месяца назад

    12