PHP or HTML first or does it matter? - php

I have a possible stupid question, but I'll ask it anyway.
Does it matter what goes first, PHP or HTML code?
For example: Does PHP go before HTML, after HTML or does it matter at all?
<?php
echo "This is text";
?>
<html>
<head>
</head>
<body>
<center>
<font size="2">This is text</font>
</center>
</body>
</html>
Or:
<html>
<head>
</head>
<body>
<center>
<font size="2">This is text</font>
</center>
</body>
</html>
<?php
echo "This is text";
?>
Or:
<html>
<head>
</head>
<body>
<?php
echo "This is text";
?>
</body>
</html>

The third one is the correct way (assuming you want the text to echo out in the body).
PHP can jump in and out of HTML as you have shown above.
<html>
<head>
</head>
<body>
<center>
<font size="2"><?php echo "This is text"; ?></font>
</center>
</body>
</html>

Personally I put the PHP as much as possible at the top of the page or even better outside the html page completely by using the html pages as purely views in the MVC pattern.

HTML doesn't go anywhere, but PHP script goes to server executes and response is returned to client side. Now that response is displayed/handled along with HTML code. HTML is only for browser where PHP script is used invoke service or do operations on database.
So, first PHP(Server) and then HTML(Client).

Add your php code before the html code.
This allows you to change the out type, set requied variables, add http response headers if you require, etc.
You can have a lot of php embeded tags in between the html.
The html in your question would be invalid, if you echoed output before or after .
Make sure your out is valid html.
Don't be bad to the browser just cause they will try to work with whatever you give them.

Onlything you have to maintain valid html structure. so you canot put anything outside the html tag. so third option is the most valid thing. but if you use any of others, it will print anything you want.

Not being a php person, will try to ans this in general sense. HTML is for browsers and php is serverside. When your pages reaches to the browser, there is only HTML, while, if I am not wrong as php should behave similarly yo jsp, at serverside html is seen as simple strings that need to be printed out at stream. So ideally, this should not matter what come first.
From good practice perspective, as this is php code(in my case jsp) whose output will be html, I try to give more feel of java to my code file.

Related

Which one is better or must be in PHP scripts that can be applied for real projects?

As I am learning PHP, I want to know if I ever wanted to put HTML tags in the middle of a PHP script, I want to know which one is better or must be, out of putting them in PHP echo or breaking the PHP script where the HTML has to be started and then continuing the PHP script using tags php starting and closing tags. Below are two examples where you can get a clear idea.
page name: home.php example: 1
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php
a=0;
while (a<=10){
?>
<input type = "text" max-length="20">
<?php
}
?>
</body>
</html>
page name: home.php example: 2
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php
a=0;
while (a<=10){
echo "<input type = 'text' max-length='20'>";
}
?>
</body>
</html>
For learning purposes I will suggest you use <?php ?> and <?= ?> to break out of HTML. Echo’ing HTML may confuse you while learning (as someone earlier pointed out code highlighting). In your journey through learning, consider learning about MVC and how you can apply in simple ways to fit your needs. It helps you separate presentation from logic.
This is really just a matter of preference, both are acceptable. Typically if you have quite a bit of HTML DOM to insert, you'd break php with ?> and then reopen when necessary. If you have just a single line of HTML that needs to be inserted, a simple echo works perfectly.

Disable output of hardcoded HTML tags in a PHP file

