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
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 dch198423@163.com for telling me.

Link to comment
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 dch198423@163.com 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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

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.