Quote from: Soshite at Nov 07, 2007, 08:41 PM
<?php
$css = isset($css) ? $css : 'tab3';
$link = preg_replace('/QU/','?',$link);
$link = preg_replace('/EQ/','=',$link);
$link = preg_replace('/AM/','&',$link);
$data = preg_replace('/SB/','<b>',$data);
$data = preg_replace('/EB/','</b>',$data);
if (isset($extra)) {
return '<td class="'.$css.'" width="'.$width.'" colspan="'.$colspan.'" rowspan="'.$rowspan.'">'.$data.'</td>';
} else {
if (isset($link)) {
if (isset($pop)) {
return '<td class="'.$css.'"><a href="'.$link.'" target="_blank">'.$text.'</a></td>';
} else {
return '<td class="'.$css.'">'.$data.'</td>';
}
} else {
return '<td class="'.$css.'">'.$data.'</td>';
}
}
?>
For &pop, I always know it will use that code is that is there, and if it isn’t there, the snippet won’t even "see" that code, and will check to see if &link exists, either doing that IF or going to the ELSE at the end.
Thus, in this case, is the ELSE statement even needed, or will I break the script for some odd reason doing that?
Soshite,
As I read it (and if I’m right about which "else" you mean), if you take out that else, if $link is set and $pop is not it will return nothing. Notice that I changed your indenting so it’s easier to see what’s going on.
If I’m understanding what you want to do, I think you could do it much more cleanly with:
if (isset($extra)) { // extra is set. We're done.
return '<td class="'.$css.'" width="'.$width.'" colspan="'.$colspan.'" rowspan="'.$rowspan.'">'.$data.'</td>';
}
If (isset($link) && isset($pop)) { // both are set
return '<td class="'.$css.'"><a href="'.$link.'" target="_blank">'.$text.'</a></td>';
} else { // at least one is not set
return '<td class="'.$css.'">'.$data.'</td>';
}
Many old-school coders would also complain that the code has three exit points instead of one. To solve that, you could use a variable, $output, and set it like this:
$output = "";
If (isset($extra)) {
$output .= '<td class="'.$css.'" width="'.$width.'" colspan="'.$colspan.'" rowspan="'.$rowspan.'">'.$data.'</td>';
} elseif (isset($link) && (isset($pop)) {
$output .= '<td class="'.$css.'"><a href="'.$link.'" target="_blank">'.$text.'</a></td>';
} else {
$output .= '<td class="'.$css.'">'.$data.'</td>';
}
return $output;
The .= (concatenation) operator tacks whatever is on the right onto the end of the variable on the left. This is a good practice in case you ever want to massage $output before or after your if statements. For example, if you wanted to wrap the html code in a <div class="something"></div>, you’d just put:
$output = '<div class="something">'; // at the beginning
and
$output .= '</div>'; // at the end just before the return.
Using $output and a single return also makes it easier to debug and (I think) takes some strain off the server since it doesn’t have to interpret the return code.
To refine it a little further, since all three return strings start with ’<td class="’.$css.’", you could make that the initial value of $output and leave that out of the later statements, but it would be easy to make a typo that would screw things up.
I hope this makes sense and isn’t TOO much more than you wanted to know. . .
Bob