Posting Code in the Forums

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Posting Code in the Forums

Postby JAM » Mon Apr 19, 2004 3:22 pm

( feyd | If this is your first time reading this thread, please make sure to read the entire thread. )

· Important links:

PHPDN's General Posting Guidelines - Should be read first thing.
Asking Smart Questions - How and how not to ask questions.
Boards Search System - Should be used prior to asking questions. Alot of the questions have allready been asked and answered.
PHP Starter Pack - Fundamental PHP Know-How - Tutorial is aimed at people taking their first steps with php. (By McGruff)

· Tweaks during post:

Short/Long PHP tags in code:

php.ini wrote:; Allow the <? tag. Otherwise, only <?php and <script> tags are recognized.
; NOTE: Using short tags should be avoided when developing applications or
; libraries that are meant for redistribution, or deployment on PHP
; servers which are not under your control, because short tags may not
; be supported on the target server. For portable, redistributable code,
; be sure not to use short tags.
short_open_tag = Off

With the above quote in mind, take a moment and think about on how you write code. If you use the short-tag version when typing, you must realize that the code might not work properly at all times. If I personally would copy-n'-paste code using short-tags, the result would be unpleasant because of the setting above.

I, as many others, use the long-tag version due to the fact that this works everywhere. If you spend hours/days/months creating your perfect application you would not like it to break if you switch hosting server... Just a friendly warning.

Syntax: [ Download ] [ Hide ]
 
<? echo 'foo'; ?>    <- Might generate issues.
<% echo 'foo'; %>    <- Might generate issues.
<?= 'foo'; ?>        <- Might generate issues.
<?php echo 'foo'; ?> <- Works.
 


The use of tags:

Please, use the [syntax="php" ]YOUR PHP CODE HERE[ /syntax] tags around php code posted in the forum and [code ]YOUR RESULT HERE[ /code ] as preferred tags to show examples of output, code results. For plain SQL queries (not embedded into the php code) use the [syntax="sql" ]YOUR SQL QUERY HERE[ /syntax]
Not just is this posts easier to read, it will also make people actually read the post instead of ignoring it. For more indepth reading on these custom tags, continue reading the thread or skip down directly to feyd's post on the subject.

Bad Example, no tags:

<?php
echo 'Hi!';
?>

Good Example, with tags:

Syntax: [ Download ] [ Hide ]
<?php
   echo 'Hi!';
?>


Example of good courtesy, showing code, result and an example providing database layout in good manners:

My code is:
Syntax: [ Download ] [ Hide ]
<?php
   echo 'Hi!';
?>
The result I get is:
Syntax: [ Download ] [ Hide ]
Hi!


Syntax: [ Download ] [ Hide ]
CREATE TABLE `tracker` (
  `PkEcode` int(4) NOT NULL DEFAULT '0',
  `Date_txt` date NOT NULL DEFAULT '0000-00-00',
  `Team` varchar(30) NOT NULL DEFAULT '',
  `CourseCode` varchar(30) NOT NULL DEFAULT '',
  `Activity` varchar(100) NOT NULL DEFAULT '',
  `Slides` int(5) NOT NULL DEFAULT '0',
  `Time` decimal(5,2) DEFAULT NULL,
  `Recordcount` int(12) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY  (`Recordcount`)) TYPE=MyISAM AUTO_INCREMENT=165 ;
INSERT INTO `tracker` VALUES (4001, '2004-06-22', 'Audio', 'Project-1', 'Audio Editing', 12, '7.00', 145);
INSERT INTO `tracker` VALUES (4002, '2004-06-22', 'Audio', 'Project-1', 'Audio Editing', 12, '8.00', 146);
INSERT INTO `tracker` VALUES (4002, '2004-06-24', 'Audio', 'Project-1', 'Audio Cutting', 34, '10.00', 147);
INSERT INTO `tracker` VALUES (4003, '2004-06-22', 'Development', 'Project-1', 'Animation', 15, '8.00', 148);
INSERT INTO `tracker` VALUES (4004, '2004-06-24', 'Development', 'Project-1', 'Animation', 15, '8.00', 149);
INSERT INTO `tracker` VALUES (4005, '2004-06-22', 'QC', 'Project-1','QC-Level1',  24, '4.00', 154);
INSERT INTO `tracker` VALUES (4006, '2004-06-24', 'QC', 'Project-1','QC-Level2',  24, '4.00', 155);
Last edited by pickle on Mon Jun 06, 2011 10:45 am, edited 9 times in total.
Reason: Fixed code tags. Pickle: spelling
User avatar
JAM
DevNet Resident
 
Posts: 2100
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden

Postby tim » Mon Apr 19, 2004 5:37 pm

ahhhhhhhhhhmen JAM.

Such great advice for those asking questions/answering. Most people wont even read a script consisitng of < 10 lines if they arent in tags.

:wink:
User avatar
tim
DevNet Resident
 
Posts: 1165
Joined: Thu Feb 12, 2004 8:19 pm
Location: ohio

Postby JayBird » Tue Apr 20, 2004 2:57 am

Excellent idea JAM, all i have done recently is move posts and tell people to use the BBCode!

Mark
User avatar
JayBird
Admin
 
Posts: 4522
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK

Postby m3mn0n » Thu Sep 02, 2004 12:55 pm

Thanks to everyone who complies with this wonderful suggestion.


It makes it that much more easy and helpful to people who want to offer their time to you to solve your particular problem.
User avatar
m3mn0n
PHP Evangelist
 
Posts: 3546
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Postby feyd » Mon Sep 06, 2004 3:20 am

STOP READING NOW. :wink:

feyd's
Syntax: [ Download ] [ Hide ]
-tag script is currently broken, but the below will be kept as it likely will be put online again.
 
