Using PHP for comments inside of HTML - php

<?php // force Internet Explorer to use the latest rendering engine available ?>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<?php // mobile meta (hooray!) ?>
<meta name="HandheldFriendly" content="True">
I've seen this trend more and more lately. Does it have any advantages or disadvantages over the traditional way of leaving comments/notes?

Correct me if I'm wrong, but I think that the only reason is that you use this, is that you won't see the comments in the browser's source-view of your website.

Adding comments with php has one Advantage that only the developers reading php code will see these comments and that cannot be seen by viewing source code of page(html).

For my experience the only advantage with this trend is valuable when you have to comment large blocks of code/markup, like php mixed with hmtl or javascript.
That being said, best practices don't recommend mixing code and html when it's possible, but if you have to, it is better do this thing
<?php /*
//js code
<!--
<script type="text/javascript">
//my js script
</script>
-->
<!--
<p>a bit of commented html</p>
-->
*/ ?>
instead of
//js code
<!--
<script type="text/javascript">
//my js script
</script>
-->
<!--
<p>a bit of commented html</p>
-->

I would rather use this:
<?php
// force Internet Explorer to use the latest rendering engine available
echo '<meta http-equiv="X-UA-Compatible" content="IE=edge">'.PHP_EOL;
// mobile meta (hooray!)
echo '<meta name="HandheldFriendly" content="True">'.PHP_EOL;
and probably not use simple echo's like this. I simply don't like mixing PHP and HTML, it's ugly. Better go with full PHP code.

<?php echo'<!--- force Internet Explorer to use the latest rendering engine available-->' ?>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<?php echo'<!--- mobile meta (hooray!)-->' ?>
<meta name="HandheldFriendly" content="True">

Related

How to create dynamic url and content like wordpress in php?

I want to create dynamic url and content like wordpress platform but I am doing something wrong. Kindly check my code:
<?php
$pages = array("story1", "story2", "story3");
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>This is my page title.</title>
<link rel="canonical" href="http://www.xyz1.com/kids-english-<?php
echo $pages[1];?>.php"/>
<!-- here the url is creating but gives error 404 -->
</head>
<body>
<p>Hello, <?php echo $pages[1];?></p><br>
<?php echo $pages[2];?>
<!-- here the second url is creating but gives error 404 -->
</body>
</html>
Now I want to get the content from database and want to load somefile.php
Here starts my second problem. I create urls by foreach loop but how to load all the content to somefile.php and how will one somefile.php will handle all the different urls and different content created dynamically. I am confused here.
Your suggestions would be welcome.
Thank You.
Well.. applications like Wordpress, Shopware and so on are built on a MVC-Design scheme like every other good coded project.
You can go for frameworks like Symfony or build your own routing mechanism.
All in all it's a little bit more tricky as you did mentioned your thoughts.

CSS loading after HTML loads

I have a form at http://www.anhatweb.tk/web/auth/create to create a new user
But whenever i load the page a loader comes but we can see if i remove the loader HTML loads first and then the CSS loads....Is there any way that I can load CSS first and then load the HTML?
My code:
<html lang="en">
<head>
<!-- Basic Page Needs-->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>ABC</title>
<!-- Mobile Specific Metas-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSS-->
<link href="<?php echo base_url(); ?>assets/css/style.css" rel="stylesheet">
</head>
<body>
The HTML has to download first before the css can even start downloading because the location of the css file is stored within the HTML head tag. Because of the direction of this dependency, you can't really reverse it.
As #Daniel said, the best option you likely have is to "hide" the HTML until the CSS has completed loading. Lots of websites do this to avoid the flash of non-css that you're experiencing, it's pretty normal (and annoying to deal with).
I would give just hiding the elements until the onLoadCompleted is called. If you still see the flash after that, I would load the stylesheet manually on the onLoadCompleted (it should be executed at runtime instead of async) and then show the elements.
No, the browser fetches the HTML, parses the Markup and when it comes to an external resource (css or javascript) it will load this.
After loading the external resource the browser will continue parsing the Markup.

seo effect - include file with <html> tag inside

