Jump to content

Calling a Function doesn't return expected results


newtophp59

Recommended Posts

I created a function called converter.  My code doesn't look like it processes anything after the first if . This is what is displayed in browser.

 

Convert a String

 

original string: roses Are red, violets are blue....

converted string: roses are red, violets are blue....

converted string: roses are red, violets are blue....

converted string: roses are red, violets are blue....

 

 

 

<html>

<head>

<title>Create a PHP Function to Convert a String</title>

</head>

<body bgcolor="pink">

<h2>Convert a String</h2>

 

 

 

<?php

$phrase = "roses Are red, violets are blue....";

function converter($arg1, $arg2){

 

if($arg1="lower"){

return strtolower($arg2);

}

elseif ($arg1="upper"){

return strtoupper($arg2);

}

else

/* if($arg1="title")*/{

return ucwords($arg2);

}

}

 

print "original string: ".$phrase."<br />";

 

print "converted string: ".converter("upper",$phrase)."<br />";

 

print "converted string: ".converter("lower",$phrase)."<br />";

 

print "converted string: ".converter("title",$phrase)."<br />";

 

?>

</body>

</html>

 

 

Link to comment
Share on other sites

you would want 2 == to do the check

 

make this:

if($arg1="lower"){
   return strtolower($arg2);
}
   elseif ($arg1="upper"){
   return strtoupper($arg2);
}
   else
   /* if($arg1="title")*/{
   return ucwords($arg2);
}

 

into this:

if($arg1=="lower"){
   return strtolower($arg2);
}
   elseif ($arg1=="upper"){
   return strtoupper($arg2);
}
   else
   /* if($arg1=="title")*/{
   return ucwords($arg2);
}

Link to comment
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.