chris57828 Posted April 5, 2011 Share Posted April 5, 2011 Can someone tell me by looking at the following code why function pcust() does not work, I am new to programming? :'( $firstname = "chris"; $surname = "reynolds"; $address1 = "12 birch end"; $town = "Wallington"; $county = "surrey"; $postcode = "wh20 3bg"; $telephone = "01372 854785"; $cust_details = fix_cust($firstname, $surname, $address1,$town, $county, $postcode, $telephone ); function fix_cust($n1, $n2, $n3, $n4, $n5, $n6, $n7) { $n1 = ucfirst(strtolower($n1)); $n2 = ucfirst(strtolower($n2)); $n3 = ucwords(strtolower($n3)); $n4 = ucfirst(strtolower($n4)); $n5 = ucfirst(strtolower($n5)); $n6 = ucfirst(strtoupper($n6)); $n7 = ucfirst(strtolower($n7)); return array($n1, $n2, $n3, $n4, $n5, $n6, $n7); } echo $cust_details[0] . " " . $cust_details[1]; echo "<br />"; echo $cust_details[2]; echo "<br />"; echo $cust_details[3]; echo "<br />"; echo $cust_details[4]; echo "<br />"; echo $cust_details[5]; echo "<br />"; echo $cust_details[6]; echo "<br />"; function pcust(){ $length = count($cust_details ); for ($i = 0; $i < $length; $i++) { echo $cust_details[$i]; echo "<br />"; } } pcust(); Quote Link to comment https://forums.phpfreaks.com/topic/232766-functions/ Share on other sites More sharing options...
coupe-r Posted April 5, 2011 Share Posted April 5, 2011 Try passing $cust_details into your function arg. Quote Link to comment https://forums.phpfreaks.com/topic/232766-functions/#findComment-1197254 Share on other sites More sharing options...
chris57828 Posted April 5, 2011 Author Share Posted April 5, 2011 Thanks babe..................it works! YIPPEE Quote Link to comment https://forums.phpfreaks.com/topic/232766-functions/#findComment-1197259 Share on other sites More sharing options...
Pikachu2000 Posted April 5, 2011 Share Posted April 5, 2011 I'd say it's due to variable scope. You need to pass the variable $cust_details to the function as an argument, as it won't be available in the function's scope. You should also use return to get values from a function rather than echoing them. In this case, however I'd say it would be easier not to deal with a function at all; simply implode the array and echo it. echo implode( '<br />', $cust_details ); Quote Link to comment https://forums.phpfreaks.com/topic/232766-functions/#findComment-1197262 Share on other sites More sharing options...
Maq Posted April 5, 2011 Share Posted April 5, 2011 chris, please use in the future. Quote Link to comment https://forums.phpfreaks.com/topic/232766-functions/#findComment-1197266 Share on other sites More sharing options...
chris57828 Posted April 5, 2011 Author Share Posted April 5, 2011 I'm having trouble ascertaining the difference between 'echo' and 'return'? Quote Link to comment https://forums.phpfreaks.com/topic/232766-functions/#findComment-1197271 Share on other sites More sharing options...
chris57828 Posted April 5, 2011 Author Share Posted April 5, 2011 chris, please use in the future. Sorry, will do! Quote Link to comment https://forums.phpfreaks.com/topic/232766-functions/#findComment-1197272 Share on other sites More sharing options...
cjrose77 Posted April 5, 2011 Share Posted April 5, 2011 echo "whatever"; sends the quoted text as HTML to the web browser return(); exits a function and returns the information in the parentheses Quote Link to comment https://forums.phpfreaks.com/topic/232766-functions/#findComment-1197277 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.