angelcool Posted February 25, 2011 Share Posted February 25, 2011 Hello I have: function MailTemplate($Type) { ob_start(); include 'mailtemplate.php'; $x=ob_get_contents(); ob_end_clean(); return $x; } Session variables are parsed; however calls to functions do not seemed to work. I'm making a call to a function that returns a value, but this value is not output in the final html. I have double checked that function is available in the template. any suggestions ? Link to comment https://forums.phpfreaks.com/topic/228754-are-custom-functions-available-in-buffered-code/ Share on other sites More sharing options...
angelcool Posted February 25, 2011 Author Share Posted February 25, 2011 ... well after heavy testing the answer is YES. The problem now are with the global variables in side the function (they aren't accessible in buffered code). function ArrayElementToVariable($array) {//this function create a variable for each element in a multidimensional array, the element key name is the variable name, numeric keyed arrays are skipped. if(is_array($array))//prevent foreach error if $array is empty { foreach( $array as $key => $value) { if(is_array($value)) { //Multidimensional Array (Array inside original Array). ArrayElementToVariable($value); } elseif(!is_array($value) && !is_numeric($key)) { //&& !is_numeric($key) <-- do not create a variable for numeric array keys (standard variables cannot start with numbers.), NOT LOGICALLY TO DO SO. //make variable scope outside function global $key; $key=htmlentities(stripslashes($value)); } } } } This function works perfectly in normal code, NOT SO in buffered code (code in original post), a solution is to use extract() in mailtemplate.php This is weird !! Link to comment https://forums.phpfreaks.com/topic/228754-are-custom-functions-available-in-buffered-code/#findComment-1179381 Share on other sites More sharing options...
trq Posted February 25, 2011 Share Posted February 25, 2011 You shouldn't be using globals with functions in the first place. Link to comment https://forums.phpfreaks.com/topic/228754-are-custom-functions-available-in-buffered-code/#findComment-1179382 Share on other sites More sharing options...
angelcool Posted February 25, 2011 Author Share Posted February 25, 2011 y not? ..php does not crash. Link to comment https://forums.phpfreaks.com/topic/228754-are-custom-functions-available-in-buffered-code/#findComment-1179383 Share on other sites More sharing options...
trq Posted February 25, 2011 Share Posted February 25, 2011 Functions are used to encapsulate code, using globals breaks encapsulation. Link to comment https://forums.phpfreaks.com/topic/228754-are-custom-functions-available-in-buffered-code/#findComment-1179415 Share on other sites More sharing options...
requinix Posted February 25, 2011 Share Posted February 25, 2011 php does not crash. That's your excuse? It must be okay because the computer doesn't explode when you do it? Using global variables makes it that much easier to 1. Introduce bugs in your script. All of a sudden you can't use certain names for variables. 2. Introduce magic functionality that relies on some special behavior from PHP. The old session 42 bug was like that. 3. Create security holes. If you assume things that aren't true, malicious users can and will use that against you. 4. Write confusing code. Sure, you can read it and understand how it works, but can anybody else? 5. Develop bad coding practices that are completely useless, if even allowed, in other languages. And before you say "I'll always write PHP", open up your mind and think of your career (assuming you want one in web development). Link to comment https://forums.phpfreaks.com/topic/228754-are-custom-functions-available-in-buffered-code/#findComment-1179417 Share on other sites More sharing options...
angelcool Posted February 25, 2011 Author Share Posted February 25, 2011 php does not crash. That's your excuse? It must be okay because the computer doesn't explode when you do it? Using global variables makes it that much easier to 1. Introduce bugs in your script. All of a sudden you can't use certain names for variables. 2. Introduce magic functionality that relies on some special behavior from PHP. The old session 42 bug was like that. 3. Create security holes. If you assume things that aren't true, malicious users can and will use that against you. 4. Write confusing code. Sure, you can read it and understand how it works, but can anybody else? 5. Develop bad coding practices that are completely useless, if even allowed, in other languages. And before you say "I'll always write PHP", open up your mind and think of your career (assuming you want one in web development). ...you should forward all that to PHP team to get rid of the global keyword. Link to comment https://forums.phpfreaks.com/topic/228754-are-custom-functions-available-in-buffered-code/#findComment-1179427 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.