Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.