php_guest Posted October 4, 2009 Share Posted October 4, 2009 if(empty($_POST[name]))$_POST[comment]){$comment=$_POST[comment] <input type="test" value="<? echo $comment; ?>" Is it ok to use empty string in a code? For example if a form won't be submited it won't put in a text field any value because value for $comment hasn't been submited. If the form si submited and there is an error, it asign value to $comment so user doesn't need to type it again. Or should I put also if(!empty($comment)... and if so, why that? It works fine also if string is empty. Link to comment https://forums.phpfreaks.com/topic/176443-is-it-ok-to-use-empty-strings/ Share on other sites More sharing options...
cags Posted October 4, 2009 Share Posted October 4, 2009 It depends if the variable is declared. If your code is like so.... <?php $comment = ''; if(!empty($_POST['comment'])) { $comment = $_POST['comment']; } ?> <input type="test" value="<?php echo $comment; ?>" /> Then I'd consider the code fine as it will not throw any errors or notices. If however your code is like this... <?php if(!empty($_POST['comment'])) { $comment = $_POST['comment']; } ?> <input type="test" value="<?php echo $comment; ?>" /> Then you could get a notice (undefined variable). If you don't mind having notices on your site then this doesn't really matter, if you do, then you either use the first option or you could change the final line to... <input type="test" value="<?php if(isset($comment)) {echo $comment; } ?>" /> Link to comment https://forums.phpfreaks.com/topic/176443-is-it-ok-to-use-empty-strings/#findComment-930073 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.