Jump to content

Recommended Posts

I took the following code from http://www.w3schools...p_variables.asp

 



<?php
$a = 5;
$b = 10;

function myTest()
{
global $a, $b;
$b = $a + $b;
}

myTest();
echo $b; (this outputs 15)
?>

 

The echo of $b comes after the closing of the function. The output is 15. If you add another echo inside the function like below:

 


<?php
$a = 5;
$b = 10;

function myTest()
{
global $a, $b;
$b = $a + $b;
}
echo $b; (this one outputs 10)
myTest();
echo $b; (this one outputs 15)
?>

 

then the output for that echo is 10.

 

I understand that the function is calling the global variables, but shouldnt the echo inside the function equal 15 and the one outside equal 10?, since it only refers to the global "$b" and is outside the function? Or does the global call inside the function completely change $b for the rest of the script?

 

Thanks guys (and gals)

. . . shouldnt the echo inside the function equal 15 . . .

 

There is no echo inside the function. I'm not sure which echo you are referring to but the output is exactly as I would expect.

 

When the script runs it will follow this logic:

 

Define $a and $b as 5 and 10, respectively

Define the function myTest()

Echo the value of $b (which is currently 10 as defined at the beginning of the script)

Call the function myTest() which . . .

- Makes the scope of $a and $b global (so they have the values as defined outside the function)

- Redefines $b as the sum of $a and $b (5 + 10) which is 15

- Function ends, execution picks up from where the function was called

Echo the value of $b (which is now 15 since it was modified globally within the function)

Edited by Psycho

What global does in php is indicate that the variables inside the function should be references to global variables of the same name. If you change them inside the function, you change the global variable.

More to the point:

 

1. W3Schools sucks as a learning resource. It is not affiliated in any way with the W3C, and is notorious for giving out false information. If you want to learn PHP, get yourself a good book (I recommend starting with Larry Ullman's: http://www.amazon.com/PHP-MySQL-Dynamic-Web-Sites/dp/0321784073/ref=sr_1_2?s=books&ie=UTF8&qid=1359031826&sr=1-2&keywords=ullman) and look at the documentation on the official PHP website.

 

2. You should learn what 'global' is and how it works, but don't use it in your own code. There's a far better, cleaner, more readable way to send values to a function. 'global' is a surefire sign of doing it wrong, and if any of the resources you're currently reading/using to learn PHP feature it, that means they're a bad resource.

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.