I am working with a software written in PHP that I have no control over. Some of the PHP files have hardcoded HTML tags in them that I'd like to suppress or exchange with my own code.
Fortunately there is a plugin-system that allows me to execute my own code at certain points, here's an example of the files I'm dealing with:
<!DOCTYPE html>
<html>
<?php
execute_plugin_code_1()
?>
<head>
<!-- some additional tags -->
</head>
<?php
execute_plugin_code_2()
?>
<body>
</body>
</html>
What I want to do here is generate my own <head> tag. I don't want to alter the file itself so I'd like to suppress the hardcoded tag and its children.
Is there any way to disable the output of hardcoded HTML between execute_plugin_code_1() and execute_plugin_code_2()?
To answer your question, no, you cannot "disable" the HTML tags. You would need to remove them from the file, or use an entirely different file. The structure of this file is pretty odd though. I'm not sure what the original developer was trying to allow by providing hooks that run just before, and just after the head of the HTML document. It would have made much more sense to provide a hook within the head and within the body.
Do you want to avoid jquery? If not, then
$(document).ready(function(){
$('head').remove();
)};
https://api.jquery.com/remove/
Then you'd need to also use jquery to insert your new head tag's html. I'd be very wary, though. Seems like it'd be pretty easy to break the page altogether.

create and save pure html file from php 'template'?

