I looked at the example in the book and it looks like they have an error.
In the second call to function raise, they have the parameters reversed.
They have "4" where the salary variable should go and the salary variable
where "4" should go. (If you look at the function definition, you can see
that the first value should be the salary and the second should be the
percent).
The reason you were getting the error was because the example was trying to
pass "4" by reference, and it makes perfect sense why you can't do that.
Adam Lang
Systems Engineer
Rutgers Casualty Insurance Company
http://www.rutgersinsurance.com
----- Original Message -----
From: "matt" <matt@i...>
To: "professional php" <pro_php@p...>
Sent: Sunday, May 06, 2001 4:41 AM
Subject: [pro_php] passing argumenst by reference
> Hello!
>
> Ch. 7, page 134 from Professional PHP, I can't get the code to work
> right. It seems an error on the books part, but I could be wrong.
>
> Here's the code from the book:
>
> <?php
>
> function raise(&$salary, $percent)
> {
> return ($salary+=$salary*percent/100);
> }
>
> $sal=50000;
>
> echo("Pre-raise salary: $sal<br>\n");
>
> raise (4, $sal);
> echo ("Post-raise salary: $sal<br>\n");
>
> ?>
>
> This produces the error: Only variables can be passed by reference.
>
> If I establish a value for hte paramenter $percent, I don't get the change
> to the calling statements variable.
>
> Changing:
>
> function raise(&$salary, $percent=4)
>
> and
>
> raise ($sal);
>
> Produces:
>
> Pre-raise salary: 50000
> Post-raise salary: 50000
>
> Where both the text in the book and my understanding of passing values by
> references indicates that the product should be:
>
> Pre-raise salary: 50000
> Post-raise salary: 52000
>
> What am I missing? Any help, much appreciated!
>
> Matt
>
>