FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Taking parameters into functions
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Taking parameters into functions [message #175660 is a reply to message #175657] Sun, 16 October 2011 16:57 Go to previous messageGo to previous message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma:
Senior Member
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Stats comp.lang.php (last 7 days)
Next Topic: Editing a combobox
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 01:25:29 GMT 2024

Total time taken to generate the page: 0.04242 seconds