Print Reseller Scheme
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

WP3.0 Displaying PHP Code

Discussion in 'Website Design Forum:' started by glenwheeler, Aug 13, 2010.

  1. glenwheeler

    glenwheeler Senior Member

    Hi Guys,

    I wrote a small blog post yesterday evening where I wanted to display some php code however the post keeps outputting the code and not just as text.

    I have used the
    tags but this is pretty damn useless as it does nothing!

    Does anyone know of a widget or a tag that will allow me to display code as text in the post.

    You can see the post I did is outputting a search box when all i want to display is some php code.

    Blog | Glen Wheeler – Freelance Web Design | Web Design Newcastle & North East UK

    PS: Excuse the styling, am just playing with the appearance of things at the moment. I just keep updating the posts so I have some decent content when the site is all nice and tidy.

    Any help would be cool, cheers dudes and dudette's :clap:
  2. you could code a blockquote style in css and then make all your code in blockquotes :)
  3. Harry

    Harry Senior Member

    You need to encode your entities lol.

    Instead of:

    <pre><code><div class="example">Text</div></code></pre>
    you need

    <pre><code><div class="example">Text</div></code></pre>
    And don't put code in a blockquote, that's not what they're for. Wrap any encoded code in <pre> and <code> tags.
  4. I stand corrected by Harry :)
  5. Harry

    Harry Senior Member

    Haha sorry, t'was a tad blunt.

    Also, only wrap code in <pre> tags if it needs it, obviously :)
  6. not a problem mate :) :lol:
  7. Mark Alexander

    Mark Alexander Senior Member

    Why do you need a <pre> tag at all when you can just apply whitespace: pre; to code elements and avoid the extra tag?
  8. Harry

    Harry Senior Member

    Semantics. Plus as soon as you disable styles the code element would 'collapse', proving unreadable.
  9. Mark Alexander

    Mark Alexander Senior Member

    Semantically, the pre tag has no meaning, it modifies appearance, which is something that should be left to CSS. Arguably not for a language where whitespace has real value, but for PHP all it's doing is improving readability.
  10. glenwheeler

    glenwheeler Senior Member

    I am not dealing with CSS here I'm talking PHP code that won't display. You can see on the page its creating a search box instead of the code because it's calling the form from wp's default general file as my theme does not use the search-form.php file.
  11. Harry

    Harry Senior Member

    Yes, you need to encode your entites. Instead of putting <code><?php blah("blah"); ?></code> you need <code><?php blah("blah"); ?></code>

    Bastard thing is making the semi-colon–bracket into a wink, but you get what I mean. Putting unescaped chevrons into markup (through a WYSYWIG or otherwise) will cause a user agent to try parsing the code.
  12. Harry

    Harry Senior Member

    It provides pre-formatted text, which you require when writing out code that requires whitespace to be fully understood.
    The markup is the information, whitespace in code is information, therefore it should be added as such. Adding meaning through CSS (i.e. the indents, line breaks etc) is just wrong.
  13. Mark Alexander

    Mark Alexander Senior Member

    So does the CSS declaration I mentioned. The whitespace has no real value in PHP, it serves only to aid code readability, as would many other depreciated style tags that you wouldn't think of using.
  14. Renniks

    Renniks Senior Member

    Whitespace in coding is not a design feature, or a layout feature, it is a content feature.

    The whitespace is part of the content.

    Sure it isn't needed. But that makes it "unnecessary" content, not something else.

    It's like arguing against using <em> or <strong> they have there uses, and should be used in those situations.
  15. Harry

    Harry Senior Member

    You should never think of markup as working-when-used-with-CSS. The two should not have to be combined for something to be readable. Not using <pre> means that, without CSS, the code wouldn't be formatted correctly, the whitespace is content, not style.
  16. Sunburn

    Sunburn Active Member

    more tea vicar?
  17. Mark Alexander

    Mark Alexander Senior Member

    Like I said, arguably for some languages where whitespace is important but not PHP - where the code runs the same no matter what tabs, spaces, line breaks or whatever is in between.

    But it's perfectly correct without the pre tag.

    <?php if (1) { echo 'Hello'; } ?>
    is just as valid and meaningful as

    if (1) {
    For the latter you've just changed the format, ie the visual appearance, to make it easier to understand. Not in order to make the content valid or meaningful, because it was already valid and meaningful.

    The code retains the same value whether you space it out or turn it bright pink.
  18. Harry

    Harry Senior Member

    Yes, the code reads the same, but readability should be added at markup level, not at style level.

    The example you use is very handy for your argument, but take:
     * Get the week start and end from the datetime or date string from mysql.
     * @since 0.71
     * @param string $mysqlstring Date or datetime field type from mysql.
     * @param int $start_of_week Optional. Start of the week as an integer.
     * @return array Keys are 'start' and 'end'.
    function get_weekstartend$mysqlstring$start_of_week '' ) {
    $my substr$mysqlstring0); // Mysql string Year
    $mm substr$mysqlstring8); // Mysql string Month
    $md substr$mysqlstring5); // Mysql string day
    $day mktime000$md$mm$my ); // The timestamp for mysqlstring day.
    $weekday date'w'$day ); // The day of the week from the timestamp
    $i 86400// One day
    if( !is_numeric($start_of_week) )
    $start_of_week get_option'start_of_week' );

        if ( 
    $weekday $start_of_week )
    $weekday $start_of_week $weekday;

        while ( 
    $weekday $start_of_week ) {
    $weekday date'w'$day );
            if ( 
    $weekday $start_of_week )
    $weekday $start_of_week $weekday;

    $day -= 86400;
    $i 0;
    $week['start'] = $day 86400 $i;
    $week['end'] = $week['start'] + 604799;
    which would, without styles, become:

    /** * Get the week start and end from the datetime or date string from mysql. * * @since 0.71 * * @param string $mysqlstring Date or datetime field type from mysql. * @param int $start_of_week Optional. Start of the week as an integer. * @return array Keys are 'start' and 'end'. */function get_weekstartend$mysqlstring$start_of_week '' ) {$my substr$mysqlstring0); // Mysql string Year$mm = substr( $mysqlstring, 8, 2 ); // Mysql string Month$md = substr( $mysqlstring, 5, 2 ); // Mysql string day$day = mktime( 0, 0, 0, $md, $mm, $my ); // The timestamp for mysqlstring day.$weekday = date( 'w', $day ); // The day of the week from the timestamp$i = 86400; // One dayif( !is_numeric($start_of_week) )$start_of_week = get_option( 'start_of_week' );if ( $weekday < $start_of_week )$weekday = 7 - $start_of_week - $weekday;while ( $weekday > $start_of_week ) {$weekday = date( 'w', $day );if ( $weekday < $start_of_week )$weekday = 7 - $start_of_week - $weekday;$day -= 86400;$i = 0;}$week['start'] = $day + 86400 - $i;$week['end'] = $week['start'] + 604799;return $week;}
    Also, just stop and think, you're deleting one element and forcing another to behave just like it—does that not seem a little, well, pointless? <pre> is a valid and non-deprecated element, use it.
  19. Renniks

    Renniks Senior Member

    If you are posting code up to be read, it should be readable.

    If it isn't posting to be read, it should be supplied in an external document (in my opinion)
  20. Mark Alexander

    Mark Alexander Senior Member

    I'm not denying that it becomes unwieldy. There are many presentational aspects which, when taken away, would result in unwieldiness. That doesn't stop them being presentational though. Take away CSS and you'll have copy that's horribly more difficult to read than a document with appropriate spacing and line height and line length and all that, but you wouldn't use that as an excuse to ignore the content/presentation separation rule.

    Well bear in mind we're talking about people without CSS here, the vast majority of people are seeing the code as intended if you just use CSS.

Share This Page