N1CK3RS0N Posted April 11, 2009 Share Posted April 11, 2009 Hello, I was wondering if its possible to pass a variable through a function? Something like this: <?php function cows($noise) { if ($noise == moo) { $say = "Hi Mr. Cow"; } else { $say = "Your no Cow fool!"; } } cows('moo'); echo $say; ?> The 'Echo $say;' does nothing. Link to comment https://forums.phpfreaks.com/topic/153572-solved-passing-a-variable-through-a-function/ Share on other sites More sharing options...
schilly Posted April 11, 2009 Share Posted April 11, 2009 you need to pass by reference. a variable created inside a function is localized to that function. http://php.net/language.references.pass Link to comment https://forums.phpfreaks.com/topic/153572-solved-passing-a-variable-through-a-function/#findComment-806979 Share on other sites More sharing options...
N1CK3RS0N Posted April 11, 2009 Author Share Posted April 11, 2009 you need to pass by reference. a variable created inside a function is localized to that function. http://php.net/language.references.pass Hmm. That confuses me. Is there no other way? Don't think that will work for what I'm trying to do. Link to comment https://forums.phpfreaks.com/topic/153572-solved-passing-a-variable-through-a-function/#findComment-806982 Share on other sites More sharing options...
schilly Posted April 11, 2009 Share Posted April 11, 2009 or declare a global variable. what are you trying to do? Link to comment https://forums.phpfreaks.com/topic/153572-solved-passing-a-variable-through-a-function/#findComment-806986 Share on other sites More sharing options...
Goldeneye Posted April 11, 2009 Share Posted April 11, 2009 <?php function cows($noise) { global $say; if ($noise == moo) { $say = "Hi Mr. Cow"; } else { $say = "Your no Cow fool!"; } } cows('moo'); echo $say; ?> That would work, but serves as poor-programming. Link to comment https://forums.phpfreaks.com/topic/153572-solved-passing-a-variable-through-a-function/#findComment-806987 Share on other sites More sharing options...
N1CK3RS0N Posted April 11, 2009 Author Share Posted April 11, 2009 or declare a global variable. what are you trying to do? I have a number of things going to be using a function. Its checking file permissions. If every file permission meets requirements then the submit button on the form works, otherwise it is disabled. <?php $pass = "true"; function check_perms($file, $perm) { if (substr(decoct(fileperms("$file")),2) == $perm) { return "<div class=\"container\" style=\"color: #008000; font-weight:bold;\">Pass</div>"; } else { $pass = "false"; return "<div class=\"container\" style=\"color: #D00000; font-weight:bold;\">Fail</div>"; } } $backups = check_perms('../backups/', '777'); $downloads_uploads = check_perms('../downloads/uploads/', '777'); $images_uploads = check_perms('../images/uploads/', '777'); $images_banners = check_perms('../images/banners/', '777'); $include_config = check_perms('../includes/config.php', '666'); $languages = check_perms('../languages/', '777'); $modules = check_perms('../modules/', '777'); $templates = check_perms('../templates/', '777'); if ($pass == 'true') { $input_button = "<input type=\"submit\" id=\"submit\" value=\"{$LANG.submit_button}"\ name=\"submit\" />"; } else { $input_button = "<input type=\"submit\" id=\"submit\" value=\"{$LANG.submit_button}"\ name=\"submit\" disabled=\"disabled\" />"; } ?> Link to comment https://forums.phpfreaks.com/topic/153572-solved-passing-a-variable-through-a-function/#findComment-806989 Share on other sites More sharing options...
schilly Posted April 11, 2009 Share Posted April 11, 2009 <?php $pass = "true"; function check_perms($file, $perm,&$pass) { if (substr(decoct(fileperms("$file")),2) == $perm) { return "<div class=\"container\" style=\"color: #008000; font-weight:bold;\">Pass</div>"; } else { $pass = "false"; return "<div class=\"container\" style=\"color: #D00000; font-weight:bold;\">Fail</div>"; } } $backups = check_perms('../backups/', '777',$pass); $downloads_uploads = check_perms('../downloads/uploads/', '777'$pass); $images_uploads = check_perms('../images/uploads/', '777',$pass); $images_banners = check_perms('../images/banners/', '777',$pass); $include_config = check_perms('../includes/config.php', '666',$pass); $languages = check_perms('../languages/', '777',$pass); $modules = check_perms('../modules/', '777',$pass); $templates = check_perms('../templates/', '777',$pass); if ($pass == 'true') { $input_button = "<input type=\"submit\" id=\"submit\" value=\"{$LANG.submit_button}" name="submit\" />"; } else { $input_button = "<input type=\"submit\" id=\"submit\" value=\"{$LANG.submit_button}" name="submit\" disabled=\"disabled\" />"; } ?> try that Link to comment https://forums.phpfreaks.com/topic/153572-solved-passing-a-variable-through-a-function/#findComment-806992 Share on other sites More sharing options...
N1CK3RS0N Posted April 11, 2009 Author Share Posted April 11, 2009 Awesome Schilly I owe ya 1 And I typed that up in 2 seconds Goldeneye, I realize I missed the "" in the if condition. Link to comment https://forums.phpfreaks.com/topic/153572-solved-passing-a-variable-through-a-function/#findComment-807000 Share on other sites More sharing options...
schilly Posted April 11, 2009 Share Posted April 11, 2009 np And I typed that up in 2 seconds Goldeneye, I realize I missed the "" in the if condition. i believe Goldeneye meant declaring a global. Link to comment https://forums.phpfreaks.com/topic/153572-solved-passing-a-variable-through-a-function/#findComment-807004 Share on other sites More sharing options...
ch1326 Posted April 11, 2009 Share Posted April 11, 2009 This might be help. <?php function cows($noise) { if ($noise == moo) { echo $say = "Hi Mr. Cow"; } else { echo $say = "Your no Cow fool!"; } } cows('moo'); ?> Link to comment https://forums.phpfreaks.com/topic/153572-solved-passing-a-variable-through-a-function/#findComment-807063 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.