Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP
Author Message
killing-hours
RavenNuke(tm) Development Team



Joined: Oct 01, 2010
Posts: 438
Location: Houston, Tx

PostPosted: Fri Apr 08, 2011 7:37 am Reply with quote

Hey all-

I'm helping with a project and the coding methods that are used raise a few questions for me. Some feedback is appreciated.

1. PHP_EOL
I understand that PHP_EOL is used to get a new line.... but is it necessary?
Example:
Code:
echo '<script type="text/javascript">//<![CDATA['.PHP_EOL;

   echo '   $("button").click(function () {'.PHP_EOL;
   echo '      $("#post-comment").show("slow");'.PHP_EOL;
   echo '   });'.PHP_EOL;
   echo '//]]></script>'.PHP_EOL;


Is it necessary to have this at the end of each output line? I've written a couple of other applications but didn't use newline's and it seems my applications run fine... but now I'm worried they aren't correct.
Here are some comments I've read: [ Only registered users can see links on this board! Get registered or login! ]

2. CDATA

Using the above code as an example... what is the purpose of have the CDATA markers? If I'm reading this correctly, HTML parsers (reading "browsers"???) don't understand them anyhow.

wikipedia wrote:
Since it is useful to be able to use less-than signs (<) and ampersands (&) in web page scripts, and to a lesser extent styles, without having to remember to escape them, it is common to use CDATA markers around the text of inline <script> and <style> elements in XHTML documents. But so that the document can also be parsed by HTML parsers, which do not recognise the CDATA markers, the CDATA markers are usually commented-out
[ Only registered users can see links on this board! Get registered or login! ]

Thanks for your time.

_________________
Money is the measurement of time - Me
"You can all go to hell…I’m going to Texas" -Davy Crockett 
View user's profile Send private message
fkelly
Former Moderator in Good Standing



Joined: Aug 30, 2005
Posts: 3312
Location: near Albany NY

PostPosted: Fri Apr 08, 2011 8:08 am Reply with quote

Good questions and I will be interested in the answer(s) to the cdata one myself. Re. PHP_EOL I think that's similar to or identical with "\n" that we see in the RN code a lot. In my experience where it is useful is if you have to review output using view source. Let's say you have a lot of options in a select list, for example state abbreviations. It's a lot easier to look through the generated code for errors if they are all on separate lines rather than just strung out on a long single line. And, unfortunately, there are a lot of times when you will need to use view source to confirm what's being output or to debug.
 
View user's profile Send private message Visit poster's website
spasticdonkey
RavenNuke(tm) Development Team



Joined: Dec 02, 2006
Posts: 1693
Location: Texas, USA

PostPosted: Fri Apr 08, 2011 8:36 am Reply with quote

I think it's a coders choice, it's kinda nice to have easy to read code that is output to the browser. I like PHP_EOL as it's a little cleaner in the code than the alternative
'.PHP_EOL;
or
'."\n";

I suppose it does offer some cross platform compatibility but I would never recommend running on a windows server anyway, so... that's somewhat a wash. You can see pretty much what is does in the Content module

modules/Content/var/cpfunc.php
Code:
if(!defined('PHP_EOL')) define ('PHP_EOL', strtoupper(substr(PHP_OS,0,3) == 'WIN') ? "\r\n" : "\n");


As for CDATA, to the best of my understanding it's only used on inline JS so that the contents are not parsed as html, and usually wrapped in comments for older browsers that don't understand CDATA. If the browser does properly understand CDATA, it keeps you from having to escape certain characters to pass validation. Although, someone may have a better explanation Rolling Eyes
 
View user's profile Send private message Visit poster's website
killing-hours







PostPosted: Fri Apr 08, 2011 8:48 am Reply with quote

@fkelly's response...

PHP_EOL = "\n" or better "r\n\" (From what I gather in the comments from stackoverflow)

The reason I ask for it's purpose and in all reality... when it "should" be used... is for size sake. The script should be a small as possible and it seems to me that if the PHP_EOL or "\n" is overused... it's going to add unnecessary size to the script itself thus slowing the load time down. (although it may only be very minute)

@ CDATA ... if used in inline JS.. since RN uses the "addJsToHead" function to pass the inline to the head when it is loaded in in the browser... wouldn't that relegate the CDATA pointless. Also very interested in further discussion on this one.

Edit*** I could see the point of CDATA in the case that JS was used on a particular input or link or something where the inline is directly on the html and not run through the addJsToHead function.

Thanks for the responses guys. Helps me to understand things a bit better.
 
fkelly







PostPosted: Fri Apr 08, 2011 9:27 am Reply with quote

Quote:
The reason I ask for it's purpose and in all reality... when it "should" be used... is for size sake. The script should be a small as possible and it seems to me that if the PHP_EOL or "\n" is overused... it's going to add unnecessary size to the script itself thus slowing the load time down. (although it may only be very minute)


If diagnosing a problem is a little bit easier (and thus quicker) for the programmer because the output is more readable it will more than make up for any inefficiencies having a few extra "r\n"s in the code cost. There are many other things we can do to make the code efficient without rendering the output humanly unreadable, IMHO.
 
killing-hours







PostPosted: Fri Apr 08, 2011 9:34 am Reply with quote

Agreed. I don't however debug that way so in my instance it doesn't really matter I don't believe.

So I'm save to assume that it's not necessary for the server itself to see that next line character (unless say outputting data to a file of some sort) for compatibility etc. It's more or less for the ease of the programmer and debugging purposes or human readability... correct?
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP

View next topic
View previous topic
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 vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©