We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Echo/Print in Controller

Hi, Can I actually echo or print a variable in Controller? I noticed that I need to put it first in the View in order to print the variable. This is for testing purposes only such that I know what data/value is the variable contains.

below is the code that is not working class PollController extends \Phalcon\Mvc\Controller {

public function indexAction()
{
    echo "HELLO";
}


98.9k

You need to tell Phalcon\Mvc\View where to place the output generated in the controller on the current view:

<?php echo $this->view->getContent(); ?>

Volt:

{{ content() }}


10.8k

@Phalcon what i mean is I don't want to use the Volt or the View for the printing... i want it to be printed directly in Controller is this possible?

die(var_dump($var));
class PollController extends \Phalcon\Mvc\Controller
{

    public function indexAction()
    {
        $this->view->disable();
        echo "HELLO";
    }
}


10.8k

thank you guys :D

edited Jun '16

define yourself a helper function and conditionally disable output buffering:

/**
 * Functional replacement for "echo $var" without the need to disable something or to die.
 */
function echof($var) {
   echo $var;
   ob_get_level() && ob_flush();    // flush output buffer if active
}