Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #3437
| From | "Peter H. Coffin" <hellsop@ninehells.com> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: Taking parameters into functions |
| Date | 2011-10-16 11:57 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <slrnj9m378.hbg.hellsop@nibelheim.ninehells.com> (permalink) |
| References | <slrnj9ll4o.3ru.houghi@penne.houghi> |
On Sun, 16 Oct 2011 14:56:56 +0200, houghi wrote:
> I have the following code:
>
> $File = "test.txt";
> function write_fifo($data,$File) {
> echo $data."<br>".$File."<hr>";
> $fh = fopen($File, 'w') or exit("can't open file -".$File."= .");
> fwrite($fh, $data);
> fclose($fh);
> }
> write_fifo("Something Here",$File);
>
> This works. What I want to ask is about the write_fifo() part. Is it
> possible to have something like the following:
> write_fifo("Something Here");
> instead of the line including $File.
>
> Not a real must. Just a nice to have. It would make my code perhaps a
> bit cleaner AND I would have learned something that I could use in the
> future.
>
> I have looked, but could not find anything. Probably because I have no
> idea what I should be looking for (php novice and all that)
In addition to Jerry's method (which I agree is a bad idea, for the
reasons he gives), you can also default $File in the function
declaration and make the parameter optional
function write_fifo($data,$File = "test.txt") {
echo $data."<br>".$File."<hr>";
$fh = fopen($File, 'w') or exit("can't open file -".$File."= .");
fwrite($fh, $data);
fclose($fh);
}
You can even acheive a level of portability by defaulting the setting of
the parameter to a constant, like
define ("fifoFile", "/path/to/default/fifo/file");
...
function write_fifo($data,$File = fifoFile) {
echo $data."<br>".$File."<hr>";
$fh = fopen($File, 'w') or exit("can't open file -".$File."= .");
fwrite($fh, $data);
fclose($fh);
}
--
2. My ventilation ducts will be too small to crawl through.
--Peter Anspach's list of things to do as an Evil Overlord
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Taking parameters into functions houghi <houghi@houghi.org.invalid> - 2011-10-16 14:56 +0200
Re: Taking parameters into functions Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-16 09:27 -0400
Re: Taking parameters into functions houghi <houghi@houghi.org.invalid> - 2011-10-16 16:16 +0200
Re: Taking parameters into functions Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-16 14:32 -0400
Re: Taking parameters into functions "Peter H. Coffin" <hellsop@ninehells.com> - 2011-10-16 11:57 -0500
Re: Taking parameters into functions houghi <houghi@houghi.org.invalid> - 2011-10-16 20:15 +0200
Re: Taking parameters into functions "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2011-10-17 10:10 +0200
csiph-web