Capture PHP Output and Assign It to a Variable

The CodeIgniter Exceptions.php script has the perfect example of capturing PHP output and storing it in a variable using output buffering.

The following is one of the methods of the CI_Exceptions class:

	 * General Error Page
	 * This function takes an error message as input
	 * (either as a string or an array) and displays
	 * it using the specified template.
	 * @access	private
	 * @param	string	the heading
	 * @param	string	the message
	 * @param	string	the template name
	 * @param 	int		the status code
	 * @return	string
	function show_error($heading, $message, $template = 'error_general', $status_code = 500)

		$message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';

		if (ob_get_level() > $this->ob_level + 1)
		$buffer = ob_get_contents();
		return $buffer;

About samehramzylabib

See About on
This entry was posted in CI Source Code Explained, PHP Code Snippets. Bookmark the permalink.


Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s