Jump to content

Learning PHP static scope


Acebars

Recommended Posts

Hello everyone,

 

I'm on w3schools getting used to PHP, I'm having issue getting around the following:

 

<!DOCTYPE html>
<html>
<body>

<?php

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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 by KevinM1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Acebars
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.