The Artima Developer Community
Sponsored Link

Python Buzz Forum
PHP default function arguments - not what you expect

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Phillip Pearson

Posts: 1083
Nickname: myelin
Registered: Aug, 2003

Phillip Pearson is a Python hacker from New Zealand
PHP default function arguments - not what you expect Posted: Oct 19, 2005 7:55 PM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Phillip Pearson.
Original Post: PHP default function arguments - not what you expect
Feed Title: Second p0st
Feed URL: http://www.myelin.co.nz/post/rss.xml
Feed Description: Tech notes and web hackery from the guy that brought you bzero, Python Community Server, the Blogging Ecosystem and the Internet Topic Exchange
Latest Python Buzz Posts
Latest Python Buzz Posts by Phillip Pearson
Latest Posts From Second p0st

Advertisement

Here's another thing you might want to watch out for in PHP (if you're used to Python or UserTalk, and probably most other languages). It doesn't seem that it lets you address arguments by name, only by position. That is, it lets you address them by name but ignores the name and uses the position instead.

Example:

error_reporting(E_STRICT | E_ALL);

function test_default_args($a = 1, $b = 2, $c = 3)
{
echo "a $a, b $b, c $c<br>";
}

test_default_args($b = 123);

This prints:

a 123, b 2, c 3

So even though you told it $b = 123, it assigned that to $a instead. I guess it's just evaluating the expression ($b = 123), which creates a local var called $b, assigns 123 to it, and returns 123.

It would be nice if there was a warning for this, like C's possibly incorrect assignment. It doesn't look like PHP checks for that case - assignments not inside an explicit comparison like (($a = foo()) != NULL).

Comment

Read: PHP default function arguments - not what you expect

Topic: Bugger Previous Topic   Next Topic Topic: The Poignant Planet Python Puzzle!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use