Jump to content

is it ok to use empty strings?


php_guest

Recommended Posts

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

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; } ?>" />

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.