The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 16, 2019, 09:30:57 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: How to pass a variable to a function in php  (Read 1592 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
*****
Offline Offline

Posts: 10096



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 is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


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

I could eat a bowl of Alphabet Soup and shit a better argument than that.
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!