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

Home » Imported messages » comp.lang.php » Sending nothing when function expects variable
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Sending nothing when function expects variable [message #178961] Tue, 28 August 2012 06:51 Go to next message
jwcarlton is currently offline  jwcarlton
Messages: 76
Registered: December 2010
Karma: 0
Member
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?
Re: Sending nothing when function expects variable [message #178962 is a reply to message #178961] Tue, 28 August 2012 07:08 Go to previous messageGo to next message
Goran is currently offline  Goran
Messages: 38
Registered: January 2011
Karma: 0
Member
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
}
Re: Sending nothing when function expects variable [message #178963 is a reply to message #178961] Tue, 28 August 2012 07:21 Go to previous messageGo to next message
Anders Wegge Keller is currently offline  Anders Wegge Keller
Messages: 30
Registered: May 2012
Karma: 0
Member
Jason C <jwcarlton(at)gmail(dot)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.*
Re: Sending nothing when function expects variable [message #178964 is a reply to message #178961] Tue, 28 August 2012 07:55 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
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
--
Re: Sending nothing when function expects variable [message #178965 is a reply to message #178961] Tue, 28 August 2012 08:23 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article <03b15810-f401-4ad2-a1a4-c4f10f0f311e(at)googlegroups(dot)com>,
Jason C <jwcarlton(at)gmail(dot)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
Re: Sending nothing when function expects variable [message #178967 is a reply to message #178962] Tue, 28 August 2012 13:01 Go to previous message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
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.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: How can i know the propery of a javascript object how can i see what object inclue
Next Topic: Folder Permissions
Goto Forum:
  

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

Current Time: Sun Nov 24 06:04:06 GMT 2024

Total time taken to generate the page: 0.02366 seconds