Helpful Information
 
 
Category: Coding tips & tutorials threads
Shorten your PHP Code, and make it easy.

I've been seeing a lot of people having code like this:


<?php
echo "<html>";
echo "<head>";
echo "<title>Title</title>";
echo "</head>";
echo "</html>";
?>

Now, some of you out there may say "There's no error!", you're right there isn't, but it would take up less room if you do something like this:


<?php
echo <<< PAGE
<html>
<head>
<title>Title</title>
</head>
</html>
PAGE;
?>

Or even this:


<?php
echo "
<html>
<head>
<title>Title</title>
</head>
</html>
";
?>

So this is just a useful tip, you don't need to use it or anything, just thought it'd be helpful.
Questions will be answered.

When writing HTML code in php, its generally thought as better practice to separate the content from the actual code... therefore in those two examples the first one would be a better idea, however what I prefer is something more along the lines of



<?php
if(condition)
{
?>
<p>Label: <?php echo $value; ?></p>
<?php
}
else
{
?>
<p>Error: <?php echo $error; ?></p>
<?php
}
?>


that is alittle bit better because it separates out the two, however you are still not entirely free and clean. the BEST solution, would be to use a template's engine like Smarty which gives you the full data and code extraction

I'm not sure I really agree here.

There's certainly a valid point to simplifying code, but the point is readability. As such, having lots of small bits is actually easier to read than one large chunk.

I think that the main point is simply to have well organized code.

In the case above, it makes some sense to separate those elements (like boogyman did, especially). But certainly streamlining similar processes is in general good practice.

In addition, it would be great if when asking for help your code had comments. It would be much faster to help as well, if that was the case.

I don't know if your saying my code would be easier if it had comments because it's the simplest thing ever. But you said having small bits is easier, and tidier. I think that it almost is identical to this:


<?php
echo <<< PAGE
<html>
<head>
<title>Title</title>
</head>
</html>
PAGE;
?>

Except with more space.

Well, smaller bits meant that having one echo per line is actually pretty easy to read. Especially when those represent different sections of the html page.

And, no, I didn't mean you should add comments, but that people should when posting questions, in addition to having organized code. "You" = "someone".

Ahh, ok I see. What does your mean then? lol.

This is good for template files, though (I think that was Nile's original point). Say you have one PHP file with nothing but a large template function, like:


<?php
function createPage($title,$body,$author,$date){

echo <<< END
<html>
<head>
<title>$title</title>
</head>
<body>
<div id="main">
<div id="menu">
menu
</div>
<div id="body">
$body
</div>
<div id="footer">
<p>Page by $author on $date</p>
</div>
</div>
</body>
</html>
END;

}
?>

Can you imagine putting individual echo statements for each line, or inline echo statements for each var? Or trying to slash out ever double quote? But for inline echo-ing, it makes it hard to read. Everything has its place.

I guess the main thing is preference.










privacy (GDPR)