commenting php code

  • tiffix
  • Student
  • Student
  • User avatar
  • Joined: Jun 03, 2009
  • Posts: 65
  • Loc: kenya
  • Status: Offline

Post August 18th, 2010, 3:32 am

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
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post August 18th, 2010, 3:32 am

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6130
  • Loc: Seattle, WA
  • Status: Offline

Post August 18th, 2010, 10:50 am

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>';
The Beer Monocle. Classy.
  • tiffix
  • Student
  • Student
  • User avatar
  • Joined: Jun 03, 2009
  • Posts: 65
  • Loc: kenya
  • Status: Offline

Post August 18th, 2010, 11:04 am

Thanks a lot.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post August 18th, 2010, 1:38 pm

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. ?>
Strong with this one, the sudo is.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 176 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
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.