开发者

What is the pro and cons using Heredoc Notation in your PHP?

开发者 https://www.devze.com 2022-12-23 21:16 出处:网络
I\'ve never seen something like this before. So, it\'s confusing me for a while. But now I understand and use it sometimes. So, after brief experience, can anybody tell me what is the pro and cons usi

I've never seen something like this before. So, it's confusing me for a while. But now I understand and use it sometimes. So, after brief experience, can anybody tell me what is the pro and cons using Heredoc No开发者_开发知识库tation in your PHP?

$stringval = <<<MYHEREDOC

   just creating variable here. nothing more.

MYHEREDOC;

Personally, how do you use this PHP feature? Is it a bad way of coding or good way?


99% of the time I use it, it's for SQL queries eg:

$sql = <<<END
SELECT *
FROM sometable
WHERE value > 100
ORDER BY name
END;

I find it easier to spot such queries in the source code and copy and paste them into something to run them. Your mileage may vary. Note: you can do multi-line with normal strings. I tend to avoid this however as the first line is indented differently to the rest.

The biggest "pro" as far as I'm concerned is that you don't need to escape quotes. That's particularly an issue with markup. Decide for yourself which is easier to read. Assuming:

$name = 'foo';
$type = 'text';
$value = 'Default value';

Version 1: single quotes

$html = '<input type="' . $type . ' name="' . $name . '" value="' . $value . '">';

Version 2: double quotes

$html = "<input type=\"$type\" name=\"$name\" value=\"$value\">";

Version 3: heredoc

$html = <<<END
<input type="$type" name="$name" value="$value">
END;

Note: in version 2 you can of course use single quotes for attribute values to solve the escaping problem but the point is you have to worry about things like this. Personally I don't like to mix attribute quote types in markup either.


There are now two types of "Heredoc:"

  • Heredoc, similar to quotation marks "" will convert any variables into their value on creation. This is very useful for putting exact spacing/returns where you need them (no need for \n or \t).
  • Nowdoc, similar to single marks '' will NOT convert any variables and stores just the text value. This can be used in default values of class variables (unlike heredoc).

Heredoc Example:

$value = 5;
$string = <<<EOL
The number is $value
EOL;
// The number is 5

Nowdoc Example:

$value = 5;
$string = <<<'EOL'
The number is $value
EOL;
// The number is $value

EDIT: The only downside I can think of is that the ending characters (in the examples, EOL) MUST be preceded by 0 spaces. This is hard to remember and looks ugly when inside nested functions:

function foo() {
  if ($bar) {
    $string = <<<LINE
Hey, how is it going, $name.
This is a great test of Heredoc and Nowdoc.
Blah blah blah, go upvote me!
LINE;
  }
}


Well, the pro(s) is

  • it's handy for large chunks of text with quotes

and the cons

  • you rarely if ever need large chunks of text in MVC
  • breaks indentation
  • has some quirks if used with classes (prior to 5.3)

Personally, i don't use it.

(an obligatory disclaimer about all this being a matter of preference, etc etc)


I don't use it at all. It was quite useful in Perl, as Perl had no HTML escape feature. So, PHP does, and if I want to print out large amount of text, i'd just close PHP tag and type text as is. In any other case old good double quotes suits me well.


HEREDOCs are very useful for dumping large blobs of text into a variable, or directly out to the client. It also saves you multiple concatenations if you're building up a long multi-line string. The only main drawback is you need to pre-process any data you'll be inserting.

echo <<<EOF
<input type="text" name="foo" value="$bar" />

EOF;

is very readable, especially with an syntax-highlighting editor. But you do need to pre-process $bar with htmlspecialchars() OUTSIDE there heredoc (for escaping quotes), so you end up with:

$bar = htmlspecialchars($bar);
echo <<<EOF
etc....

Whereas if you're in "html mode", you could use:

<input type="text name="foo" value="<?php echo htmlspecialchars($bar) ?>" />

or

echo '<input type ... snip ... value="' . htmlspecialchars($bar) . etc....

Generally you don't need to store the escaped version of the data, so you might as well dump it out directly and save yourself the variable assignment step. HEREDOCs don't let you do that, as you can't insert a function call into them directly.


