Jump to content

Problem using/reaching included function?


theloveforphp

Recommended Posts

Hey!

 

I have a small code where I want to check if one variable matches the other when the other is sent through a function. The function, for practical reasons, is included with a separate PHP-file. And I cant seem to apply a variable to the function which processes the other variable. Its really wierd to me.

 

Here is the code for the main PHP-file:

 

<?php

include 'function.php';

$unformatted = "FORMATTING AY ...";

$formatted1 = "formattingay...";

$formatted2 = test_format($unformatted);

if ($formatted1 == $formatted2) {
     echo "Success, formatted!";
} else {
     echo "Fail, unformatted.";
}

?>

 

And here is the code for "function.php":

 

<?php

function test_format($var) {
     strtolower(str_replace(array(" "), array(""), $var));
}

?>

 

Does anybody have a clue to why this simply wont work? Any answers and help is highly appreciated!

Link to comment
https://forums.phpfreaks.com/topic/251599-problem-usingreaching-included-function/
Share on other sites

My english is very poor,so I may not speak very clearly. Your error is function.php.You forget "return"

 

<?php
function test_format($var) {     
return strtolower(str_replace(array(" "),array(""),$var));
}
?>

 

If my answer is wrong ,you send email to [email protected] for telling me.

My english is very poor,so I may not speak very clearly. Your error is function.php.You forget "return"

 

<?php
function test_format($var) {     
return strtolower(str_replace(array(" "),array(""),$var));
}
?>

 

If my answer is wrong ,you send email to [email protected] for telling me.

 

Thats the thing, I dont want the function to echo the result as default. If I use "return" it will automatically echo the formatted value/variable and I want to be able to use it in a if-expression, which I cant if its echoing.

Thats the thing, I dont want the function to echo the result as default. If I use "return" it will automatically echo the formatted value/variable and I want to be able to use it in a if-expression, which I cant if its echoing.

 

return does not produce output.

Thats the thing, I dont want the function to echo the result as default. If I use "return" it will automatically echo the formatted value/variable and I want to be able to use it in a if-expression, which I cant if its echoing.

 

return does not produce output.

 

It doesnt? Hmm, I must have missed something in my testing, it seems to work fine now. Thats odd because I tried the exact same thing yesterday and it echoed the formatted value. Anyhow, I'll mark this as solved and test it a bit further. Thanks to you both for the help!

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.