0

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.

4
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by furlanut
0

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

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.

0

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

Edited by Stefano Mtangoo: n/a

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.