PHP Variable Scopes - Static Variables - Full PHP 8 Tutorial

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

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

  • @glyakk
    @glyakk Год назад +12

    I decided to finally go thought your whole PHP course. I am a working PHP developer but there are some holes in my understanding since I am self taught. This course has been great at helping me fill in some holes and learn about edge cases I was not aware of. Each video is very quick(especially at x2 speed) but they are packed full of information that you also somehow make easy to understand, even at x2 speed! Lets see if I keep up this pace once I hit the second and third chapters ;)

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

      wow watching at x2 speed? I had few complaints that my videos were too fast in first section, but this is good feedback heh. Let me know how it goes in 2nd & 3rd sections

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

      same here. when i go back to 1x speed it seems like you are at 0.5 speed :D

  • @aleksandrkanygin2672
    @aleksandrkanygin2672 3 года назад +3

    I always leave a like after watching the entire video so the youtube algorithm works correctly and hopefully gives you more views :)

  • @rohitpatil3264
    @rohitpatil3264 10 месяцев назад

    I am an experienced developer but for years I was only working with the frameworks so wanted to brush up on my basics again. Your series is just what I wanted, these lessons are on point. Keep up the great work!!👏👏

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

    Love you bro, I am literally understanding everything. Best PHP course ive ever taken

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

    I found in your videos the explanation to problems that made me lose hours.. Thanks for the hard work you did.

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

    Thank you very much I am taking your course continuously it is a wonderful course is increasing my knowledge and I am learning a lot.

  • @sophiac4860
    @sophiac4860 3 года назад +1

    Best PHP tutorial

  • @dhanushkafernando6316
    @dhanushkafernando6316 3 года назад +4

    clean explanation

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

    Yet again I learned something very basic but important. O was seeing static all over the place but I did not know what it means. Now I do 😜 next thing aı want to learn is @

  • @Vitalii-m6r
    @Vitalii-m6r Год назад

    This lesson is pretty useful. Thanks a lot again!

  • @invgreat5608
    @invgreat5608 2 года назад +1

    You are the best!

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

    thank you! your tutorial is great!

  • @ahmedmohammedjayantimochi6904
    @ahmedmohammedjayantimochi6904 Год назад +6

    Really didn't understand the static variable use.
    If you could explain it again in a more simple way, I would appreciate it.
    And thanks by the way for these great tutorials, they helped a lot.

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

      It might make more sense in 2nd section where we cover static properties & methods.

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

      me too didn't get it

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

      Me too have trouble understanding it.

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

      I finally got it bro, watch it a several time and you'll grasp it. The static variable retains the value through the script thus not needing to call the expensive function three times even though it is get assigned null at the beginning. (because of the null check). without the static declaration it gets assigned null every time and thus the expensive function gets called thrice.

    • @mugi1530
      @mugi1530 10 месяцев назад +1

      @@HasinthaWeragala So basically static gets assigned only once, then it will skip this static line, because static value was assigned before and only will be checked for null

  • @FranciscoJunior29
    @FranciscoJunior29 2 года назад

    Thanks for share with us!

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

    Like for the lesson 👍

  • @mostafamahfouz1139
    @mostafamahfouz1139 3 года назад +1

    شكرا لك

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

    great. thanks.

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

    why did this work only for the first function call?
    static $value= null;
    was it excuted only in the first time we called the function?
    if it was executed in the 2nd and 3rd time it would have overwritten the value of $x to null always
    right?
    so, is this what you mean by saying that static variables are cached?
    Edit:
    asked chat gpt, now I know that a static variable can be initialized only once, next re-initializations won't affect it
    now it makes sense

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

    Six years with PHP, and I didn't even know about static variables in function, shame on me :D

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

      Nothing to be ashamed off buddy, you probably did not need it and having static variables in functions is most of the time not a good thing.

  • @jewgienijbrzozowski8451
    @jewgienijbrzozowski8451 3 года назад +2

    For now only 2912 people learns PHP right way on yt

  • @maxibi
    @maxibi 2 года назад +1

    It would be awesome if you can explain about why the running sleep(2) will be called 3 times and you compared the result with PHP CLI. Using PHP CLI (not in browser), you will have the exact sleep delay (2 seconds) with no issue. For browser the sleep would be called 6 seconds.

    • @ProgramWithGio
      @ProgramWithGio  2 года назад

      I explained it at 4:15. It's called 3 times because we are calling the function 3 times, it would be the same in CLI as well it would sleep for 6 seconds unless you add static keyword

    • @maxibi
      @maxibi 2 года назад

      @@ProgramWithGio Did you try, it is not the same.

    • @ProgramWithGio
      @ProgramWithGio  2 года назад

      @@maxibi Yes of course, each time you call getValue() method it sleeps for 2 seconds because it calls the other function within it regardless of where you execute the code unless as I said you add the static keyword in which case it would only execute the expensive function one time.

    • @ProgramWithGio
      @ProgramWithGio  2 года назад

      You can see the result here, I added couple lines to calculate # of seconds it takes to run: i.imgur.com/NMdzsf6.png
      You can also try it here or copy code & try it yourself: jdoodle.com/ia/ls4

    • @maxibi
      @maxibi 2 года назад +1

      @@ProgramWithGio I think we had misunderstanding here, what I meant was the execution 2 seconds between the 3 echos (your example shown as CLI is working fine there). Because, if you use PHP CLI, you will get 2 seconds delay between each echo. Example: Sleep(2) then first echo, then again sleep(2) then the second echo, and finally sleep(2) then the third echo. But for webapp, you will have no output until the program sleep 6 seconds and it will echo all the 3 lines. That is what you should explain because I thought you were about to solve this issue with constant but I was wrong because that was not your intention. You can compare the result from your code you just shown to me with your video. The way the output shown are not the same between PHP CLI and web.

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

    in real life scenario i dont know how exactly you would use this method 5:22. Ususally when you call a function, the value will be dependent on smthing thus different everytime, so it wouldnt really workout...

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

      There are many use cases for static variables & I showed an example in the video. Ideally you would structure your app in a way where you wouldn't need to rely on static variable caching, but it still comes in handy for those situations.

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

      @@ProgramWithGioI see, thanks for the response!

  • @vivamedia5958
    @vivamedia5958 3 года назад

    Thank you Gio for these great tutorials. I learned a lot from you.
    could you please explain more about "caching" that is mentioned in this tutorial?
    does it mean static variables in php sit in memory or heap or something like that? or am I wrong?!
    thanks

    • @ProgramWithGio
      @ProgramWithGio  3 года назад +4

      You're very welcome and thank you. Yes, it's kept in memory for the current request. It's called memoization. I'll talk more about other types of caching in third section of the course using memcached or redis.

    • @vivamedia5958
      @vivamedia5958 3 года назад +1

      @@ProgramWithGio Thank you for your help:)

    • @ProgramWithGio
      @ProgramWithGio  3 года назад

      @@vivamedia5958 you're welcome 👍

  • @Armando-px8hi
    @Armando-px8hi Год назад

    Are we gonna learn about static more in OOP, or is it all we need to know about static Gio?

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

    Hi. How can I switch the displaying of operators in PHPStorm (!=, ==, ===, => etc.) like yours. They are looking pretty good and non-standard on your IDE.

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

      Hey, it's called font ligatures, try enabling it if your IDE supports it

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

      @@ProgramWithGio Okay, thanks 😊
      By the way, the quality of your lessons is very good, I keep learning and it is not boring at all. Thank you very much for these lessons 🤗

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

      @@artyomaghababyan4879 thank you, happy to hear 🙌💙

  • @DMZee101
    @DMZee101 2 года назад

    I use the include method to link the script1 file to the index file. But when I echo out the variable $x in script1.php it says undefined. 0:48

    • @ProgramWithGio
      @ProgramWithGio  2 года назад

      Would you mind sharing screenshot of your code? You can ping me on Twitter, I can help troubleshoot it.

    • @DMZee101
      @DMZee101 2 года назад

      @@ProgramWithGio Thanks Gio. You've solved that already on twiter

  • @patel5532
    @patel5532 2 года назад

    I have doubt at 5:11 in tutorial, when we called getValue() second time, then didn't we assigned the static variable $value to the null (static $value = null), So didn't it execute the someVeryExpensiveFunction() second time?

    • @ProgramWithGio
      @ProgramWithGio  2 года назад

      No because its a static variable, the value was assigned first time

    • @szybkiinwalida8212
      @szybkiinwalida8212 2 года назад

      @@ProgramWithGio it doesn't make sense to me. If we call getValue() the code in function start working from top to bottom untill the return statement,yes? And first thing we're doing in function is assigning null to $value variable, only after that we're checking if it's null or not
      so what? by using static keyword we can't assign a new value (null)? Or maybe execution of function is not exactly from top to bottom and it skips some parts of code related to static variable? Really i can't imagine it by myself how it could work

    • @ProgramWithGio
      @ProgramWithGio  2 года назад +3

      @@szybkiinwalida8212 static works differently than the regular variables, once it's called one time, it is not called again within the same request. Static variables are loaded at compile time so to speak.

  • @subjectfrank
    @subjectfrank 2 года назад

    Hey gio,
    i still don't understand what is the use case of static?
    i am trying to udnerstand the logic rather than programming so i want to know why do we need static variables?

    • @ProgramWithGio
      @ProgramWithGio  2 года назад +1

      You technically don't. It has become kind of an antipattern. It has its use cases but I try to avoid using statics as much as I can. It introduces global state & things can get messy. You might understand static better once you reach second section where we talk about static properties in OOP.

    • @subjectfrank
      @subjectfrank 2 года назад

      @@ProgramWithGio so what you are saying is I must get familiar with oop first in order to understand static variables?

    • @ProgramWithGio
      @ProgramWithGio  2 года назад +2

      @@subjectfrank not exactly, it might just make it easier to understand it if at this stage of learning it does not make sense. Just keep watching and once you complete the second section with static properties and still have doubts, you can ping me on Twitter and we can discuss it further

    • @subjectfrank
      @subjectfrank 2 года назад

      @@ProgramWithGio thank you very much Gio, much appreciated.

  • @subish20me
    @subish20me 2 года назад +3

    i highly doubt if this is a bull blown beginner friendly tutorial ... u quickly skip to the more technical part that makes begineers to loose interest

    • @ProgramWithGio
      @ProgramWithGio  2 года назад +8

      Many beginners have watched this tutorial & had positive feedback. I'm sorry if it's not working out for you. Can you give me an example with timestamp of when I quickly skip to the more technical part? I go into a lot of detail in all of my videos which is why it's over 80 videos & still going instead of being 3hr crash course.

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

    $x = 5;
    function foo(){
    echo $GLOBALS['x'];
    $_GLOBALS['x'] = 10;
    }
    foo();
    echo $x;
    The output was 55 not 510 !!!

    • @ProgramWithGio
      @ProgramWithGio  6 месяцев назад

      Because you are using $_GLOBALS to set to 10 and not $GLOBALS. Change $_GLOBALS to $GLOBALS and it will print 510.
      Also in video I set global value to 10 first then echo like this:
      function foo(){
      $GLOBALS['x'] = 10;
      echo $GLOBALS['x'];
      }
      which will print 1010

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

    great job

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

    gio is too generous to give this course away for free,

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

    at least the price should be a like or a comment

  • @PrasadaReddyG
    @PrasadaReddyG 24 дня назад

    Best PHP tutorial