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 ;)
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
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!!👏👏
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 @
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.
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.
@@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
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
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.
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 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.
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
@@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.
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...
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.
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
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.
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 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 🤗
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 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
@@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.
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?
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 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
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.
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
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 ;)
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
same here. when i go back to 1x speed it seems like you are at 0.5 speed :D
I always leave a like after watching the entire video so the youtube algorithm works correctly and hopefully gives you more views :)
Thank you 💙💙
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!!👏👏
Great to hear, thank you
Love you bro, I am literally understanding everything. Best PHP course ive ever taken
Happy to hear 💙, thank you
I found in your videos the explanation to problems that made me lose hours.. Thanks for the hard work you did.
Happy to hear this, thank you
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.
That's great, happy to hear 🙌
Best PHP tutorial
clean explanation
Thank you 🙌
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 @
Nice, happy to hear
This lesson is pretty useful. Thanks a lot again!
Glad to hear that!
You are the best!
💙💙
thank you! your tutorial is great!
You are welcome 💙
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.
It might make more sense in 2nd section where we cover static properties & methods.
me too didn't get it
Me too have trouble understanding it.
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.
@@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
Thanks for share with us!
Sure thing, thanks for the support
Like for the lesson 👍
Thank you
شكرا لك
You're welcome 👍
great. thanks.
You're welcome
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
Yup exactly
Six years with PHP, and I didn't even know about static variables in function, shame on me :D
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.
For now only 2912 people learns PHP right way on yt
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.
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
@@ProgramWithGio Did you try, it is not the same.
@@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.
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
@@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.
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...
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.
@@ProgramWithGioI see, thanks for the response!
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
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.
@@ProgramWithGio Thank you for your help:)
@@vivamedia5958 you're welcome 👍
Are we gonna learn about static more in OOP, or is it all we need to know about static Gio?
Yes there is more in OOP section
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.
Hey, it's called font ligatures, try enabling it if your IDE supports it
@@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 🤗
@@artyomaghababyan4879 thank you, happy to hear 🙌💙
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
Would you mind sharing screenshot of your code? You can ping me on Twitter, I can help troubleshoot it.
@@ProgramWithGio Thanks Gio. You've solved that already on twiter
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?
No because its a static variable, the value was assigned first time
@@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
@@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.
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?
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.
@@ProgramWithGio so what you are saying is I must get familiar with oop first in order to understand static variables?
@@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
@@ProgramWithGio thank you very much Gio, much appreciated.
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
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.
$x = 5;
function foo(){
echo $GLOBALS['x'];
$_GLOBALS['x'] = 10;
}
foo();
echo $x;
The output was 55 not 510 !!!
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
great job
Thank you
gio is too generous to give this course away for free,
💙💙
at least the price should be a like or a comment
💙💙
Best PHP tutorial
Thank you 💙