I am looking for a simple and effective way to create a pure html file based off a php file. For instance, in template.php below the php would be inserting various portions of the page. I want to save the page then as html removing all php code and leaving what was inserted by it... hopefully that makes sense... the output of template.php would be a better way to say it I guess.
First, I do not know if something like this is possible. Second, is this the best way to go about something like this?
Before anyone starts screaming about security there will be ZERO user submitted / form submitted variables in this page. My goal is to create a report from database values with the template which the user can then view/print/save off the server as pure html. There will be no images only inline css.
EDIT :
This html only output of template.php needs to be saved on the server as its own file. The reason for the php 'template' is because I will be creating the vast majority of the page with php... but I only want to save its resulting output.
template.php :
<!DOCTYPE html>
<html lang="en">
<!-- BEGIN HEAD -->
<head>
<meta charset="utf-8" />
<title><?php echo $title; ?></title>
<meta content="<?php echo $desc; ?>" name="description" />
</head>
<!-- END HEAD -->
<!-- BEGIN BODY -->
<body>
... further html with php mixed in
</body>
<!-- END BODY -->
</html>
Current solution :
I did some further research and this is acting exactly how I want it to. Comments/suggestions welcome for it.
<?php
ob_start();
require_once('/home/test/public_html/template.php');
if ( ob_get_length() > 0 )
{
$ssReport = ob_get_contents();
file_put_contents('/home/test/public_html/test.html', $ssReport);
}
ob_end_clean();
?>
You can use REGEX (Regular Expressions) to strip out all php-code.
The Expression <\?php.*?\?> can do that for you...
PHP-Example:
$tempateFile = file_get_contents('template.php');
$htmlPlain = preg_replace('/<\?php.*?\?>/si', '', $tempateFile);
If you allow "Shorthand Open Syntax" for php-files <? instead of <?php you should use the pattern <\?(?:php)?.*?\?> which will become preg_replace('/<\?(?:php)?.*?\?>/si', '', $tempateFile);
If you want to take a html-snapshot of your site(s) try this options:
use wget to grab a copy of your website (see wget manual or wget tutorial)
use curl with a script to grab a copy of your website (see curl manual or
curl tutorial
make a php-script which uses file_get_contents($url) to grab outputs of you specific public-available URL and store it with file_put_contents(...) to a html-file.

Is there a way to overwrite a page's <title> tag by using PHP within the body?

I'm pretty new to PHP and was wondering if there was a way to overwrite what is displayed in a title tag by using PHP inside the body.
Let me explain why I'm trying to do this. I'm using a forum/cms software that allows me to create PHP pages, but won't let me change anything about the header (including the title tag). I was hoping there was a script that I could place into the body using PHP that would overwrite whatever was displayed into the default title tag.
This is probably a crazy question, and if so I apologize.
Just running out of ideas how to get what I need in the title.
Thanks!
You can't.
if you want to change it add some Java Script code that will execute on the client side and do this for you:
<script>
document.title = "This is the new page title.";
</script>
And with some PHP:
<head><title>some title</title></head>
<body>
<?php if (some condition, etc) { ?>
<script>
document.title = "This is the new page title.";
</script>
<?php } ?>
</body>
<html>
<head>
<title>Hello</title>
</head>
<body>
<?
echo "<script>document.title='Hello, World!';</script>";
?>
</body>
</html>

Can I configure PHP to automatically format the HTML it generates?

For example…
This PHP code
<?php
echo '<html>';
echo '<body>';
echo '<h1>Header One</h1>';
echo '<p>Hello World!</p>';
echo '</body>';
echo '</html>';
?>
Generates this HTML markup
<html><body><h1>Header One</h1><p>Hello World!</p></body></html>
Are there any functions, libraries or configuration options to make PHP automatically apply some simple formatting (line breaks & indentation) based on the nesting of html tags generated in the output? So that instead something like this would be generated…
<html>
<body>
<h1>Header One</h1>
<p>Hello World!</p>
</body>
</html>
You could try templating engines like Smarty, Savant, PHP Sugar or VLib.
Or you could go commando with output handling (which I think is a hack). ob_start('ob_tidyhandler');
For the output handling, Tidy might not be installed or enabled, typically the package you will need to install is named php-tidy and you will need to uncomment extension=tidy in your php.ini
You can put html in your PHP script without having to echo it. You also might want to look for a PHP template engine like smarty, so you can separate the view from logic.
I prefer to use heredoc strings and format/indent the HTML myself. Mixing HTML strings inside PHP code quickly leads to unreadable, unmaintainable code. Some advantages of this method:
Quotes don't need to be escaped.
You can put variables inside the heredoc strings.
Even when working with code that loops, you can output the HTML inside heredoc strings. If these strings are indented properly relative to your other blocks of HTML, you will still get HTML code that has good indentation.
It forces you to think about when you want to print HTML, and to print it in blocks instead of lots of little pieces sprinkled throughout your code (very hard to read and maintain).
It's better to separate the PHP code from the HTML as much as you can, whether this means using a templating engine or just putting all of the code before all of the HTML. However, there are still times when it's easiest to mix PHP and HTML.
Here's an example:
<?php
$text = 'Hello World!';
echo <<<HTML
<html>
<body>
<h1>Header One, with some '"quotes"'</h1>
<p>$text</p>
</body>
</html>
HTML;
?>
If I understand your question right, you want to pretty-print the HTML output.
This can be done by post-processing the output of your PHP script. Either by using PHP's output handling feature combined with the tidy extension­Docs:
ob_start('ob_tidyhandler');
Tidy is an extension specialized on cleaning up HTML code, changing indentation etc.. But it's not the only way.
Another alternative is to delegate the post-processing task to the webserver, e.g. output filters in Apache HTTP Server­Docs.
You could do this (my preference):
<html>
<body>
<h1>Header One</h1>
<p>Hello World!</p>
<?php echo '<p>Hello Hello!</p>'; ?>
</body>
</html>
Or:
<?php
$html = '<html><body><h1>Header One</h1><p>Hello World!</p></body></html>';
$tidy = new tidy();
$tidy->parseString($html, array('indent'=> true,'output-xhtml'=> true), 'utf8');
$tidy->cleanRepair();
echo $tidy;
?>";
...would print:
<html>
<body>
<h1>Header One</h1>
<p>Hello World!</p>
</body>
</html>
...Or you can use the "<<<" operator where you can set formation by yourself:
<?php
echo <<<DATA
<html>
<body>
<h1>Header One</h1>
<p>Hello World!</p>
</body>
</html>
DATA;
If the below code looks useful to generate html with php, try this library
https://github.com/raftalks/Form
Html::make('div', function($html))
{
$html->table(function($table)
{
$table->thead(function($table)
{
$table->tr(function($tr)
{
$tr->th('item');
$tr->th('category');
});
});
$table->tr(function($tr)
{
$tr->td()->ng_bind('item.name','ng-bind');
$tr->td()->ng_bind('item.category','ng-bind');
$tr->setNgRepeat('item in list','ng-repeat'); //using second parameter to force the attribute name.
});
$table->setClass('table');
});
$html->setClass('tableContainer');
});

Categories