3

In both perl and python, you have to print some headers (if you want) and a new line before the page content. Otherwise you get an error - End of script output before headers.

In php things are different. Whenever you print something (using either print "" or echo ""), it is assumed as a plain text, even if you try to print a header. To do this (print a header), you should use the header() function (which is not available in perl and python).
Some examples:

<?php
# this is a plain text in the page, you can use 'echo ""' to get the same result
print "Content-type: text/plain; charset=utf-8\n\n";

#!usr/bin/perl 
# this is a header with two new lines after it (without them you get the error)
print "Content-type: text/plain; charset=utf-8\n\n";
# (the same is in python)

<?php
print "Some text, no headers are sent, no new lines are printed and you get no errors";

#!usr/bin/perl
print "Some text, no headers are sent, no new lines are printed and you get an error.";
# (the same is in python)

You have to place a new line before text in perl in python but not in php. The same code in similar languages has such a different result?

Why in perl and python you print() the headers but in php you header() them?

Why in php you don't have to print new line to say "end of headers"?

2
  • 1
    A whole php platform is designed toward clueless programmers and most of design decisions draws from this basic idea. So presumption of print in php means that user wants to output html/text and not headers which are implicit in such scenario. Commented Dec 29, 2014 at 11:51
  • 2
    The assumption in your question isn't true in general. Lots of Perl and Python web frameworks support output buffering, and lots of them have a special function to set a header. You're looking at one antiquated way of communicating with a webserver (CGI) and assuming that it's a difference in the languages, but it has nothing to do with that, really. Commented Dec 29, 2014 at 17:25

1 Answer 1

3

PHP follows the "X Server Pages" processing model - ASP and JSP are two other examples of this paradigm - everything in the template is text to be written to the client unless wrapped in special tags which cause the PHP engine to execute code. Think of those chunks of text as being implicitly wrapped print statements. In general, the output is buffered until processing reached the end of the template, at which point the accumulated headers are written followed by the accumulated page output buffer.

The Perl code that you've provided is an example of CGI programming, and using raw Perl, or using a CGI module, everything in the file (a plain old Perl program) is code, and the output is sent to the client in the order that it's written. Since the HTTP protocol says that first you send headers, and then you send page content, that's why you have to print your headers, before you print your page content.

So your Perl program is the lowest level processing you can get. You're doing all of it from scratch because you're using a general purpose programming language and not using any modules that package higher-level web processing functionality.

PHP, on the other hand, is a higher-level program system, specifically for generating HTML pages. So PHP knows that you need headers as well as content (while Perl just thinks you're printing stuff), and expects you to use the header() function that it provides.

I suspect that you're doing things in Python in a similar way to what you're doing in Perl.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.