Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #3434

Re: Taking parameters into functions

From Jerry Stuckle <jstucklex@attglobal.net>
Newsgroups comp.lang.php
Subject Re: Taking parameters into functions
Date 2011-10-16 09:27 -0400
Organization A noiseless patient Spider
Message-ID <j7em50$knr$1@dont-email.me> (permalink)
References <slrnj9ll4o.3ru.houghi@penne.houghi>

Show all headers | View raw


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@attglobal.net
==================

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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