views:

262

answers:

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

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

?>

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

+2  A: 

It's likely C, not PHP. Try

<?php
    echo "Hello World";
?>
erenon
Oh you dont need to use a main() function?
Joshua
Every installed lib is included by default, and for simple output, use echo. main() doesn't neccesary.
erenon
No. PHP scripts just run from top to bottom.
JacobM
A: 

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

Evän Vrooksövich
A: 

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

   echo "Hello World";
wallyk
There is printf http://php.net/manual/en/function.printf.php
Gordon
There is printf :)
erenon
Oh yeah! I'd forgotten that. Fixing...
wallyk
+1  A: 

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.

Gordon
+3  A: 

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.

Wally Lawless
exit(main()); is probably slightly more accurate, but otherwise, +1
Frank Farmer
Of course, you're totally right. I was going with the assumption that the asker might want to continue doing something else with the return value of the function.
Wally Lawless