jayhawker Posted March 31, 2011 Share Posted March 31, 2011 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 https://forums.phpfreaks.com/topic/232338-i-am-new-to-php-and-trying-to-debug-some-old-code-how-am-i-to-reade-the-follow/ Share on other sites More sharing options...
mattal999 Posted March 31, 2011 Share Posted March 31, 2011 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 https://forums.phpfreaks.com/topic/232338-i-am-new-to-php-and-trying-to-debug-some-old-code-how-am-i-to-reade-the-follow/#findComment-1195233 Share on other sites More sharing options...
AbraCadaver Posted March 31, 2011 Share Posted March 31, 2011 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 https://forums.phpfreaks.com/topic/232338-i-am-new-to-php-and-trying-to-debug-some-old-code-how-am-i-to-reade-the-follow/#findComment-1195237 Share on other sites More sharing options...
DavidAM Posted March 31, 2011 Share Posted March 31, 2011 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 https://forums.phpfreaks.com/topic/232338-i-am-new-to-php-and-trying-to-debug-some-old-code-how-am-i-to-reade-the-follow/#findComment-1195239 Share on other sites More sharing options...
mattal999 Posted March 31, 2011 Share Posted March 31, 2011 Ah, didn't know that - thanks! Link to comment https://forums.phpfreaks.com/topic/232338-i-am-new-to-php-and-trying-to-debug-some-old-code-how-am-i-to-reade-the-follow/#findComment-1195244 Share on other sites More sharing options...
AbraCadaver Posted March 31, 2011 Share Posted March 31, 2011 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 https://forums.phpfreaks.com/topic/232338-i-am-new-to-php-and-trying-to-debug-some-old-code-how-am-i-to-reade-the-follow/#findComment-1195245 Share on other sites More sharing options...
AbraCadaver Posted March 31, 2011 Share Posted March 31, 2011 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 https://forums.phpfreaks.com/topic/232338-i-am-new-to-php-and-trying-to-debug-some-old-code-how-am-i-to-reade-the-follow/#findComment-1195262 Share on other sites More sharing options...
jayhawker Posted March 31, 2011 Author Share Posted March 31, 2011 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? Link to comment https://forums.phpfreaks.com/topic/232338-i-am-new-to-php-and-trying-to-debug-some-old-code-how-am-i-to-reade-the-follow/#findComment-1195305 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.