The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 07, 2010, 01:01:56 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: How to pass a variable to a function in php  (Read 215 times)
jammaster82
Lifer
*****
Offline Offline

Posts: 666


Thats craigs list for ya


View Profile
« on: December 02, 2009, 10:47:14 PM »

I want to pass $bugpos to the function and
have it manipulate it im already using the 'return'
Is there any way to pass a 'handle' to the variable
so it gets manipulated by the function and passed back?

Code:
<?php
 $myvar2 
'222';
 
$worked dosomethingwiththisvar($myvar2);
 echo 
$myvar2;
 
// it says 222 , not 223 
?>


<?php

function dosomethingwiththisvar($myvar){
 
$myvar++;
 return 
true;
}

?>


Logged

The watched pot, never boils... But if you walk away from it , the soup burns.  What gives?
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 8758



View Profile
« Reply #1 on: December 02, 2009, 11:07:19 PM »

<?php

$count 
100;
for (
$i=0$i<10$i++)
{
	
doSomething($count);
	
echo 
"$count\n";
}

function 
doSomething(&$myVar)
{
	
$myVar++;
}

?>

It's the ampersand in the function declaration before the variable that you want passed by reference that does it. By default all variables (except arrays and objects) are passed by value.
Logged

It's the things you learn after you know it all that really count.
-John Wooden
nutballs
Administrator
Lifer
*****
Online Online

Posts: 5329


DAMN YOU!!!!!!!


View Profile
« Reply #2 on: December 02, 2009, 11:22:27 PM »

thats to pass the reference.
but the other way obviously:

<?php
 $myvar2 
'222';
$myvar2 dosomethingwiththisvar($myvar2);
 echo 
$myvar2//spits out 223
?>

<?php
function dosomethingwiththisvar($myvar){
 
$myvar++;
 return 
$myvar;
}
?>


Logged
jammaster82
Lifer
*****
Offline Offline

Posts: 666


Thats craigs list for ya


View Profile
« Reply #3 on: December 02, 2009, 11:31:31 PM »

 Praise

awesome, thanks!!!
Logged

The watched pot, never boils... But if you walk away from it , the soup burns.  What gives?
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!