Alright so I've been using php for years but it's always been as a newbie and I've never really progressed beyond the basics. About the only thing I know about php inside and out is DOM functions which doesn't exactly help me here.
Anyways I'm trying to construct a string using a function and am meeting with limited success. Here's the basics:
function prepare_tags($tag,$is_text,$content,$has_attributes,$attributes) {
// Quick and easy if is_text is true otherwise find attributes
if ($is_text) {
echo $content;
}
else {
// Start the tag
echo '<'. $tag;
// Check for attributes
if ($has_attributes) {
foreach ($attributes as $attribute) {
echo ' '. $attribute->name .'="'. $attribute->value .'"';
}
}
// End the tag
echo '>'. $content .'</'. $tag .'>';
}
}
When I call the function like this:
$clean_tags = prepare_tags($tag,$is_text,$content,$has_attributes,$attributes);
It simply prints out the string instead of allowing me to take the $clean_tags variable and apply it somewere else. I've gone through the php manual quite extensively and have searched google but I'm a little tired after writing this script.
I'm writing something to create an rss feed by the way.
Posts
First, the only DOM methods PHP would have would be to manipulate XML. If you knew those, you would know what you're doing is very wrong. This leads me to suspect when you speak of DOM methods, you are talking about Javascript.
Second, it looks like you're just trying to escape some text. I'm not very familiar with PHP, but it must have a function for doing that built into the base library.
Third, confusion over why it's printing out the value. The function you gave uses echo, which prints directly to the output. You need to learn about what a function is, and how to use a return value. You could try some programming tutorials, or could buy one of those "* for dummies" books.
Somewhere inside your function it should say:
return $outputstring;
but it doesn't, so your function doesn't pass back any variables.
Just so I know what you are trying to do, this function should take an XHTML tag and it's attributes as arguments, construct the complete tag as escaped XHTML (that is to say, you want the tag to be printed to the screen, not part of the page) contained within a string, then pass that string back to the rest of the script. Correct?
*Note there are a few things wrong with this script aside from what I'm asking about here. I haven't completely finished it yet but for the moment I want to focus on this particular problem.
You've got a good grasp of roughly where comments should go and how to make blocks out of the code. However, comments should explain "why" and not "what". Whenever you write a comment like "Initiate connection with site and save copy", it means you should separate that block out into a function.
For example:
Could be changed to:
You should especially apply this to that giant foreach at the end of the code. Whenever you get that level of nested loops and ifs, it's an alarm bell to separate parts of your code. When you separate your code into small blocks of logically organized code, it becomes possible to verify correctness merely by sight. There is no way to check that giant loop is correct short of manually tracing it.
On lines 210 and 213, it seems you know how to use booleans. So, why do you set $found to a string on lines 178 and 248?