Forum Moderators: coopster

Message Too Old, No Replies

php 'if' scope

         

sneaks

8:12 pm on Aug 31, 2005 (gmt 0)

10+ Year Member



question:
is the variable - $TimeStamp (initiated in the first if statement) outside the scope of the second 'if' statement?

if so why and how do i go about accessing it?


if ($node->nodeName == "Date") {
$TimeStamp = trim($elem->nodeValue);
print '<div class="containerNewsItem"><div class="indexDate">';
convertTimeStamp($TimeStamp);
print '</div>';
}
if ($node->nodeName == "Title") {
print '<div class="indexTitle">' . '<a href="xnView.php?release=' . $TimeStamp . '">' . trim($elem->nodeValue). '</a></div></div>';
}

dreamcatcher

8:14 pm on Aug 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi sneaks,

No, there are no scope issues with if statements unless they are in a function. Why, do you have a problem?

dc

sneaks

8:25 pm on Aug 31, 2005 (gmt 0)

10+ Year Member



yep, $TimeStamp doesnt want to come across to the second 'if' statement, its really confusing me but i wanted to double check php scope...

$TimeStamp goes to a function however, the function only uses it as a variable and makes no changes to its value whatsoever.

wierd...

dreamcatcher

8:36 pm on Aug 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, the way you have it, only one of the if statements can return true at any one time. No reason for the variable not to populate if both if statements are working as they should.

dc

caspita

8:48 pm on Aug 31, 2005 (gmt 0)

10+ Year Member



From where I see it, when the condition goes thru the second "if" TimeStamp won't have any value, it won't exist at all because it gets created (initialized) only in the first "if".

Is that the issue you are facing?

CS.

sneaks

9:02 pm on Aug 31, 2005 (gmt 0)

10+ Year Member



i see your point... and i have resolved it using a slightly different method but i still want to work this through. here is the modified method the convertTimeStamp function is the same:


if ($newsIndex->nodeType == 1 && $newsIndex->nodeName == "Item") {
print '<div class="newsItem">';
foreach ($newsIndex->childNodes as $item) {
if ($item->nodeType == 1 && $item->nodeName == "Date")
{
$TimeStamp = $item->textContent;
print '<span class="newsDate">';
convertTimeStamp($TimeStamp);
print '</span><br />';
}
if ($item->nodeType == 1 && $item->nodeName == "Title") print '<span class="newsTitle">' . '<a href="xmlnews.php?release=' . $TimeStamp . '">' . $item->textContent . '</a></span>';
}
print '</div>';
}

sneaks

9:59 pm on Aug 31, 2005 (gmt 0)

10+ Year Member



figured it out...
it was function scope... trying to declare global from the start rather than in the function itself...

damn fleas...

the resolved code if you're interested:
( btw coding critic is appreciated, coming from an design & actionscript background but am really diggin' php & xml)

----------------
<?php
include 'xnHeader.php';

$xnFile=$_GET[release];
$dom = new DomDocument();
$dom->load("news/nr_$xnFile.xml") or die("Could not load news/nr_$xnFile.xml");
$root = $dom->documentElement;

print '<div class="containerViewNews">';
process_children($root);
print '</div>';

function process_children($node)
{
$children = $node->childNodes;

foreach ($children as $elem) {
if ($elem->nodeType) { // == XML_TEXT_NODE
if (strlen(trim($elem->nodeValue))) {

if ($elem->nodeName == "Date") {
print '<div class="viewDate">';
$TimeStamp = trim($elem->nodeValue);
convertTimeStamp($TimeStamp);
print '</div>';
}
if ($elem->nodeName == "Title") {
print '<div class="viewTitle">';
print $elem->nodeValue;
print '</div>';
}
if ($elem->nodeName == "Content") {
print '<div class="viewContent">';
print $elem->nodeValue;
print '</div>';
}
}
} else if ($elem->nodeType == XML_ELEMENT_NODE) {
process_children($elem);
}
}
}

function convertTimeStamp($TimeStamp)
{
trim($TimeStamp);
preg_match("#^([0-9]{4})([0-9]{2})([0-9]{2})#",$TimeStamp,$Match);
list(,$Year,$Month,$Day) = $Match;
print date("l, F j Y.",mktime(0,0,0,$Month,$Day,$Year));

}

include 'footerDom.php';
?>