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


Groups > comp.lang.php > #6890 > unrolled thread

Sending nothing when function expects variable

Started byJason C <jwcarlton@gmail.com>
First post2012-08-27 23:51 -0700
Last post2012-08-28 09:23 +0100
Articles 6 — 6 participants

Back to article view | Back to comp.lang.php


Contents

  Sending nothing when function expects variable Jason C <jwcarlton@gmail.com> - 2012-08-27 23:51 -0700
    Re: Sending nothing when function expects variable Goran <goran@nospam.com> - 2012-08-28 09:08 +0200
      Re: Sending nothing when function expects variable Scott Johnson <noonehome@chalupasworld.com> - 2012-08-28 06:01 -0700
    Re: Sending nothing when function expects variable Anders Wegge Keller <wegge@wegge.dk> - 2012-08-28 09:21 +0200
    Re: Sending nothing when function expects variable "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2012-08-28 09:55 +0200
    Re: Sending nothing when function expects variable Tim Streater <timstreater@greenbee.net> - 2012-08-28 09:23 +0100

#6890 — Sending nothing when function expects variable

FromJason C <jwcarlton@gmail.com>
Date2012-08-27 23:51 -0700
SubjectSending nothing when function expects variable
Message-ID<03b15810-f401-4ad2-a1a4-c4f10f0f311e@googlegroups.com>
I have a function like:

function whatever($a, $b, $c) {
  if (!$b) $b = "something";
  if (!$c) $c = "something else";

  // etc
}

Since I'm checking for $b and $c in the function, I call the functions like this when $b and $c are going to be the defaults:

whatever('this thing');

But then in the error log, I get these warnings:

PHP Warning:  Missing argument 2 for q()...
PHP Warning:  Missing argument 3 for q()...

Does this mean that I need to go back through all of the scripts (dozens of them) and change all of the whatever() references to:

whatever('this thing', false, false);

or is there a way to set PHP to ignore this warning by default?

[toc] | [next] | [standalone]


#6891

FromGoran <goran@nospam.com>
Date2012-08-28 09:08 +0200
Message-ID<k1hqq4$krc$1@news.metronet.hr>
In reply to#6890
On 28.8.2012 8:51, Jason C wrote:
> or is there a way to set PHP to ignore this warning by default?

Declare function this way:

function whatever($a, $b = '', $c = '')
{
  // your existing code
}

[toc] | [prev] | [next] | [standalone]


#6895

FromScott Johnson <noonehome@chalupasworld.com>
Date2012-08-28 06:01 -0700
Message-ID<k1ifef$v68$1@dont-email.me>
In reply to#6891
On 8/28/2012 12:08 AM, Goran wrote:
> On 28.8.2012 8:51, Jason C wrote:
>> or is there a way to set PHP to ignore this warning by default?
>
> Declare function this way:
>
> function whatever($a, $b = '', $c = '')
> {
>    // your existing code
> }
>

This is a common problem, at least for me when first creating custom 
class libraries for repeated use.

I find a way to make a function more useable over several needs and I 
add extra parameters.

The default argument method works OK but you also need to make sure that 
the defaults are received in order.

For example.

if you write
	function func($a, $b='', $c='')

You cannot call $c without $b.

	func('a','c')
will put 'c' in $b.

A few options that I have used is to pass an array of needed variables.

	function func($arg_ary) {
		//parse out $arg_ary
	}
Call
	$args = array('a'=>'a data','c'=>'c data')
	func($args)


Or use the php built function

	function func() {
		// put incoming args in an array
		$args_ary = func_get_args();
		
		// Parse $args_ary
	}

call
	func('a','b');


These are obvious bare bone non-working examples with no error checking.

Also
http://www.php.net/manual/en/index.php is your best friend.



[toc] | [prev] | [next] | [standalone]


#6892

FromAnders Wegge Keller <wegge@wegge.dk>
Date2012-08-28 09:21 +0200
Message-ID<87wr0jjxc5.fsf@huddi.jernurt.dk>
In reply to#6890
Jason C <jwcarlton@gmail.com> writes:

> But then in the error log, I get these warnings:
> 
> PHP Warning:  Missing argument 2 for q()...
> PHP Warning:  Missing argument 3 for q()...
 
> Does this mean that I need to go back through all of the scripts
> (dozens of them) and change all of the whatever() references to:
 
> whatever('this thing', false, false);

 Not neccesarily. You can declare the function with default arguments:

function whatever($a, $b = "something", $c = "something else") {
 ...
}

 If no arguments for b and c is passed, the function will get the
defaults instead. This replacement will not be visible inside the
function. If this is of importance, or the defaults are calculated
somehow, you'd be better off using NULL as default value:

function whatever($a, $b = NULL, $c = NULL) {

  if (!isset($b)) {
    /* Calculate default */
    $b = <<some complex operation>>;
  }

}

 Example 3 and 4 in the manual page describe your case pretty well:

 <http://php.net/manual/en/functions.arguments.php>
 
> or is there a way to set PHP to ignore this warning by default?

 In theory you could use error_reporting(E_ERROR), but that would be
purely cosmetic.

-- 
/Wegge

Leder efter redundant peering af dk.*,linux.debian.*

[toc] | [prev] | [next] | [standalone]


#6893

From"Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid>
Date2012-08-28 09:55 +0200
Message-ID<k1hth4$p2d$1@dont-email.me>
In reply to#6890
El 28/08/2012 8:51, Jason C escribió/wrote:
> But then in the error log, I get these warnings:
>
> PHP Warning:  Missing argument 2 for q()...
> PHP Warning:  Missing argument 3 for q()...
>
> Does this mean that I need to go back through all of the scripts (dozens of them) and change all of the whatever() references to:
>
> whatever('this thing', false, false);
>
> or is there a way to set PHP to ignore this warning by default?

Don't ever ignore error messages: they are there to help you, not to 
annoy. You should configure your development environment to display 
errors, warnings and notices on screen. If you need to remember to 
manually inspect the log, you end up with situations like this.


-- 
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

[toc] | [prev] | [next] | [standalone]


#6894

FromTim Streater <timstreater@greenbee.net>
Date2012-08-28 09:23 +0100
Message-ID<timstreater-2D0EFC.09235728082012@news.individual.net>
In reply to#6890
In article <03b15810-f401-4ad2-a1a4-c4f10f0f311e@googlegroups.com>,
 Jason C <jwcarlton@gmail.com> wrote:

> I have a function like:
> 
> function whatever($a, $b, $c) {
>   if (!$b) $b = "something";
>   if (!$c) $c = "something else";
> 
>   // etc
> }
> 
> Since I'm checking for $b and $c in the function, I call the functions like 
> this when $b and $c are going to be the defaults:
> 
> whatever('this thing');
> 
> But then in the error log, I get these warnings:
> 
> PHP Warning:  Missing argument 2 for q()...
> PHP Warning:  Missing argument 3 for q()...
> 
> Does this mean that I need to go back through all of the scripts (dozens of 
> them) and change all of the whatever() references to:
> 
> whatever('this thing', false, false);
> 
> or is there a way to set PHP to ignore this warning by default?

No, it means you need to do a bit of your own research before posting. 
Y'know, like thinking to yourself: "Ooh, this is a question about 
function arguments, how about I read up in the PHP online 
donkeymentation about function arguments to see if there is something 
useful there that I could apply to my situation?" kind of thing.

Then when that stuff is unclear or there are some salient details that 
are missing from the online manual, *then* you could post here. How 
about that eh?

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web