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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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
}

 

Link to comment
Share on other sites

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("orders@acme.com");

$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?

Link to comment
Share on other sites

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.