Jump to content

are custom functions available in buffered code?


angelcool

Recommended Posts

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
Share on other sites

... 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
Share on other sites

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
Share on other sites

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