Jump to content

Inlining PHP in HTML


kid_drew

Recommended Posts

Question on inlining PHP calls.  Let's skip the argument on whether to use Smarty templates, etc.

 

I have base PHP scripts for each page that I serve that perform the logic of the page and then call another PHP template script that has all the HTML information.  I inline PHP calls in my templating script to access my variable values and to iterate through arrays to create tables etc.  For those of you who don't use Smarty templates, is this how you do it?

 

Also, I access my PHP variables in a way similar to this:  <?php echo "<span class=\"error_msg\">$errors['foo']</span>"?>.  The problem there is that it will throw a notify message to the screen if I have errors turned on and the value is not initialized (if I had no errors in a submitted form, for instance).  Is there a way to suppress that message other than adding the verbose code <?php if(isset($errors['foo'])) echo "<span class.........."?>

 

Am I completely missing an easier way to do this?

 

-Drew

Link to comment
Share on other sites

well you can type in the html an then set the php inside it.. eg

<html>
<head></head>
<body>
<?php here ?>
</body>
</html>

 

and if youre using an if, while loop or any function like that you can just close the php and continue with your html without using and echo or print

Link to comment
Share on other sites

The verbose way is the "correct" method. You can also use the "@" character to suppress the warning messages, but this method will hide important errors also. Also if you don't use the verbose method you are adding unnecessary HTML code into your document.

 

Ken

Link to comment
Share on other sites

I don't think the @ character works for echo calls.  At least, it gives me an error when I do that.

 

The verbose way is the "correct" method. You can also use the "@" character to suppress the warning messages, but this method will hide important errors also. Also if you don't use the verbose method you are adding unnecessary HTML code into your document.

 

Ken

Link to comment
Share on other sites

Yeah, I can turn off error reporting, but I don't like doing that.  I'm from the old C/C++ mentality of getting rid of all warning messages.  ;)

 

You wouldn't put it before the "echo" but before the array reference:

<?php
echo '<span class="error_msg">' . @$errors['foo'] . "</span>";
?>

 

Or you can turn error reporting off by using the error_reporting() function.

<?php
// Turn off all error reporting
error_reporting(0);
?>

 

Ken

 

Link to comment
Share on other sites

I personally dont like error_reporting(0) while testing/ creating. I use error_reporting(E_ALL) while creating or testing. And I think Its a very bad habit using error_reporting(0). But for security reson After Finishing something i add error_reporting(0) at teh top of every page.

Link to comment
Share on other sites

I personally have a global definitions file that I include in every page, and in that file I have a DEBUG constant that I can set to turn error reporting on and make every page print out a bunch of debug statements.  That works really well.  The problem I have is when I have debug turned on, my forms display every single notice statement about the undefined variables I'm trying to access, and it totally breaks my formatting, making editing HTML in debug mode completely impossible.  Maybe I should turn error reporting to E_ERROR instead of E_ALL

 

I personally dont like error_reporting(0) while testing/ creating. I use error_reporting(E_ALL) while creating or testing. And I think Its a very bad habit using error_reporting(0). But for security reson After Finishing something i add error_reporting(0) at teh top of every page.

Link to comment
Share on other sites

Not exactly.  I have a form where I display errors inline with each field if the user inputs something incorrectly.  So:

 

<input type="text" name="foo"><?php echo $errors['foo']?>

 

This displays a notice unless $errors['foo'] is defined.  Seems like there might be a better way to do it, anyway.

 

Try Code Clean Althoungh Its not posible all time

So I think You have this kind of error.

$var is not initialized.

but You did $var += or $var .=

and You use $array[key] instead of $array['key']

or something like that

Link to comment
Share on other sites

Yeah, I mentioned earlier that I do that.  It just seems like this solution is very clunky, and I bet there is a better way.  But my suspicion is that there is a better way to implement forms/tables in general than writing out the html code.  I bet there is a class solution to implement forms and embedded tables, and I posted the question to this forum a few days ago looking for such a solution.  No replies.  :P

 

Sorry, I'm a bit of a code purist and I can get a little anal about my code being clunky and non-elegant.  ;D

 

Use

if(isset($errors['foo']))

  {

    //Your code here

  }

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.