// JAM
 
[b]· · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · [/b]
 
All about the new, improved [php] tags::
 
We've got line numbers now! You can offset the starting line number too! We've got labeling of code. And it's still easily copied! Here's how to use it:
 
 
[b]The basics still work[/b]
 
[php]<?php
echo 'Hi';
?>


becomes:
Syntax: [ Download ] [ Hide ]
<?php
echo 'Hi';
?>



Line Number Offset

[php=20]<?php
echo 'This is a test of line number offset';
?>

becomes:
Syntax: [ Download ] [ Hide ]
<?php
echo 'This is a test of line number offset';
?>



Labeling

[php="foo.php"]<?php
echo 'This is to show the new labeling feature.';
?>

becomes:
Syntax: [ Download ] [ Hide ]
<?php
echo 'This is to show the new labeling feature.';
?>



another example, note how the quotes are usually optional:

[php=bar.php]<?php
echo 'Another example of labeling.';
?>

becomes:
Syntax: [ Download ] [ Hide ]
<?php
echo 'Another example of labeling.';
?>


Labeling and Number Offset

[php=foo.php|224]<?php
// this is an example of labeling and number offset
?>

becomes:
Syntax: [ Download ] [ Hide ]
<?php
// this is an example of labeling and number offset
?>



An example of a very long line:

Syntax: [ Download ] [ Hide ]
?php=highlight.php|166]<?php
        //  strip unwanted data
        /*
        $fonts?] = '#<br?^>]*?>#i';
        $rep?] = "";
        */

        $fonts?] = '#^( )+(<br?^>]*?>)?$#im';
        $rep?] = '\\2';
       
        //  set up all the html for displaying it.
        $block?'highlighted'] = '<div class="php"><div class="lang">'.( !empty($matches?5]) &amp;amp;&amp;amp; !preg_match('#^?0-9]+$#',$matches?5]) ? make_clickable(htmlentities(preg_replace('#^(?''"]?)(.*?)\\1$#','\\2',$matches?5]),ENT_QUOTES)) : $lang?'PHPCode'] ).' (' . $block?'lines'] . ($block?'lines'] != 1?' ' . $lang?'Lines']:' ' . $lang?'Line']) . ') ::</div>';
        $block?'highlighted'] .= '<div class="container"><table class="block"><tr>';
        $block?'highlighted'] .= '<td class="linenumber" nowrap="nowrap">';
?>?/php]

becomes:
Syntax: [ Download ] [ Hide ]
<?php
        //  strip unwanted data
        /*
        $fonts?] = '#<br?^>]*?>#i';
        $rep?] = &quote;&quote;;
        */

        $fonts?] = '#^( )+(<br?^>]*?>)?$#im';
        $rep?] = '\\2';
       
        //  set up all the html for displaying it.
        $block?'highlighted'] = '<div class=&quote;php&quote;><div class=&quote;lang&quote;>'.( !empty($matches?5]) &amp;amp;&amp;amp; !preg_match('#^?0-9]+$#',$matches?5]) ? make_clickable(htmlentities(preg_replace('#^(?''&quote;]?)(.*?)\\1$#','\\2',$matches?5]),ENT_QUOTES)) : $lang?'PHPCode'] ).' (' . $block?'lines'] . ($block?'lines'] != 1?' ' . $lang?'Lines']:' ' . $lang?'Line']) . ') ::</div>';
        $block?'highlighted'] .= '<div class=&quote;container&quote;><table class=&quote;block&quote;><tr>';
        $block?'highlighted'] .= '<td class=&quote;linenumber&quote; nowrap=&quote;nowrap&quote;>';
?>
finally got IE to work again.. \":roll:\"


A final, complex example:
Syntax: [ Download ] [ Hide ]
?php="http://foo.com/foo.php?this+is+an+example=of%34a+complex+setup"|42]<?php
    preg_match_all('#</?(font|span)?^>]*?>#i',$block?'highlight'],$fonts);
   
    $fonts = $fonts?0];
    $color_names = array_keys($colors);
    $color_values = array_values($colors);
    foreach($fonts as $k => $font)
    {
        $fonts?$k] = '#'.preg_quote($font,'#').'#';
        if($font{1} != '/')
        {
            preg_match('@color\s*?:=]\s*"?\s*(#?0-9A-F]{6}|#?0-9A-F]{3}|?a-z0-9-]+)@i',$font,$match);
            $rep?$k] = '<span class="'.$color_names?(($p = array_search($match?1],$color_values)) !== false ? $p : 0)].'">';
        }
        else
            $rep?$k] = '</span>';
    }
?>?/php]

turns into:
[php=\"http://foo.com/foo.php?this+is+an+example=of%34a+complex+setup\"|42]<?php
preg_match_all('#</?(font|span)?^>]*?>#i',$block?'highlight'],$fonts);

$fonts = $fonts?0];
$color_names = array_keys($colors);
$color_values = array_values($colors);
foreach($fonts as $k => $font)
{
$fonts?$k] = '#'.preg_quote($font,'#').'#';
if($font{1} != '/')
{
preg_match('@color\s*?:=]\s*&quote;?\s*(#?0-9A-F]{6}|#?0-9A-F]{3}|?a-z0-9-]+)@i',$font,$match);
$rep?$k] = '<span class=&quote;'.$color_names?(($p = array_search($match?1],$color_values)) !== false ? $p : 0)].'&quote;>';
}
else
$rep?$k] = '</span>';
}
?>
Last edited by feyd on Tue Mar 08, 2005 1:28 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
 
Posts: 31559
Joined: Mon Mar 29, 2004 4:24 pm
Location: Bothell, Washington, USA


Return to PHPGD.com

Who is online

Users browsing this forum: No registered users and 3 guests