It is very useful to have a very neat programming style segregating html with php. Your php codes will be very readable and easier to understand. It facilitates debbuging codes considerably. It allows you to concentrate more on the logic of the program rather than the presentation with html. You just need one echo statement rather than the annoying php scripts all over within your html. To illustrate how it is being use, here is an example.

masterpage.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title><?php $title ?></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="../js/jquery.min.js" type="text/javascript"></script>
        <link href="../bsa.css" rel="stylesheet" type="text/css">
         <script language="javascript" type="text/javascript" src="../js/sortEligibility.js"></script>
     <body>
            <div id="maincontainer">        
    <div id="menu">
                <ul id="navtop">
                    <li><*linkhere* title="Home">Home</a></li>
                    <li >*linkhere* title="Eligibility">Eligibility</a></li>
                    <li >*linkhere*  title="Registration  List">Registration</a></li>
                    <li ><*linkhere*" title="BSA Ranks and Requirements ">BSA Ranks</a></li>
                    <li><*linkhere*  title="Merit Badges">Merit Badges</a></li>                   
                </ul>
            </div>

            <div id='contentcolumn'>
                <?php  echo "<h2>".$title."</h2>".$bodyhtml;?>
                 </div>
            <div id="footer">Footer</div>
        </div>
    </body>
</html>

meritBadges.php

<?php
//include dbase connection
include("*linkhere*/includes/dbconnect.php");
//this page is called where the  value id_scout is sent through GET
//Show required output
$result = mysql_query("SELECT e.id_merit, m . *
FROM bsa_merits AS m
LEFT JOIN bsa_earned_merits AS e ON e.id_merit = e.id_merit
WHERE id_scout = '{$_GET[id_scout]}'");
while($row = mysql_fetch_assoc($result)){
    $body .=<<<__HTML_END
<td align="center"><img_src="*linkhere*/MeritBadges_Files/{$row['filename']}"><br>{$row['merit']}</td>
__HTML_END;       
}
if(!empty($body)){
$bodyhtml =<<<__HTML_END
Earned Merits:
<table border="0" cellpadding="5" cellspacing="0">
<tr>
$body
</tr>
</table>
__HTML_END;
}
else{
die ("There is nothing to display");
}
mysql_free_result($result);
$title="Merit Badges" ;
include("masterpage.php");
?>

All other pages may be created in similar fashion with the above example. Remember you just create one big string for your output and display it in your hmtl file with just one echo! Just analyze the above codes and see how you can improve your php programming with heredoc!


Is it a bad way of coding or good way?

You mean, Is it good practice or is it bad practice?

Generally, there's nothing wrong with using it. I'd go as far to say there's something wrong with not using it:

  1. You don't have to escape single and double quotes
  2. Allows you to append to a previous variable while defining a "context"
  3. Allows straight-up non-PHP markup (Such as Javascript) to be used alongside PHP; makes non-PHP code noticeable
  4. No noticeable differences in performance[*]. Even so, it's trivial.

Of course there's nothing wrong with $var = "Welcome, <b>$user</b>;" - However, it's not ideal if you intend to work with large quantities of foreign code and wish to maintain readability:

Really, it's an ideal way to deal with markup such as HTML if you can't output it directly.

[*]Based off personal tests, but should remain true regardless.


Whilst it is acceptable to use HEREDOC, the major downside in my opinion is that most IDE's lose the ability to track the code, and often display messages where closing tags cannot be found - particularly if an opening tag inside a different HERDOC to the the end tag, or the code is a mixture of all styles. One of the applications I inherited was all over the place. For example.

echo <<<PART1
<table>
   <tr>
   ....
   </tr>
PART1;
//more php code
?>
</table>

This is a bad example, where some code is in a HEREDOC, such as the start of the table, and then much later down the code, the table is closed normally outside of PHP tags. My IDE (phpstorm 2017) complains that it cannot find matching table tags.

For this reason, I always convert the HEREDOCs I find into single quote versions. I also dislike escaping, so the single quote method looks tidiest for me.

But that's just my opinion....

0

精彩评论

暂无评论...
验证码 换一张
取 消