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 #175658 is a reply to message #175657] Sun, 16 October 2011 13:27 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
Senior Member
On 10/16/2011 8:56 AM, 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)
>
> houghi

You could, by making $File global in your function. But it's a very bad
thing to do. It now makes the function dependent on a variable outside
of it. You couldn't, for instance, use the function with a variable
named $outfile.

Parameters are good things to pass to functions, and you should strive
to use them as much as necessary, not get rid of the for "cleaner code".
Your code will end up much "dirtier" in the long run.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
[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: Sat Nov 23 22:42:38 GMT 2024

Total time taken to generate the page: 0.04720 seconds