For our first example, we will try the first approach to separating markup and code. We will place the HTML output under test and change the PHP code slowly and incrementally so that the code is always working and the tests never fail. This is manageable in this case since there is not much HTML
The example is loosely based on a part of a real application. All the problems are real ones from that application. But the example is much simpler and cleaner than the original, which had several additional global and local variables and used these to build the URL.
The example is loosely based on a part of a real application. All the problems are real ones from that application. But the example is much simpler and cleaner than the original, which had several additional global and local variables and used these to build the URL.
function print_link($search,$form,$link_text,$blank_target) {
if( !($search || $form))
echo "<a href=\"index.php\"";
else {
if(!$search) {
echo "<a href=\"form.php\"";
}
else {
echo "<a href=\"index.php?action=search\"";
}
}
if($blank_target)
echo " target=\"_blank\">";
else
echo ">";
echo "$link_text</a>\n";
}
if( !($search || $form))
echo "<a href=\"index.php\"";
else {
if(!$search) {
echo "<a href=\"form.php\"";
}
else {
echo "<a href=\"index.php?action=search\"";
}
}
if($blank_target)
echo " target=\"_blank\">";
else
echo ">";
echo "$link_text</a>\n";
}