php: performing math inside of an "echo". also passing variable name to a function

Jakob Jingleheimer picture Jakob Jingleheimer · Sep 15, 2011 · Viewed 13.4k times · Source

I'm building a table (tho not using <table>) and populating it with data from mysql. I have the following code to build the "table":

$NYC = // data from mysql, working fine
$NYC_min = 300;
switch($cou_mun){
    case "New York":
        $NYC++;
        break;
    //etc
}

function cityMinMet($city){
    $city_min= "$city" . "_min";
    if($city>$city_min){return "yes";}
    else{return "no";};
}

echo "<h3>Table 6: Recruitment by target area</h3>";
echo "<ul>\n<li><span>Target Area</span><span>Number Recruited</span><span>Amount under minimum</span><span>Minimum</span><span>Minimum met?</span></li>";
echo "<li><span>12 Jurisdictions with Highest AIDS Prevalance</span><span></span><span></span><span></span><span></span></li>";
echo "<li><span>New York</span><span>$NYC</span><span>" . $NYC_min-$NYC . "</span><span>$NYC_min</span><span>"; cityMinMet('$NYC'); echo "</span></li>";

I encounter a problem with " . $NYC_min-$NYC . ": it breaks the row (the row gets interpreted as ending just before " . $NYC_min-$NYC . ". However, if I have <span>$NYC_min-$NYC</span> (not as an additional component of the echo), the value of the cell is printed as 300-500 instead of 200.

Also I'm not sure I've setup my function properly (but this is not breaking the table/row). From cityMinMet('$NYC') I want the literal string $NYC (and not its value) sent to the function. Inside the function I need to append _min to $NYC and then call $NYC_min and it return with its value.

EDIT: I changed the order of $NYC and $NYC_min in the equation.

Answer

Adam Hopkinson picture Adam Hopkinson · Sep 15, 2011

Wrap it in parenthesis:

" . ($NYC - $NYC_min) . "