0

Not sure what I am doing wrong? Never ran into this issue before? But this is also the first function I have written.

<?php
function category($string){
if(preg_match("/(Slimming|Laser|Spa|Massage|Manicure|Pedicure)/i", $string)){  
echo 'Spa';
}
}  // This is function.php

$title = 'Spa';

include 'function.php';
$category = category($title);
$category = mysql_real_escape_string($category);
echo "$category<br>"; // I get Spa
var_dump("$category"); //added for testing, I get string(0) ""

$category is then sent to a mysql insert, the value is blank in the database...

Thanks for the help.

2 Answers 2

2
echo 'Spa';

Should be:

return 'Spa';

and

var_dump("$category");

Should be:

var_dump($category);

Functions should always return a value if you're defining a variable otherwise you're doing nothing.

Sign up to request clarification or add additional context in comments.

1 Comment

No problem, just remember to pick this as the best answer. :)
0

You get the ouput "Spa" when you call category($title), because in the function is echo instead of return.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.