Jump to content

I am new to PHP and trying to debug some old code. How am I to reade the follow


jayhawker

Recommended Posts

I am new to PHP and trying to debug some old code.  How am I to read the following:

 

$properties = $GLOBALS['Props'];

 

I am guesisin that it is saying:

 

Create a string variable named $porperties and set it equal to the Global variable of 'Props'.

 

But I assume my interpretation is wrong since I get some weird results.

Well, $GLOBALS is an array (that I assume is user defined).

 

You're correct in your theory that the variable $properties is assigned a value, but that value is actually a value from an array.

 

I'm assuming that you know what an array is. If not, then you have some reading to do.

 

Basically, $GLOBALS['Props'] would retrieve the value from the array associated with the key Props.

Well, $GLOBALS is an array (that I assume is user defined).

 

You're correct in your theory that the variable $properties is assigned a value, but that value is actually a value from an array.

 

I'm assuming that you know what an array is. If not, then you have some reading to do.

 

Basically, $GLOBALS['Props'] would retrieve the value from the array associated with the key Props.

 

$GLOBALS is a PHP created super global array that contains all global variables.

Actually, $GLOBALS is a predefined array that references all variables in global scope.  The manual provides more information on it. But basically, if you define a variable (in global scope) you can get to it through this array.

 

$myVar = "This is a globally defined variable";

echo $GLOBALS['myVar']; // will output: This is a globally defined variable

 

 

Actually, $GLOBALS is a predefined array that references all variables in global scope.  The manual provides more information on it. But basically, if you define a variable (in global scope) you can get to it through this array.

 

$myVar = "This is a globally defined variable";

echo $GLOBALS['myVar']; // will output: This is a globally defined variable

 

Yes, and really the only practical use is to access global vars when not in global scope:

 

// global scope
$myvar = 'something';
echo $myvar;  // echos something
test();
echo $myvar;  // echos nothing

function test() {
   // local scope while in test
   echo $GLOBALS['myvar'];  // echos something
   $GLOBALS['myvar'] = 'nothing';
   echo $GLOBALS['myvar'];  // echos nothing
}

If you want to check all post entries:

 

if(array_diff_key($_POST, array_filter($_POST))) {
    //redirect with error
} else {
    //continue
}

To just do certain ones, something like this (may be an easier way but I'm headed out):

$required = array('name'=>true, 'email'=>true);

if(!array_intersect_key($required, array_filter($_POST))) {
    //redirect with error
} else {
    //continue
}

 

Thanks for the replies.  That cleared up some confusion for me.

 

I still have a problem though.

 

Trying to set a variable = to a value in following way, I can't get access to it in a function for some reason.  But I konw the variable has a value because If I echo it outside the function I am trying to call it from I get the correct results on the screen.

 

$colname_rsOrderInformation = "-1";

if (isset($_SESSION['KT_kartOrderId'])) {

  $colname_rsOrderInformation = (get_magic_quotes_gpc()) ? $_SESSION['KT_kartOrderId'] : addslashes($_SESSION['KT_kartOrderId']);

}

mysql_select_db($database_t_shop, $t_shop);

$query_rsOrderInformation = sprintf("SELECT * FROM order_ord WHERE id_ord = %s", GetSQLValueString($colname_rsOrderInformation, "text"));

$rsOrderInformation = mysql_query($query_rsOrderInformation, $mx_shop) or die(mysql_error());

$row_rsOrderInformation = mysql_fetch_assoc($rsOrderInformation);

$totalRows_rsOrderInformation = mysql_num_rows($rsOrderInformation);

 

$test = $row_rsOrderInformation['email_ord'];  //email_ord exists in the table and a value will show up if echoed outside of the function but it won't show up with-in the function.

//$test = 'test1';  // If is use this value for the variable, it will show up in the function.

function Trigger_SendEmail(&$tNG) {

  $emailObj = new tNG_Email($tNG);

  $emailObj->setFrom("[email protected]");

$emailObj->setTo("{email_ord}");

  $emailObj->setCC("");

  $emailObj->setBCC("");

 

  $emailObj->setContent($GLOBALS['test']);  //Note: I will get a correct value here if $test is set to anything other then the value of a field from the table

 

  $emailObj->setEncoding("ISO-8859-1");

  $emailObj->setFormat("Text");

  $emailObj->setImportance("Normal");

  return $emailObj->Execute();

}

//end Trigger_SendEmail trigger

 

 

What am I doing wrong?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.