Page 1 of 1

Simple function not working

Posted: Thu Oct 18, 2007 8:41 pm
by jimandy
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am trying to build a larger function but find I am failing miserably at the basics of creating a simple one.

The core function is used to replace some text in a string. Although the replace function inside checks out when used independently, the created function simply returns nothing.

Code: Select all

<?
@$original_text="AB";
@$newtext="";
//
 function translate($text) {
 $newtext= str_replace('A','C',$text);
  return $newtext;}
//
translate($original_text);           //call the function
 echo $newtext . '<br>';             //display translated text
 echo "should return CB";
?>
Thanks in advance for your help.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Oct 18, 2007 9:40 pm
by jeffery
try

Code: Select all

<?
$original_text="AB";
$newtext="";
//
 function translate($text) {
 $newtext= str_replace('A','C',$text);
  return $newtext;}
//
$newtext = translate($original_text);           //call the function
 echo $newtext . '<br>';             //display translated text
 echo "should return CB";
?> 

Posted: Thu Oct 18, 2007 10:31 pm
by jimandy
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Saved as testscrubber2.php on local MAMP server.

Throws the following error...

Parse error: syntax error, unexpected T_STRING in /Applications/MAMP/htdocs/testscrubber2.php on line 5

Code: Select all

<? 
$original_text="AB"; 
$newtext=""; 
// 
 function translate($text) { 
 $newtext= str_replace('A','C',$text); 
  return $newtext;} 
// 
$newtext = translate($original_text);           //call the function 
 echo $newtext . '<br>';             //display translated text 
 echo "should return CB"; 
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Oct 18, 2007 10:33 pm
by John Cartwright
There is no parse error in that code.

Posted: Thu Oct 18, 2007 10:54 pm
by jimandy
Well, yes and no, Jcart. I copied straight from the post into BBedit saved and it threw the error. But then I used the "convert to ascii" and "zap gremlins" functions and apparently there were gremlins that were invisible and now the code runs great. Whatever was fooling the parser was invisible for sure.

Thanks to you both.