In my view, the question is about using javascript to do the PHP work. A request made with ajax can return data formatted in back-end
or it can receive a json
and format for composition of a view. My answer is based on this assumption.
I will give as an example a simple request containing an array of 2 indexes, only first and last name and formats in JSON , XML and HTML for comparison purposes only.
DATA
array( array( 'Papa' , 'Charlie' ) , array( 'Papa' , 'Charlie' ) )
JSON
LENGTH : 39
STING : [["Papa","Charlie"],["Papa","Charlie"]]
XML
LENGTH : 191
STING : <usuarios>
<usuario><name>Papa</name><lastname>Charlie</lastname></usuario>
<usuario><name>Papa</name><lastname>Charlie</lastname></usuario>
</usuarios>
HTML
LENGTH : 66
STING : <div>Papa</div><div>Charlie</div><div>Papa</div><div>Charlie</div>
An output in HTML will use the server to process the data, and you can display the data using simple functions. It may be the best option when having little data flow or for those who are not so familiar with javascript. On the other hand, we have a greater flow of data and can make the application more secure.
Data in JSON is easy to work with type object
, in addition to having a much shorter length than other formats, and this is an interesting point for bandwidth savings. JSON and XML are more flexible and simple to implement webservices, APPs, desktop, mobile devices ...
I did not find a benchmark of respect that I could use as a comparison effect, but I do not see such a major drawback in processing time between PHP and javascript that it implies as a deciding factor for change. In fact PHP consumes more resources to build the HTML, while the javascript composes with low cost. But to answer which one to use or when, depends on the need - reduce traffic, flexibilizar the application to other platforms ... I think they are points of greater importance in the choice.
Some interesting references
• Why is it bad practice to return HTML instead of JSON? Or is it?
AJAX - Using JSON vs. echo HTML
• Why Facebook, Twitter and GMail render all their date to the browser as JSON as opposed to HTML?
• PHP vs. node.js: The REAL statistics
• The AJAX response: XML, HTML, or JSON?
• Creating HTML: PHP server-side vs. jQuery clie