Re: Custom function
| From: | Ashley Sheridan | Date: | Tue, 03 May 2011 09:13:43 +0000 |
| Subject: | Re: Custom function | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-312652@lists.php.net to get a copy of this message | ||
"Andre Polykanine" <andre@oire.org> wrote:
>Hello Ashley,
>
>By the way, the non-last optional parameter can't be missed, am I
>right? In some languages we could write something like:
>function test ($a, $foo=50, $bar=true) {
>// ...
>}
>
>Then call it like this:
>
>$m=test("blah", , false);
>
>meaning
>
>$m=test("blah", 50, false);
>This is impossible in PHP, isn't it?
>Thanks!
>
>--
>With best regards from Ukraine,
>Andre
>Skype: Francophile
>My blog: http://oire.org/menelion (mostly in Russian)
>Twitter: http://twitter.com/m_elensule
>Facebook: http://facebook.com/menelion
>
>------------ Original message ------------
>From: Ashley Sheridan <ash@ashleysheridan.co.uk>
>To: Ron Piggott
>Date created: , 1:37:59 AM
>Subject: [PHP] Custom function
>
>
> On Mon, 2011-05-02 at 18:28 -0400, Ron Piggott wrote:
>
>> On Mon, May 2, 2011 at 3:16 PM, Ron Piggott
><ron.piggott@actsministries.org> wrote:
>>
>>
>> Is it possible to write a function with an optional flag? What
>would the syntax look like?
>>
>> So far I have:
>>
>> function load_advertisement( $web_page_reference ,
>$web_advertising_sizes_reference ) {
>>
>>
>>
>> Hi Ron:
>>
>> I'm not sure what you mean by "optional flag"? Do you mean an
>optional parameter?
>>
>> Richard
>>
>>
>> Correct Richard. Ron
>
>
>function load_advertisement( $web_page_reference ,
>$web_advertising_sizes_reference=default_value)
>
>This sets a default value for a parameter, which you can check for
>within the function itself and use it if it differs from what you set.
>Set it to something that it shouldn't be, like null or something.
>
>Also note that these parameters have to occur after any that don't have
>a default value
>
>--
>Thanks,
>Ash
>http://www.ashleysheridan.co.uk
>
>
>
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php
Yes, it behaves like any built-in php function in that respect, you can't omit a parameter
mid-way in a param list. However, you can give it a dummy value that you can look for and ignore in
your code, but it gets a little more complex then.
I did forget to mention that a function also has a special func_get_args() method that can be used
to access parameters sent to the function that weren't in the function declaration.
Thanks
Ash
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.