Forum Moderators: coopster

Message Too Old, No Replies

My "Hello World" is not working. ;-(

My "Hello World" is not working. ;-(

         

haryanto

8:39 am on Oct 3, 2004 (gmt 0)

10+ Year Member



Hi guys, I have 2 scripts.

1.helloworld.php
<?
echo "hello world";
?>

2.myhelloworld.php
<?
$myhelloworld = require('/home/chippy/public_html/helloworld.php');

$myfile = @fopen('/home/chippy/public_html/myhelloworld.html', "w");

$fp = fwrite($myfile,$myhelloworld);

fclose($myfile);

?>

I am trying to output the result of the first PHP script to myhelloworld.html but so far it only outputs '1' on that HTML page.
By right, the HTML page should have 'hello world' but that is not happening.

Please help!

Thanks in advance!

Hanu

9:10 am on Oct 3, 2004 (gmt 0)

10+ Year Member



Call a function that returns the string.

1.helloworld.php
<?
function hello() {
return
"hello world";
}
?>

<?
require('/home/chippy/public_html/helloworld.php');
$myhelloworld = hello();

$myfile = @fopen('/home/chippy/public_html/myhelloworld.html', "w");

$fp = fwrite($myfile,$myhelloworld);

fclose($myfile);

?>

Or you could play around with output buffering [de3.php.net].

haryanto

9:18 am on Oct 3, 2004 (gmt 0)

10+ Year Member



what if I don't modify my helloworld.php

is there any way round this?

Hanu

9:38 am on Oct 3, 2004 (gmt 0)

10+ Year Member



You can use the output buffering method I mentioned. The problem with your aproach is that require doesn't return the output of the included script. The script outputs to the browser. Output buffering is a way to intercept a script's output.

If you use Apache, you could configure PHP to parse html files in addition to php files. Then you simply include/require helloworld.php file in helloworld.html.

I'm not sure what you are trying to accomplish. Maybe there's an easier way ...

mykel79

10:39 am on Oct 3, 2004 (gmt 0)

10+ Year Member



You could also try using file_get_contents(), which reads and entire file into a string, instead of using require().

haryanto

11:13 am on Oct 3, 2004 (gmt 0)

10+ Year Member



file_get_contents only writes the contents of my PHP files. I need to write the parsed values into the HTML file instead of the source code itself.

Hanu

11:44 am on Oct 3, 2004 (gmt 0)

10+ Year Member



So what about my suggestions?

haryanto

12:01 pm on Oct 3, 2004 (gmt 0)

10+ Year Member



Hanu,

Well this example is just a simplistic view of what my main script is all about.I wanted to write the output of a PH file to a HTML file basically.

I really wished php has a output_parsed('hello world.php');

Hanu

12:38 pm on Oct 3, 2004 (gmt 0)

10+ Year Member



Have you looked into output buffering? The link is in my first post. I'm quite certain it's possible. Also search the PEAR site. Someone must have written a wrapper the around PHP's output buffering framework that does exactly what you need.

ergophobe

6:08 pm on Oct 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think Hanu is right. What you want is ob_get_contents() [php.net]

This is designed for exactly the situation you describe and should work like a charm.

That said, instead of

echo "<h1>Hello World</h1>";

why don't you write your code as

$html = "<h1>Hello World</h1>";

and then write the $html to a file?

Tom

mincklerstraat

9:09 am on Oct 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Elaborating on Hanu's comment (necessary for understanding why this doesn't work):

To make this work the way you want, you'd have to change your first hellowold script - instead of:

echo "hello world";

you'd need:

return "hello world";

jezra

4:55 pm on Oct 4, 2004 (gmt 0)

10+ Year Member



This might do the trick for you
<?
//get the path to a php script
$file_to_parse="/home/chippy/public_html/helloworld.php";
//get the path to the php executable
//name our output file( do we have write access?)
$out_put_file = "/home/chippy/public_html/myhelloworld.html";
// this will vary from system to system
$path_to_PHP = "/usr/bin/php";
exec("$path_to_PHP -f $file_to_parse > $out_put_file");
?>