Re: writing to files
| From: | Philip Olson | Date: | Sat, 13 Oct 2001 09:55:25 +0000 |
| Subject: | Re: writing to files | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-70968@lists.php.net to get a copy of this message | ||
It's all about syntax. Here's an example that is similar yet
different.
First, let's define our string :
<?php
$string = "$firstname : $lastname\n\n";
if ($type == 'hero') {
$string .= "\tSUPER HERO\n";
} else {
$string .= "\tAverage Human\n";
}
?>
Now is a good time to learn about using strings in PHP. A
couple useful resources are as follows :
http://www.zend.com/zend/tut/using-strings.php
http://www.php.net/manual/en/language.types.string.php
Next, let's open and write our string to a file :
<?php
$filename = 'file.txt';
$fp = fopen($filename,'w');
if ($fp) {
fwrite($fp,$string);
fclose($fp);
} else {
echo "Unable to open file : $filename : for writing.";
exit;
}
echo "Success! $filename now contains the following : <br>";
echo "<pre>$string</pre>";
?>
Be sure to read about fopen() in the manual. Knowing about the different
'modes' is good, the above uses 'a' for appending purposes. See :
http://www.php.net/fopen
Hope that helps. Could probably use more error checking but who's
counting :)
Regards,
Philip Olson
On Sat, 13 Oct 2001, Adam Jackson wrote:
> Hello,
>
> I am fairly new to php, but would like to write my outputs to a file here is
> what I have so far..... but it writes things like "else" and "if" to the
> filename as well.
>
> <?
> $file = samplefile;
> $fd = fopen($file, "w");
> fwrite($fd, " \n
> #<?=$site_user?> - <?=$mailbox_id?> - <?=$mailbox_name?><br>
> <?=$mailbox_main_email?>> <?=$mailbox_id?><br>
> <?
> if($alias1 == NULL)
> {
> print("#");
> }
> else
> {
> ?>
> <?=$mailbox_alias1?>@<?=$web_address?> <?=$mailbox_id?>
> <?
> }
> ?
>
> and this displays text like
>
> # blah.com - blah-mail - blahmail.com
> blah@blah.com blah-mail
>
> etc etc
>
> any help would be greatly appreciated.
>
> -Adam
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>