Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Variable not passing to function

I have the following setup:

class.staff.php

This defines many variables, the one I'm working with right now is $thisuser->getStaffLang();

class.language.php

(Only a function, not a class) This file runs a sql query based on the one variable I pass it from header.inc.php as well as it should pull the staff members unique language ID.

The function is:

function translate($TRANSLATION){

$sql="SELECT $TRANSLATION FROM ".LANGUAGE_TABLE." WHERE LANGUAGE_ID=".$thisuser->getStaffLang;
 $query = mysql_query($sql);
 $translation = mysql_result($query,0);
 print $translation;
 }

header.inc.php

First file I'm working with using this function

example translation entry is:

translate('TEXT_WELCOME_BACK_STAFF');

My problem is that when I'm outside the function $thisuser->getStaffLang; is populated but inside the function it is empty. I really don't want to have to pass the same variable to the function over and over as some files can have upwards of 20 translations in them and that seems like alot of redundant coding. Can someone tell me how in the heck I can get that variable to be recognized by the function without have to pass it to it every single time when calling the function? Hope this wasn't clear as mud. :\

Note: Both class.language.php (where the function is and doesn't work) and header.inc.php (where the variable alone works) have required class.staff.php. So they both should be able to utilize that code/variable.

like image 213
Scott Rowley Avatar asked Oct 20 '25 04:10

Scott Rowley


2 Answers

add global $thisuser; at the beggining of translate()

like image 158
Alon Eitan Avatar answered Oct 22 '25 17:10

Alon Eitan


You need to mark $thisuser as global. See below:

function translate($TRANSLATION)
{
   global $thisuser;     //<----  MUST MARK global

   $sql="SELECT $TRANSLATION FROM ".LANGUAGE_TABLE." WHERE LANGUAGE_ID=".$thisuser->getStaffLang;
   $query = mysql_query($sql);
   $translation = mysql_result($query,0);
   print $translation;
 }
like image 24
Mike Dinescu Avatar answered Oct 22 '25 16:10

Mike Dinescu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!