Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Display php code on webpage without executing?
Old 09-18-2011, 08:08 PM Display php code on webpage without executing?
webspace's Avatar
Super Spam Talker

Posts: 933
Name: Buck Roberts
Location: Astoria, Oregon, United States
Trades: 0
I decided to actually learn PHP I'm just doing some tutorials I found on YouTube.

So say I wanted to show some php code on my webpage without executing it.

Is there HTML tags I can put the PHP in to display the code. Or do I do it with PHP script.

And which way works best?

And just is there an HTML tag that is equivalent to the BB code?
PHP Code:
     <?php

$num 
1;

while (
$num<=10)
{
echo 
"Loop $num <br />";

$num $num 1;
}

?>
This seems like a really stupid thing to not know.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
blog about paintings, drawings and photographs.

Please login or register to view this content. Registration is FREE
Weapons system and aerospace components
webspace is offline
Reply With Quote
View Public Profile Visit webspace's homepage!
 
 
Register now for full access!
Old 09-18-2011, 09:19 PM Re: Display php code on webpage without executing?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
So say I wanted to show some php code on my webpage without executing it.
Anything outside of the PHP tags (<?php ?>) is not going to be parsed.

Quote:
Is there HTML tags I can put the PHP in to display the code. Or do I do it with PHP script.
PHP and HTML are completely distinct. The output of a PHP script is typically HTML code but not necessarily. If you want to display PHP code as HTML then just write it as HTML outside of the PHP tags:

PHP Code:
<?php
//this code will be parsed
$num 1

while (
$num<=10

echo 
"Loop $num <br />"

$num $num 1
}
?>
This will not:
<pre>
$num = 1; 

while ($num&lt;=10) 

     echo "Loop $num &lt;br /&gt;"; 

     $num = $num + 1; 
}
</pre>
The pre tag will maintain the formatting (tabs, newlines and whitespace). Notice that I replaced the wickets ( < and > ) with their html entities. You can use PHP to simplify this process:
PHP Code:
<?php
ob_start
(); //start an output buffer
?>
$num = 1; 

while ($num<=10) 

echo "Loop $num <br />"; 

$num = $num + 1; 
}
<?php
$source 
ob_get_clean(); //assign the content of the buffer to $source
echo '<pre>' .  htmlentities($source) . '</pre>'//output $source with html entities
?>
Edit:
PHP also comes with a function that you can use to highlight source code:
http://us3.php.net/manual/en/functio...ght-string.php
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 09-20-2011 at 11:34 PM..
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-18-2011, 11:08 PM Re: Display php code on webpage without executing?
webspace's Avatar
Super Spam Talker

Posts: 933
Name: Buck Roberts
Location: Astoria, Oregon, United States
Trades: 0
Thanks for the info. Exactly what I was looking for. Much appreciated.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
blog about paintings, drawings and photographs.

Please login or register to view this content. Registration is FREE
Weapons system and aerospace components

Last edited by webspace; 09-18-2011 at 11:13 PM..
webspace is offline
Reply With Quote
View Public Profile Visit webspace's homepage!
 
Old 09-19-2011, 03:40 AM Re: Display php code on webpage without executing?
vectorialpx's Avatar
Extreme Talker

Posts: 249
Name: octavian
Location: Bucharest
Trades: 0
PHP Code:
<?php
highlight_string
('<?php
    echo "hello";
?>'
);
?>
__________________
you can
Please login or register to view this content. Registration is FREE
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 09-19-2011, 03:45 AM Re: Display php code on webpage without executing?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
http://tycoontalk.freelancer.com/htm...tml#post294819
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-19-2011, 06:18 AM Re: Display php code on webpage without executing?
vectorialpx's Avatar
Extreme Talker

Posts: 249
Name: octavian
Location: Bucharest
Trades: 0
PHP Code:
<?php
highlight_string
('<?php
    echo "hello";
?>'
);
?>
Code:
<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"hello"</span><span style="color: #007700">;
<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code>
So, highlight_string will also strip special chars
__________________
you can
Please login or register to view this content. Registration is FREE
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 09-25-2011, 03:10 PM Re: Display php code on webpage without executing?
webspace's Avatar
Super Spam Talker

Posts: 933
Name: Buck Roberts
Location: Astoria, Oregon, United States
Trades: 0
Thanks for the help everybody
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
blog about paintings, drawings and photographs.

Please login or register to view this content. Registration is FREE
Weapons system and aerospace components
webspace is offline
Reply With Quote
View Public Profile Visit webspace's homepage!
 
Reply     « Reply to Display php code on webpage without executing?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.25421 seconds with 12 queries