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


Groups > comp.lang.php > #3437

Re: Taking parameters into functions

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From "Peter H. Coffin" <hellsop@ninehells.com>
Newsgroups comp.lang.php
Subject Re: Taking parameters into functions
Date Sun, 16 Oct 2011 11:57:12 -0500
Organization A noiseless patient Spider
Lines 50
Message-ID <slrnj9m378.hbg.hellsop@nibelheim.ninehells.com> (permalink)
References <slrnj9ll4o.3ru.houghi@penne.houghi>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
Injection-Info mx04.eternal-september.org; posting-host="le/2G4+BNpieVhQI6uJ2Lw"; logging-data="15335"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18WkygicTbRz7q13vHnZy5G"
User-Agent slrn/0.9.9p1 (OpenBSD)
Cancel-Lock sha1:qlW2nbJIbvQj/5b0oOopKScQuI4=
Xref x330-a1.tempe.blueboxinc.net comp.lang.php:3437

Show key headers only | View raw


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


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