Acebars Posted August 6, 2013 Share Posted August 6, 2013 Hello everyone, I'm on w3schools getting used to PHP, I'm having issue getting around the following: <!DOCTYPE html><html><body><?phpfunction myTest(){static $x=0;echo $x;$x++;}myTest();myTest();myTest();?> </body></html> The result being 012? if I take away one of the myTest(); its becomes 01? Why on earth is this happening and what does the $x++ do? I've searched high and low for an answer to no avail. Quote Link to comment https://forums.phpfreaks.com/topic/280903-learning-php-static-scope/ Share on other sites More sharing options...
AbraCadaver Posted August 6, 2013 Share Posted August 6, 2013 function myTest() { static $x=0; //static means this var keeps its value across each function call echo $x; $x++; // This just increments the value of $x by 1 } myTest(); //0 myTest(); //1 because it was incremented last time myTest(); //2 because it was incremented last time Contrast with not static: function myTest() { $x=0; //local non-static means this var disappears at the end of the function echo $x; $x++; // This doesn't matter because $x will disappear } myTest(); //0 myTest(); //0 myTest(); //0 Quote Link to comment https://forums.phpfreaks.com/topic/280903-learning-php-static-scope/#findComment-1443716 Share on other sites More sharing options...
Zane Posted August 6, 2013 Share Posted August 6, 2013 $x++ is the same thing as $x = $x + $x Quote Link to comment https://forums.phpfreaks.com/topic/280903-learning-php-static-scope/#findComment-1443717 Share on other sites More sharing options...
Acebars Posted August 6, 2013 Author Share Posted August 6, 2013 Thank you, you guys are great, wish the ++ was explained on w3! Quote Link to comment https://forums.phpfreaks.com/topic/280903-learning-php-static-scope/#findComment-1443718 Share on other sites More sharing options...
Zane Posted August 6, 2013 Share Posted August 6, 2013 w3schools is not the place to learn programming languages effectively. Try tutorials or books elsewhere. http://www.w3fools.com/ Quote Link to comment https://forums.phpfreaks.com/topic/280903-learning-php-static-scope/#findComment-1443719 Share on other sites More sharing options...
Acebars Posted August 6, 2013 Author Share Posted August 6, 2013 Thanks Zane, I am under time constraints and I find reading huge books is very time consuming to learn. I am fairly good at HTML and CSS and learnt from a very good free online tutorial from a totally independent russian guy, have created not bad sites but my PHP is poor. I tried a few Lynda.com video courses on PHP and Wordpress theming but I found them poor and mind numbing, not because it's boring stuff but because they are so terrible at teaching, they spend so much time on trivial stuff speaking to you as if you have the IQ of a chicken then gloss over the important stuff as if it's nothing. Could you suggest any comprehensive video tutorials for PHP? Quote Link to comment https://forums.phpfreaks.com/topic/280903-learning-php-static-scope/#findComment-1443721 Share on other sites More sharing options...
requinix Posted August 6, 2013 Share Posted August 6, 2013 $x++ is the same thing as $x = $x + $x(cough) Quote Link to comment https://forums.phpfreaks.com/topic/280903-learning-php-static-scope/#findComment-1443724 Share on other sites More sharing options...
KevinM1 Posted August 6, 2013 Share Posted August 6, 2013 Could you suggest any comprehensive video tutorials for PHP? To be honest, I don't think there are any. I've seen Lynda's stuff, and wasn't impressed. And even something like Plural Sight (which is predominantly Microsoft) is a bit lacking. Time constraints + learning to program is never a good combination. Learning to program well takes time. And the best resources I've found have been books. Quote Link to comment https://forums.phpfreaks.com/topic/280903-learning-php-static-scope/#findComment-1443731 Share on other sites More sharing options...
Zane Posted August 6, 2013 Share Posted August 6, 2013 (cough) $x + 1 I mean, d'oh Quote Link to comment https://forums.phpfreaks.com/topic/280903-learning-php-static-scope/#findComment-1443732 Share on other sites More sharing options...
KevinM1 Posted August 6, 2013 Share Posted August 6, 2013 (edited) $x++ is the same thing as $x = $x + $x (cough) Actually, ++ is the increment operator. You can have it as either ++$x;, OR $x++;. And there IS a (slight) difference between the two. Test the following two examples: $i = 0; $j = $i; while ($j < 5) { echo "$i"; $j = ++$i; } $i = 0; $j = $i; while ($j < 5) { echo "$i"; $j = $i++; }If you want to shorten something like: $x = $x + 5;, you do it with $x += 5; Edited August 6, 2013 by KevinM1 Quote Link to comment https://forums.phpfreaks.com/topic/280903-learning-php-static-scope/#findComment-1443734 Share on other sites More sharing options...
boompa Posted August 6, 2013 Share Posted August 6, 2013 (edited) What is with people's fascination with watching people write code on a screen via video tutorials? Edited August 6, 2013 by boompa Quote Link to comment https://forums.phpfreaks.com/topic/280903-learning-php-static-scope/#findComment-1443752 Share on other sites More sharing options...
KevinM1 Posted August 7, 2013 Share Posted August 7, 2013 What is with people's fascination with watching people write code on a screen via video tutorials? You can see exactly what someone writes, and then immediately see if it works or not. Newbies tend to be gun shy about writing code if there's a good chance it will have errors. That's why a lot of the questions we get are, "Will this code snippet work?" It takes most people a while to both learn that errors aren't bad, and how to actually use errors in a productive way during debugging. Quote Link to comment https://forums.phpfreaks.com/topic/280903-learning-php-static-scope/#findComment-1443767 Share on other sites More sharing options...
Acebars Posted August 7, 2013 Author Share Posted August 7, 2013 (edited) What is with people's fascination with watching people write code on a screen via video tutorials? Simply because it's fast and I can get the jist.. At a later date I can then properly revise the material with books etc. There is a lot of fluff in many books and tutorials, I can skip that on videos, like Lynda whose tutors I would love to punch in the face for patronising the people watching their vids. I'm trying to hold down a job, restore an old motorcycle, and learn PHP all at the same time as well as having to go to hospital from time to time, I have little time and learning from books is the most time consuming. Edited August 7, 2013 by Acebars Quote Link to comment https://forums.phpfreaks.com/topic/280903-learning-php-static-scope/#findComment-1443834 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.