473,231 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

php5 functions definitions are not working according to spec (?)

Program (PHP5):

<?php

$a='x';
$b=5;
foo($a,$b);
echo "a=$a b=$b";

function foo($x,$y){
$x='y';
$y=56;
}

?>

According to the php5 manual, function arguments are, by default, passed by
reference. Therefore, the echo should display:

a=y b=56

but, instead it displays:

a=x b=5

I can get everything to work by specifying the following function
definition:

function foo(&$x,&$y)

but I would think that if "by reference" is the default for arguments, then
"by reference" should be the default for a function parameters. What's
wrong with my logic?

TIA,

Larry Woods
Aug 6 '05 #1
3 2733
On Sat, 6 Aug 2005 11:36:46 -0700, "lwoods" <la***@lwoods.com> wrote:
According to the php5 manual, function arguments are, by default, passed by
reference. Therefore, the echo should display:


Which bit of the manual?

As far as I'm aware, PHP 5 still passes everything by value by default.

The change in PHP 5 is that object parameters are now in fact handles, which
are still passed by value, but when dereferenced stay referring to the original
object; as opposed to PHP 4 where it'd copy the whole object instead unless you
explicitly used references.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Aug 6 '05 #2
I dug into the manual further and the reference to "by reference" is talking
about "default values"

Here is the statement:

Note: As of PHP 5, default values may be passed by reference.


Sorry.

Larry

"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:j2********************************@4ax.com...
On Sat, 6 Aug 2005 11:36:46 -0700, "lwoods" <la***@lwoods.com> wrote:
According to the php5 manual, function arguments are, by default, passed byreference. Therefore, the echo should display:
Which bit of the manual?

As far as I'm aware, PHP 5 still passes everything by value by default.

The change in PHP 5 is that object parameters are now in fact handles,

which are still passed by value, but when dereferenced stay referring to the original object; as opposed to PHP 4 where it'd copy the whole object instead unless you explicitly used references.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool


Aug 6 '05 #3
On Sat, 06 Aug 2005 12:07:35 -0700, lwoods wrote:
I dug into the manual further and the reference to "by reference" is
talking about "default values"

Here is the statement:

Note: As of PHP 5, default values may be passed by reference.

The relevant part of the same manual page is this:

" Making arguments be passed by reference

By default, function arguments are passed by value (so that if you change
the value of the argument within the function, it does not get changed
outside of the function). If you wish to allow a function to modify its
arguments, you must pass them by reference.

If you want an argument to a function to always be passed by reference,
you can prepend an ampersand (&) to the argument name in the function
definition:

Example 17-5. Passing function parameters by reference
<?php
function add_some_extra(&$string)
{
$string .= 'and something extra.';
}
$str = 'This is a string, ';
add_some_extra($str);
echo $str; // outputs 'This is a string, and something extra.'
?>"

Default arguments are described below that. Default arguments are
what is coded as

my @stuff=(@_,"Default Argument");

in another scripting language.

--
http://www.mgogala.com

Aug 6 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mxsmanic | last post by:
The require() I'm using in a PHP script has stopped working after I moved from PHP4 and Apache 1.3.x to PHP5 and Apache 2.x. Now I get messages like this: Warning:...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
1
by: PTS | last post by:
I am working on a program that will calculate some numbers. As an example, I will say student average test score and print out the grade but my question is how do I put AVERAGE and test score and...
2
by: mananvyas | last post by:
Hi, I am working on PHP5 and all the xmldom related extensions are installed and on properly. I am trying to execute following code but some how not working. $doc = new DomDocument();...
2
abdoelmasry
by: abdoelmasry | last post by:
Hi all i have problem while using sessions i was working with php4 on apache with This Functions: session_start(); session_register("username"); $username="abdoelmasry"; it was working...
0
by: Erwin Moller | last post by:
Hi group, I found something strange in PHP5.2.4 (on IIS7/Vista). I am working on an app that has been running just fine under heavy load for over a year at some custumer of mine. (they have...
8
by: FFMG | last post by:
Hi, I am slowly moving my code to php5. But I would like to make it backward compatible in case something bad happens, (and to make sure I understand what the changes are). The way the...
30
by: Logos | last post by:
I have what may be a bug, or may be a misunderstanding on how pass by reference and class inheritance works in PHP. Since I'm relatively new to PHP, I'm hoping for a little outside help to shed...
1
by: Gilles Ganault | last post by:
Hello I'd like to play with MemcacheD and see how it improves read access to data, but I'm a bit lost at how to get from a working Apache2 + PHP5 + MySQL5 server on FreeBSD 6.3. I've never...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.