Simple function not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jimandy
Forum Newbie
Posts: 22
Joined: Sat Sep 15, 2007 10:56 am
Location: Alabama, USA

Simple function not working

Post 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]
jeffery
Forum Contributor
Posts: 105
Joined: Mon Apr 03, 2006 3:13 am
Location: Melbourne, Australia
Contact:

Post 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";
?> 
jimandy
Forum Newbie
Posts: 22
Joined: Sat Sep 15, 2007 10:56 am
Location: Alabama, USA

Post 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]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

There is no parse error in that code.
jimandy
Forum Newbie
Posts: 22
Joined: Sat Sep 15, 2007 10:56 am
Location: Alabama, USA

Post 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.
Post Reply