Wrox Programmer Forums
|
BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6
This is the forum to discuss the Wrox book Beginning PHP, Apache, MySQLWeb Development by Michael K. Glass, Yann Le Scouarnec, Elizabeth Naramore, Gary Mailer, Jeremy Stolz, Jason Gerner; ISBN: 9780764557446
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 2nd, 2009, 01:59 PM
Authorized User
 
Join Date: Dec 2007
Posts: 65
Thanks: 9
Thanked 2 Times in 2 Posts
Send a message via AIM to sandeepgreaternoida Send a message via MSN to sandeepgreaternoida Send a message via Yahoo to sandeepgreaternoida
Arrow where this function missing !

could anybody tell me where the following script missing. i am not able to execute it on the browser...i know this is simple but ..i am not able to get through it.

/*

<?php
function addFive($num)
{$num +=5 ;}
$orignum=190;
addFive($orignum);
echo $orignum;
?>


*/
 
Old April 2nd, 2009, 05:57 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Hi Sandeep,

If you mean, why does the function not work, it is because the $num parameter is being passed by value, which is the default for basic types such as ints, booleans etc. In other words, a copy of the variable data is passed to addFive, not the actual bit of memory the variable points to, so the original $orignum value is never changed.
You need to change the function to pass it by reference, so that $num references the actual variable data - you can think of $num being an alias name for $orignum. In PHP you do this by putting & in front of it as so:

PHP Code:
<?php
function addFive(&$num)
{
  
$num +=5;
}

$orignum=190;
addFive($orignum);
echo 
$orignum;
?>
This will set value of orignum to 195 as you want.

You can read more about reference variables on the PHP site.

HTH
Phil
The Following User Says Thank You to philip_cole For This Useful Post:
sandeepgreaternoida (April 3rd, 2009)
 
Old April 3rd, 2009, 03:35 AM
Authorized User
 
Join Date: Dec 2007
Posts: 65
Thanks: 9
Thanked 2 Times in 2 Posts
Send a message via AIM to sandeepgreaternoida Send a message via MSN to sandeepgreaternoida Send a message via Yahoo to sandeepgreaternoida
Thumbs up thanx for your answer

thanx,when i tried in another way by puttinng "&" at addFive(&$orignum); then, It also work, IS "& "ALWAYS USED IN passaing an argument to the function by reference ?
 
Old April 3rd, 2009, 04:53 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Yes that works too, but note that doing it this way is not recommended and creates a warning in PHP5+. I personally prefer to keep all the workings of the function within itself anyway.

When passing values to functions, & does always mean pass by reference. However it is also used as an AND operator in bitwise calculations (and,or,xor etc). See http://uk2.php.net/manual/en/languag...rs.bitwise.php
It is normally fairly obvious what type of & is being used if you are looking at some code.

Phil





Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there some ':, missing ??? saban SQL Server ASP 1 May 28th, 2007 06:32 AM
send variable in function to another function schoolBoy Javascript How-To 6 March 3rd, 2007 09:16 AM
VBScript find missing number function?? mat41 Classic ASP Professional 6 February 12th, 2007 07:07 PM
How to call javascript function from VB function vinod_yadav1919 VB How-To 0 February 13th, 2006 06:03 AM
retreive function/Line from macro or function? MikoMax J2EE 0 April 1st, 2004 04:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.