Lokolo Posted May 15, 2007 Share Posted May 15, 2007 This is my function: function filenamecreator(&$newfilename) { $chars = "abcdefghijkmnopqrstuvwxyz023456789"; srand((double)microtime()*1000000); $i = 0; $newfilename = '' ; while ($i <= 7) { $num = rand() % 33; $tmp = substr($chars, $num, 1); $newfilename = $newfilename . $tmp; $i++; } I can use it in the main program it works however i use it in another function, the code used below $flag = 0; while ($flag = 0) { $flag = 1; filenamecreator($newfilename); $get_otherfilenames = mysql_query("SELECT * FROM logfileinfo"); $num=mysql_numrows($get_otherfilenames); $count = 0; while ( $count < $num ) { $otherfilename = mysql_result($get_otherfilenames, $count, "filename"); if ( $newfilename == $otherfilename ) { $flag = 0; } $count++; } } $add_logfile = mysql_query("INSERT INTO logfileinfo (hotelID, filename) VALUES ('$hotelID','$newfilename')"); However, its not adding $newfilename as anything (i.e. its "") Hope someone can help! Thanks Link to comment https://forums.phpfreaks.com/topic/51568-solved-function-in-a-function-value-not-returning/ Share on other sites More sharing options...
Barand Posted May 15, 2007 Share Posted May 15, 2007 Should be ok in principle. This works (at least with PHP5) <?php function foo(&$bar) { $bar = 'xyz'; } function fubar() { foo($x); echo $x; } fubar(); //--> xyz ?> edit : ok with 4.3 too Link to comment https://forums.phpfreaks.com/topic/51568-solved-function-in-a-function-value-not-returning/#findComment-253970 Share on other sites More sharing options...
btherl Posted May 16, 2007 Share Posted May 16, 2007 You want this while ($flag == 0) Single = is assignment, double == is comparison. Triple === is strict comparison. You could have found this bug by adding some print statements inside your while loop, and seen that they were not executed. Link to comment https://forums.phpfreaks.com/topic/51568-solved-function-in-a-function-value-not-returning/#findComment-254097 Share on other sites More sharing options...
Lokolo Posted May 16, 2007 Author Share Posted May 16, 2007 hm i keep doing this. its not even funny anymore. im use to other programming. i'll try that but not sure if it'll work. (as you can see in the other if statement below i did use == rather than = so i do know some things hehe ) i'll try that out Link to comment https://forums.phpfreaks.com/topic/51568-solved-function-in-a-function-value-not-returning/#findComment-254288 Share on other sites More sharing options...
nikkieijpen Posted May 16, 2007 Share Posted May 16, 2007 Try seperate functions and invoke the filenamecreator function from the first and let de filenamecreator return the filename to the first function which stores the returned filename in a variable: function filenamecreator() { $chars = "abcdefghijkmnopqrstuvwxyz023456789"; srand((double)microtime()*1000000); $i = 0; $newfilename = '' ; while ($i <= 7) { $num = rand() % 33; $tmp = substr($chars, $num, 1); $newfilename = $newfilename . $tmp; $i++; } return $newfilename; } function addLogFile() { $flag = 0; while ($flag == 0) { $flag = 1; $newfilename = filenamecreator(); $get_otherfilenames = mysql_query("SELECT * FROM logfileinfo"); $num=mysql_numrows($get_otherfilenames); $count = 0; while ( $count < $num ) { $otherfilename = mysql_result($get_otherfilenames, $count, "filename"); if ( $newfilename == $otherfilename ) { $flag = 0; } $count++; } } $add_logfile = mysql_query("INSERT INTO logfileinfo (hotelID, filename) VALUES ('$hotelID','$newfilename')"); } Link to comment https://forums.phpfreaks.com/topic/51568-solved-function-in-a-function-value-not-returning/#findComment-254298 Share on other sites More sharing options...
Lokolo Posted May 16, 2007 Author Share Posted May 16, 2007 i had tried to return thing but that didn't work either. it was the == thing. thanks everyone Link to comment https://forums.phpfreaks.com/topic/51568-solved-function-in-a-function-value-not-returning/#findComment-254341 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.