commenting php code

  • tiffix
  • Student
  • Student
  • User avatar
  • Posts: 65
  • Loc: kenya

Post 3+ Months Ago

Could anyone be kind enough to tell me what's wrong with this code.
PHP Code: [ Select ]
<?php echo "<option value=\"".if(isset($_SESSION['passignment'])){echo $_SESSION['passignment'];}."\" selected=\"selected\">".if(isset($_SESSION['passignment'])){echo $_SESSION['passignment'];}."</option>"; ?>

thanks
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6254
  • Loc: Seattle, WA

Post 3+ Months Ago

You can't insert conditional blocks as part of a string itself; you should use ternary conditionals if that's what you're trying to do:

PHP Code: [ Select ]
<?php echo '<option value="' .
  (isset($_SESSION['passignment']) ? $_SESSION['passignment'] : '') .
  '" selected="selected">' .
  (isset($_SESSION['passignment']) ? $_SESSION['passignment'] : '') .
  '</option>';
  1. <?php echo '<option value="' .
  2.   (isset($_SESSION['passignment']) ? $_SESSION['passignment'] : '') .
  3.   '" selected="selected">' .
  4.   (isset($_SESSION['passignment']) ? $_SESSION['passignment'] : '') .
  5.   '</option>';
  • tiffix
  • Student
  • Student
  • User avatar
  • Posts: 65
  • Loc: kenya

Post 3+ Months Ago

Thanks a lot.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

I like to use printf for complex printing/echos like that.

PHP Code: [ Select ]
<?php
 
printf('<option value="%1$s" selected="selected">%2$s</option>',
   isset($_SESSION['passignment']) ? $_SESSION['passignment'] : '',
   isset($_SESSION['passignment']) ? $_SESSION['passignment'] : '' //2
);
 
?>
  1. <?php
  2.  
  3. printf('<option value="%1$s" selected="selected">%2$s</option>',
  4.    isset($_SESSION['passignment']) ? $_SESSION['passignment'] : '',
  5.    isset($_SESSION['passignment']) ? $_SESSION['passignment'] : '' //2
  6. );
  7.  
  8. ?>


It lets me keep the HTML together and makes it easier to remember exactly what that section is for. Having all of the HTML together with simple tokens also makes it easier to find with something such as grep or findstr down the road, as well as makes it easier to add to in the future.

Something you might notice is that I'm using numbered tokens even though I only have two tokens. At first it might seem that there's no need to use numbered tokens because two tokens is easy enough to keep track of, but what if I want to add/modify something between existing tokens in the foture ?

For instance, what if the selected attribute were to become dependent on a condition for whatever reason in the future ?

PHP Code: [ Select ]
<?php
 
printf('<option value="%1$s"%3$s>%2$s</option>',
   isset($_SESSION['passignment']) ? $_SESSION['passignment'] : '',
   isset($_SESSION['passignment']) ? $_SESSION['passignment'] : '', //2
   $is_selected ? ' selected="selected"' : ''
);
 
?>
  1. <?php
  2.  
  3. printf('<option value="%1$s"%3$s>%2$s</option>',
  4.    isset($_SESSION['passignment']) ? $_SESSION['passignment'] : '',
  5.    isset($_SESSION['passignment']) ? $_SESSION['passignment'] : '', //2
  6.    $is_selected ? ' selected="selected"' : ''
  7. );
  8.  
  9. ?>


Another thing you might notice is that I've got a " //2 " in there. I do this periodically throughout my list of tokens so that it's easier to keep track of which tokens go with which number and so I can quickly determine which number to use if I need to add a new token. :)

// Edit -- Though, now that I actually pay attention to value of the tokens, I would probably write that as follows. Again, a nice utilization of the numbered tokens.

PHP Code: [ Select ]
<?php
 
printf('<option value="%1$s"%2$s>%1$s</option>',
   isset($_SESSION['passignment']) ? $_SESSION['passignment'] : '',
   $is_selected ? ' selected="selected"' : '' //2
);
 
?>
  1. <?php
  2.  
  3. printf('<option value="%1$s"%2$s>%1$s</option>',
  4.    isset($_SESSION['passignment']) ? $_SESSION['passignment'] : '',
  5.    $is_selected ? ' selected="selected"' : '' //2
  6. );
  7.  
  8. ?>

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 65 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.