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>

 

 

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);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.