0

I have two functions that print some values. I need to use the variable $mv in the second function. However, $mv can only be defined in the first function. I have tried all types of PHP global examples and none of them has allowed the $mv variable to be used or visible or accessible in the second function.

 function printMobilePrev(&$mobileprevresults) { if (count($mobileprevresults->getRows()) > 0) { $mv = $mobileprevRows[0][0]; $mobileprevRows = $mobileprevresults->getRows(); echo '<p>Previous period (sessions): '.$mobileprevRows[0][0].'..............................'; } else { print '<p>No results found.</p>'; } } function printMobileCurr(&$mobilecurrresults) { if (count($mobilecurrresults->getRows()) > 0) { $mobdiff = ($mobcur - $mv); $mobpctchg = ($mobdiff / $mobprev) * 100; $mobilecurrRows = $mobilecurrresults->getRows(); echo '<p>Previous period (sessions): '.$mobileprevRows[0][0].'..............................'; echo '<p>Previous period (sessions): '.$mv.'..............................'; echo '<p>Current period (sessions): '.$mobilecurrRows[0][0].'..............................'; if ($mobdiff > 0){ echo '<p>Percent increase: '.$mobpctchg.'..............................'; } else { echo '<p>Percent decrease: '.$mobpctchg.'..............................'; } } else { print '<p>No results found.</p>'; } } 

5 Answers 5

1

You can use the global scope:

That is what you want to do:

$mv = 0; function function1() { global $mv; $mv = 'whatever'; //code } function function2() { global $mv; // use $mv; } 
Sign up to request clarification or add additional context in comments.

1 Comment

the first global $mv; does nothing
0

You have to predefine that variable OUTSIDE any function, and then you can use Global to get that to any function.

1 Comment

It doesn't need to be defined outside. It just needs to have the global keyword inside the functions that use it, even if it's initialized there. Or better $GLOBALS['mv'] or better still return it from the first and pass to the second.
0

You can pass it by reference. For example

function doSomething(&$mv) { $mv = 1; } function doSomethingElse($mv) { return $mv; } $mv = 0; doSomething($mv); echo doSomethingElse($mv); //Output: 1 

Comments

0

You could return $mv after your print and save that in a var to pass to the next function:

$printMobilePrev = printMobilePrev(); function printMobilePrev(&$mobileprevresults) { $mv = $mobileprevRows[0][0]; ... print '<p>No results found.</p>'; return $mv; ... } $printMobileCurr = printMobileCurr(&$mobilecurrresults,$mv); function printMobileCurr(&$mobilecurrresults,$mv) { ...... } 

Comments

0

Most likely you have to make a correct use of globals.

declare your $mv variable as global before asigning it a value on your first function

global $mv; $mv = $mobileprevRows[0][0]; 

use global at the begining on your second function before using it

function printMobileCurr(&$mobilecurrresults) { if (count($mobilecurrresults->getRows()) > 0) { global $mv; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.