kid_drew Posted April 5, 2007 Share Posted April 5, 2007 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 Quote Link to comment Share on other sites More sharing options...
only one Posted April 5, 2007 Share Posted April 5, 2007 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 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 5, 2007 Share Posted April 5, 2007 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 Quote Link to comment Share on other sites More sharing options...
kid_drew Posted April 5, 2007 Author Share Posted April 5, 2007 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 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 5, 2007 Share Posted April 5, 2007 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 Quote Link to comment Share on other sites More sharing options...
kid_drew Posted April 5, 2007 Author Share Posted April 5, 2007 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 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 7, 2007 Share Posted April 7, 2007 If you don't want to turn off the errors, then the "verbose" coding is the way to go. Ken Quote Link to comment Share on other sites More sharing options...
neel_basu Posted April 7, 2007 Share Posted April 7, 2007 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. Quote Link to comment Share on other sites More sharing options...
kid_drew Posted April 7, 2007 Author Share Posted April 7, 2007 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. Quote Link to comment Share on other sites More sharing options...
neel_basu Posted April 7, 2007 Share Posted April 7, 2007 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 Quote Link to comment Share on other sites More sharing options...
kid_drew Posted April 7, 2007 Author Share Posted April 7, 2007 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 Quote Link to comment Share on other sites More sharing options...
neel_basu Posted April 7, 2007 Share Posted April 7, 2007 Use if(isset($errors['foo'])) { //Your code here } Quote Link to comment Share on other sites More sharing options...
kid_drew Posted April 7, 2007 Author Share Posted April 7, 2007 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. Sorry, I'm a bit of a code purist and I can get a little anal about my code being clunky and non-elegant. Use if(isset($errors['foo'])) { //Your code here } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.