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 开发者_StackOverflow社区"$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.
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.
You get the ouput "Spa" when you call category($title)
, because in the function is echo
instead of return
.
精彩评论