Include PHP functions

I have a php file for include and css page functions. I am trying to make an include function to generate most of my html. I just can't figure out how to get the css file in the php include function I did.

<?phpe
    function HTMLStart( $Title)
    {
        echo "<!DOCTYPE html>\n";
        echo "<html>\n";
        echo "<head>\n";
        echo "      <title>".$Title."</title>\n";
        echo "      <link href=\ 'IncLab.css' type=\"text/css\" rel=\"Stylesheet\" />\n";
        echo "</head>\n";
        echo "<body>\n";
    }
?>

      

I have a problem with the line echo

:

echo "      <link href=\ 'IncLab.css' type=\"text/css\" rel=\"Stylesheet\" />\n";

      

I tried using double quotes and getting an error, so I tried using single quotes for "IncLab.css" and didn't get any errors. I was hurt if this is the right way to do it.

Also, I created a nav bar in my include file, but how do I add the class (for CSS) so that my links line up correctly.

<?php
    function PageNagivation()
    {
        echo "<!-- Nav of the page --> \n";
        echo "<nav> \n";
        echo       '<div class="nav-item float-left"> <a href="IncLabPage1.php">Page 1</a>';
        echo       '<div class="nav-item end-float"> <a href="IncLabPage2.php">Page 2</a>';
        echo "</nav> \n";
    }
?>

      

The navigation bar works and displays, but does not display correctly. The problem I know lies in a piece of code <div class....

. How can I get it to work. Thank you.

+3


source to share


5 answers


For the quote problem, yes, it is perfectly acceptable to use single quotes in this situation for what they are for. You escaped one of them, which you don't need to do:

echo "<link href='IncLab.css' type='text/css' rel='Stylesheet' />\n";

      

To add a class, you can simply inject it into HTML, for example:

echo '<div class="nav-item end-float myclass"> <a href="IncLabPage2.php">Page 2</a>';

      



Or, if it is stored in a variable:

echo '<div class="nav-item end-float '.$myclass.'"> <a href="IncLabPage2.php">Page 2</a>';

      

or

echo '<div class="nav-item end-float $myclass"> <a href="IncLabPage2.php">Page 2</a>';

      

+6


source


Change the quotes as follows:

echo "      <link href='IncLab.css' type='text/css' rel='Stylesheet' />\n";

      



You should do this with all the HTML you echo to keep your code clean and troubleshooting simplified.

+3


source


Instead of one echo for one line, why don't you do it?

$sOutput = '<html>
            <head>
                <link href="IncLab.css" type="text/css" rel="Stylesheet">
                <!-- Any HTML you want to echo -->';

echo $sOutput;

      

Or better, use the heredoc syntax:

$sOutput = <<<EOT
<html>
<head>
   <link href="IncLab.css" type="text/css" rel="Stylesheet">
   <!-- Any HTML you want to echo -->
EOT;

      

Just one quote at the beginning and at the end, and you can even step back a little ... And you get rid of anxiety by avoiding so many quotes.

+2


source


You can avoid these problems by using the "boilerplate" part of the PHP language correctly.

<?php
    function HTMLStart( $Title)
    {
?>
        <!DOCTYPE html>
        <html>
        <head>
          <title><?php echo $Title ?></title>
          <link href="IncLab.css" type="text/css" rel="stylesheet"/>
        </head>
        <body>
<?php
    }
?>

      

+1


source


I don't recommend outputting html with php. However, in your code, you are not avoiding href = \ '... Just use single quotes:

echo "      <link href='IncLab.css' type='text/css' rel='Stylesheet' />\n";

      

As far as navigation is concerned, you are not closing your divs. It should be like this:

echo "<!-- Nav of the page --> \n";
echo "<nav> \n";
echo       '<div class="nav-item float-left"> <a href="IncLabPage1.php">Page 1</a></div>';
echo       '<div class="nav-item end-float"> <a href="IncLabPage2.php">Page 2</a></div>';
echo "</nav> \n";

      

Without knowing what you want your nav to look like and have no css for the classes you are calling, it's hard to help further. Normall, the navigator is in an unordered list, for example:

<ul class="nav">
    <li><a href="IncLabPage1.php">Page 1</a></li>
    <li><a href="IncLabPage2.php">Page 2</a></li>
</ul>

      

CSS

ul.nav {
    list-style: none;
}
ul.nav li {
    display: inline-block;
}

      

0


source







All Articles