Jump to content

Functions?


chris57828

Recommended Posts

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();








 

 

 

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/232766-functions/
Share on other sites

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 );

Link to comment
https://forums.phpfreaks.com/topic/232766-functions/#findComment-1197262
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.