Jump to content

Proper PHP formatting?


Arimil

Recommended Posts

So I've been working on PHP for quite some time now and I'm confident I can do pretty much anything after looking up a few commands. But one thing thats been bugging me is I think I'm outputting information to HTML incorrectly. For example.

 

<head>
<?php 
   if(stristr($_SERVER['HTTP_USER_AGENT'], "Android")){
      echo "<meta http-equiv="refresh" content="0;url=http://android.website.com">"; 
   }
?>
</head>
<body>
<?php
   echo "Your user agent is " . $_SERVER['HTTP_USER_AGENT'];
?>
</body>

 

But I remember reading something when first starting with php that you should always put all of your php content at the top of your document or something like that. I also read somewhere that its bad to echo information like that. Someone mind showing me how you would go about doing something like this correctly?

Link to comment
Share on other sites

Actually I think I know why its bad to do this. I was thinking and if it's a PHP page why should it have HTML in it at all? The correct way to do it would probably be to have external pages to generate the headers etc so they can easily move from page to page. But that still leaves me with the problem of using echo to create the page. Anyone?

Link to comment
Share on other sites

There's nothing wrong with echo.  However you will find that your life as a program maintainer will be made significantly easier when files are mostly PHP or mostly markup.  It's not always the case but I tend to keep most of my PHP at the top of a file.

 

Using some sort of view and controller mechanism helps as well; basically MVC without the M.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.