What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » PHP help

PHP help
Author: Message:
lordy
Senior Member
****


Posts: 853
Reputation: 24
34 / Male / Flag
Joined: Jul 2004
Status: Away
O.P. PHP help
Just got a quick question... if I'm creating a function in PHP, and I'm creating variables in said function. How do i make those variables available outside the actual function? I know how to make variables created outside the function available inside the function... but how do you do the reverse?

for example:

code:
$blah = 2;
$blah2 = 4;

function yay() {
global $blah, $blah2;

$minus = $blah2 - $blah;
$plus = $blah + $blah2;
}


I know that doesn't do anything but meh its an example...

if what I understand is correct, to make those variables ($plus and $minus) available to other things outside the function, do i also need to add them to the global statement alongisde $blah and $blah2 inside the actual function??

This post was edited on 12-25-2007 at 01:37 PM by lordy.
12-25-2007 01:15 PM
Profile E-Mail PM Find Quote Report
Eljay
Elite Member
*****

Avatar
:O

Posts: 2949
Reputation: 77
– / Male / –
Joined: May 2004
RE: PHP Variables
You have to define the variable in the global scope first, e.g:
code:
$test = 'foo';

function blah(){
  global $test;
  $test = 'bar';
}

echo $test; // outputs foo
blah();
echo $test; // outputs bar

12-25-2007 01:37 PM
Profile PM Find Quote Report
lordy
Senior Member
****


Posts: 853
Reputation: 24
34 / Male / Flag
Joined: Jul 2004
Status: Away
O.P. RE: PHP help
thanks Eljay,

another question, if I set a return value for a function, inside a loop which is in the function once a certain criteria is met, does that then break the loop?
12-25-2007 01:38 PM
Profile E-Mail PM Find Quote Report
Eljay
Elite Member
*****

Avatar
:O

Posts: 2949
Reputation: 77
– / Male / –
Joined: May 2004
RE: PHP help
quote:
Originally posted by lordy
thanks Eljay,

another question, if I set a return value for a function, inside a loop which is in the function once a certain criteria is met, does that then break the loop?

yes. :P
12-25-2007 01:42 PM
Profile PM Find Quote Report
lordy
Senior Member
****


Posts: 853
Reputation: 24
34 / Male / Flag
Joined: Jul 2004
Status: Away
O.P. RE: PHP help
thankin you muchly master Eljay
12-25-2007 01:44 PM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On