Maybe its a stupied question but i didnt fild any answer for it,
If i have file with the html tag head tag&meta.. and I include him in all my web page, its effect the seo of the site?
I have file named "start_html.php" that have this code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="bla bla.">
<meta name="keywords" content="bla bla">
<meta name="author" content="bla">
<title>bla bla</title>
<!-- CSS: -->
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<!-- JavaScript -->
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
I start every page with this line: <?php include('start_html.php'); ?>
When i check for code error on w3c validator its says that i dont have those tags so the page is not coded good.
If the code from that file is not being displayed in your web page then obviously your PHP code is incorrect and that file is not being included. You need to make sure you have error reporting on and displaying all errors as this will catch this for you.
See this StackOverflow question for how to enable error reporting.
You should have these meta and title tags on all your pages, so including them from PHP is certainly not a bad idea.
However, if the W3 validator tells you these tags aren't there, you should check your output. Perhaps start by 'show source code' in your browser, and see if the tags appear there.
If you try to send your source file for validation, where you have:
<?php include('start_html.php'); ?>
Of course you will get the expected result - no tags, because the source file must be parsed and handled by PHP.
You can give a working link for validation, or copy output in your browser after execution, save the file and send it.

Alternative to CSS in body tag?

In my login system in my site I wanted the user's selected CSS to be loaded after validation was complete. The only problem is that my PHP is located in the body element and I ECHOed the user's CSS link there. I know this is "bad", but what else can I do?
if (isset($_SESSION['loggedin'])){
ECHO $_SESSION['style'];
}
else
{
ECHO 'green';
}
ECHO ".css' />";
The PHP script echos some text in the body after the user is logged in, this is why I cannot put the PHP script in the header.
I'm under the assumption that you're not at all familiar with the basics of PHP, so I'll break it down as simply as I can. If your document is able to execute PHP (which it sounds like it can because you have things happening in the body), all you need to do is wrap the PHP code with <?php and ?> for it to parse as PHP.
You shouldn't put stylesheets inside your body. Indeed, that is something that no one will recommend. What you can do, however, is execute PHP in the head of your document.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
<link rel="stylesheet" href="style.css">
<?php
echo "<link rel='stylesheet' href='{$_SESSION['user_css']}'>";
?>
<script src="script.js"></script>
</head>
<body>
<?php
echo 'body text';
?>
</body>
</html>
It doesn't matter where you decide to inject PHP code into your document, it will render as you wish. I'd suggest an MVC solution, but if this is just a small, one-off file, feel free to inject PHP wherever you want it, as often as you need it.
http://php.net/manual/en/language.basic-syntax.phptags.php
You can load the user css after the document is loaded using jQuery.
<script>
$(document).ready(function() {
$(head).append('<style>
<?php echo($user_css); ?>
</style>');
});
</script>
Though just adding a section to output the CSS in the head would be easier to maintain in the future and doesn't need the page to load. A dramatic enough change on a slow(ish) connection and the user will see the flicker as the CSS loads their style.
you could use some jQuery to append the style in the <head>:
$(head).append('<style><?php echo user_css ?></style>');

Is it good practice to add a php include of the head section in my pages?

I am creating my portfolio site and I am wanting to include the head section as a php include on my page. Reason being is because the site will have a fair few pages and I will want to make changes later on to things later on like tidying up the css files.
For example;
<head>
<?php include('head.php'); ?>
</head>
as opposed to all this below being shown on each and every page:
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/1140.css">
<link rel="stylesheet" href="css/ie.css">
<script src="js/vendor/modernizr-2.6.1.min.js"></script>
</head>
I just didn't know if this was good practice to do, as with this being my portfolio site, I need the code to be correct from the start also as they will probably look into the standard of it also.
What are your opinions and advice people? Thanks.
Yep, it's quite standard. But instead of writing:
<head>
<?php include('head.php'); ?>
</head>
you should put the tags inside head.php. I say it's better because what's inside head.php has no sense without the head tags, so they are kinda linked together. It's good practice to join things so linked into a single file without having to repeat open and close head tags for each page.
Actually, it's even good practice (and commonly used) to have header.php, body.php and footer.php files that has respectively:
header.php
<html>
<head>
...
</head>
<body>
body.php
...
footer.php
</body>
</html>
I'm doing that in my application but I've found that it's not a good idea, because you have many of your stylesheets, javascripts, etc in a php file including the head section and you'll have problems with including it in php files in nested folders. this problem is because of relative paths.
If you can use absolute paths then it's ok otherwise it's not a good idea ...
PHP Includes are used like this all the time. Any time that you have content that will be the exact same on every page, it is very helpful to use an include
This is an old topic but I use
<?php include_once("phpinclude/head.txt"); ?>
phpinclude is it's own folder and I keep the footer, header, and common place info in that folder. .js, and .css has it's own as well.
Edit: I use require now. I would rather have a code fail and die rather than give some random string. They are the same except one dies and the other will print out an error or random code. This is for people learning PHP, not old heads.

Categories