Var Declare

I have the following code


//Check if they have enough credit for sms over 160 chars
$numcount = mb_strlen($message);
if ($numcount > 160)
{
$second_number = 2;
$cost = $count * $second_number;
if ($cost > $credits) {
echo $costerror;
exit();
}
}

This keep showing Notice: Undefined variable: cost when it’s not triggering the $costerror but works fine if the $costerror gets triggered.

any way to stop php saying it’s undefined.

where does $count come from?

Presumably the error is coming from after the costerror if block. You’ll have to show us that code. The small snippet you posted doesn’t appear to have anything wrong with it.

Just before the first bit of code that might fill $costerror add:

$costerror='';

That will setup $costerror as an empty string which will be filled by other code if there is an error

towards to the top of the script


$dbnumbers = $db->prepare('SELECT * FROM `numbers` WHERE `sent` = 0 AND `userid` = :id');
$dbnumbers->bindParam(':id', $id, PDO::PARAM_INT);
$dbnumbers->execute();
$count = $dbnumbers->rowCount();

@SpacePhoenix gave you the answer

it’s fixed now :slight_smile: thank you all for your input and help :slight_smile: