Question

<?php
include("stdio.h");

function main()
{
    printf("Hello World");
    return 0;
}

?>

Error on line 2? Dunno what I'm doing wrong.

Was it helpful?

Solution

As others have said, your code looks like C code inside a PHP tag. Here is the PHP equivalent of what you're trying to do:

<?php    
printf("Hello World");    
?>

However, if you actually did need the main() function, it would look like this:

<?php    
function main()
{
    printf("Hello World");
    return 0;
}
$returnValue = main();

?>

This would have the result of echoing the string "Hello World" and setting $returnValue to 0.

OTHER TIPS

It's likely C, not PHP. Try

<?php
    echo "Hello World";
?>

The include() statement includes and evaluates the specified file.

So, PHP will try to parse the contents inside stdio.h and since this is likely full of C Code, there will be errors, because that is not what PHP expects to find in there.

Check the PHP Manual for further reference.

Line 2 is include("stdio.h");. This is unnecessary.

PHP isn't C. There is no stdio.h, nor is there printf. (But echo is usually used if not formatting.)

   echo "Hello World";

The correct PHP equivalent of that pseudo-C program is:

Hello World

Note: no <?php opening tags required.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top