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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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\" />"; } ?> Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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'); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.