Posting Code in the Forums

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Locked
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Posting Code in the Forums

Post by JAM »

( 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.

Code: Select all

 
<? 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

Code: Select all

YOUR PHP CODE HERE[ /syntax][/b] tags around php code posted in the forum and [b][code ]YOUR RESULT HERE[ /code ][/b] as preferred tags to show examples of output, code results. For plain SQL queries (not embedded into the php code) use the [b][syntax="sql" ]YOUR SQL QUERY HERE[ /syntax][/b]
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 [url=http://forums.devnetwork.net/viewtopic.php?t=21171#131836]feyd's post on the subject[/url].

[b]Bad Example, no tags:[/b]

<?php
   echo 'Hi!';
?>

[b]Good Example, with tags:[/b]

[syntax=php]<?php
   echo 'Hi!';
?>
Example of good courtesy, showing code, result and an example providing database layout in good manners:

My code is:

Code: Select all

<?php
   echo 'Hi!';
?>
The result I get is:

Code: Select all

Hi!

Code: Select all

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.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

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
JayBird
Admin
Posts: 4522
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

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

Mark
User avatar
m3mn0n
PHP Evangelist
Posts: 3547
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

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
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

STOP READING NOW. :wink:

feyd's

Code: Select all

-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:

Code: Select all

<?php
echo 'Hi';
?>

Line Number Offset

Code: Select all

<?php
echo 'This is a test of line number offset';
?>

becomes:
[syntax=php]<?php
echo 'This is a test of line number offset';
?>[/syntax]


[b]Labeling[/b]

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

becomes:
[syntax=php]<?php
echo 'This is to show the new labeling feature.';
?>[/syntax]


another example, note how the quotes are usually optional:

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

becomes:
[syntax=php]<?php
echo 'Another example of labeling.';
?>[/syntax]

[b]Labeling and Number Offset[/b]

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

becomes:
[syntax=php]<?php
// this is an example of labeling and number offset
?>[/syntax]


[b]An example of a very long line:[/b]

[syntax=php]?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; !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][/syntax]
becomes:
[syntax=php]<?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; !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;>';
?>[/syntax][size=84]finally got IE to work again.. [/size]


[b]A final, complex example:[/b]
[syntax=php]?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][/syntax]
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 12:28 pm, edited 1 time in total.
Locked