Best way to return html from php function?

So, I asked a similar question before, and now I'm only more confused, so I will ask about it differently ...

What I'm trying to do is abstract my php and put the html bits used in my validator as error messages in functions that return html to where they are needed on the page.

What I have now:

<section>
<?php 
if($errMsg)
{
    errMsg();
}
?>
</section>

<?php
function errMsg()
{
       ?>
       <section>
           <p>O crap!  There was an error</p>
       </section>
       <?php
}
?>

But in my previous question I was told that doing it this way is a "dirty hack", and it is better to use it in such situations. But to use return, I will need to assign all the returned html to var and to do this, I will need to put html in the string. I would prefer not to add more than two html lines to the line due to the pain in the butt of the number of quotes needed for this.

, heredoc, ob_start/ob_get_clean. .

1 , ? 2 , ? /
3 - ?: -\

+5
7

HTML. , html . , .

+4
  • , , , .
  • HTML PHP , , - , , , PHP .
  • ! , !?

Zend http://framework.zend.com/manual/en/zend.application.quick-start.html, , , , .

:

try{
     //do work here and throw exception on error
}
catch (Exception $e)
{
     //do error logging/display error
     //or preferably load another class which handles your error and produces output
}
+4

1: , , .

2: EOF PHP error func, :

function errMsg()
{
       $error = <<<EOF 
<section>
<p>O crap!  There was an error</p>
</section>
EOF;
}
+4

:

<?php if($errMgs){ ?>
<section>
    <p><?php echo errMsg(); ?></p>
</section>
<?php } ?>

, HTML PHP. ? , , , / , HTML .

+2

html- , error.php. :

function errMsg()
{
    ob_start();
    include '/path/to/error.php';
    return ob_get_clean();
}

// ...

echo errMsg();
+2

-, . , , , .

-, , . , , , - :

class userObject
{
    public $history;// = historyObject;
    public $userName;
    public $userRelation;
    public $userCode;
    private $mySQLAccessData;
    private $isDebug=false;

    public function makeHistoryObject($KPI, $KPIType)
    {
        if($this->isDebug){echo "Having a go at creating ".$this->userName.".<br>";}
        $this->history = new historyObject($this->userCode, $KPI, $KPIType);
    }
}

private, false . , true, / . , , , , , . , , , . , , .

+2

- , :

<?php
$errors = array();

if (!$item = getItem($id)) {
    addError("No item $id");
}

if (!updateUser($user, $id)) {
    addError("Can not update user");
}

if (!updateItemPicture($id, $picture)) {
    addError("Can not add picture to $id");
}

function addError($error) {
    global $errors; $errors []= $error;
}

function hasErrors() {
   global $errors; return count($errors);
}

function printErrors() {
   if (!hasErrors()) return;

   print "<ul class='errors'><li>" . join("</li><li>", $errors) . "</li></ul";
}


printErrors();
?>

This means that you are not using an object oriented path. And assuming that all errors are not fatal and fall into blocks try {} catch () {}.

0
source

All Articles