I have been able to write functions but cannot get them to work. Many sites show you how to write them but not how to call them in your web programming.
Can someone explain why this call does not work for me ?
the function is:

<? php

function fix_name($name)
{
$name= ucwords(strtolower($name));
return $name;
}

?>

If I do this:

<? php

require_once "fix_name.php";
echo ("costa playa");

?>

IT does not work
But, if I enter this

<?php

echo fix_name("costa playa");

function fix_name($name)
{
$name= ucwords(strtolower($name));
return $name;
}
?>

IT works.
It is very basic,I know but I am struggling.

The third block of code is OK, that is how you do it. Of course you can also have functions in a separate file like in your second block example in the fix_name.php file. Only the line 4 of the second block should be:

echo fix_name("costa playa");
Member Avatar for diafol

As broj1 states include files can be created to hold functions - this is in fact an useful way of organising your code, but including a file is very different from calling a function directly.

<?php
 
require_once "fix_name.php";
echo fix_name("costa playa");
 
?>

should work fine if the include file contains the function called fix_name.

However, you wouldn't normally create an include file for just one simple function - so the name seems a little out of place.

also "it does not work" is not better way to explain problem. Say what and how exactly it does not work!

Having corrected the call to 'echo fix_name("costa playa") I get the error
! ) Fatal error: Call to undefined function fix_name() in C:\wamp\www\WSRU\costa playa.php on line 15
When I change the function to contain 'ucfirst' instead of 'ucwords' the call is correctly displayed and the 3rd code is always OK.
Perhaps it is something in Wampserver ?
Thanks, I will call the thread resolved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.