Here’s the IF statement. I want to access the timeStampCleaned variable later on.
if ($xmlRatesTime = '') {
$timeStampCleaned = date('j F Y H:i', $ratesTimeStamp); // Convert unix timestamp into date format
} else {
// ...
}
Like so:
if(empty($ratesTimeStamp)) {
$newXML = simplexml_load_file('cache/rates.xml');
$child = $newXML->addChild('currency');
$child->addAttribute('id', ''.$to.'');
$child->addChild('title', $toTitle);
$child->addChild('loc', $toLocation);
$child->addChild('rate', $finalRate);
$child->addChild('timestamp', $timeStamp);
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($newXML->asXML());
$newXMLdomCleaned = $dom->saveXML();
file_put_contents('cache/rates.xml', $newXMLdomCleaned);
}
But I’m getting the error:
Notice: Undefined variable: timeStampCleaned in ...file... on line 208
From what I understand, accessing variables within if statements is fine. So I have no idea why this isn’t working!?
Thanks
It’s probably because you are not declaring the variable in the
elsepart of your statement. If unless$xmlRatesTimeis equal to'',$timeStampCleanedwill not be created. Try adding a declaration in the “else”, for example:Although, generally speaking, I find this to be bad programming practice. I would recommending declaring the variable before the if statement altogether, as in:
As a side note, did you mean
$xmlRatesTime==''(two equal signs)?