Chapter 2 PHP Fundamentals
Chapter 2 PHP Fundamentals
<code>
<p>this is normal text</p>
<?php
echo '<p>This is created by php';
?>
<p>this is normal text again</p>
</code>
The code above shows three lines. The first is normal HTML, the
second is running PHP code, rendering HTML. The third is more HTML
but after/outside the PHP code snippet. You can go between PHP and
HTML as many times as you like within a .php file. This can get messy, so
you want to limit this to very clean and precise code elements. Now create
a file called vartest.php and open it up. Type this code into the file and
save it:
<code>
<?php
echo "<p>variable test</p>";
$color = "blue";
$item = "pants";
echo "Today I am using $item which happen to be the color
$color";
</code>
16
Chapter 2 PHP Fundamentals
Ok, let's catch up on what you are doing here. First, you are declaring
the PHP script with
<code>
<?php
</code>
The next line you use echo. This is a PHP command that makes up
one of the most basic ways to display text from PHP to the browser. It does
one thing: sends output to the browser or command prompt. Notice that
after echo, you use the double quote (“) as the delimiter to separate this
portion of text and begin the line of text you wish to output to the screen.
When you are done with the text, you end it with another double quote.
The double quote is the delimiter that marks the beginning and end of a
line of text you want to use. As humans, we can easily determine text or
a sentence that is written down or on a screen. Computers need special
markings, delimiters in this case, to determine where the text boundaries
are. This is true for the echo command or for setting a variable, as in the
next line. $color is the name of a variable you want to use and "blue" is
the value you are setting it to. In this particular case, $color is a variable
of type string. A string is any text that you want to use that will not be used
to compute, say, a mathematical value. Once you have the value of "blue"
set in $color, you can use echo to display it on the page. You do this again
with the variable $item when you set its value to "pants". You will notice
as well that each line of code ends with a semicolon (;). In PHP, this is how
you tell the interpreter to stop reading the line and move on. You will get
an error any time you leave out the ending semicolon.
17
Chapter 2 PHP Fundamentals
Speaking of errors, let’s go ahead and get comfortable with errors and
how they can be useful to us instead of annoyances.
<?php
error_reporting( E_ALL );
ini_set( "display_errors", 1);
Before you run this, let’s explain what you are doing.
error_reporting( E_ALL ); is telling PHP to display ALL errors. Here
is the full list of options available for error_reporting:
<?php
18
Chapter 2 PHP Fundamentals
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
<code>
Parse error: syntax error, unexpected token ">" in /var/www/
chapter2/vartest.php on line 8
</code>
Using this error, you can begin to hunt down the bug. This error is
saying that line 8 has an unexpected >. Take a look at line 8 of your code:
<code>
echo "<p>variable test</p>";
</code>
19
Chapter 2 PHP Fundamentals
This line looks perfectly fine to me. What PHP is telling us is that
you have done something, in this case set a delimiter for text, on the line
BEFORE line 8 and now the perfectly acceptable > on line 8 is unexpected.
You need to look at line 6 where you will find the closing delimiter of “
missing at the end of your text. Go ahead and add “ to the end of the line
and refresh your page, which should now render with no errors.
Now that you can create and assign variables, render text to the screen,
and trigger/understand errors that show up, let’s start building some
pages. This is the reason you picked up this book, right? Go back to your
Chapter2 directory and open the file main.php.
<code>
<?php
error_reporting( E_ALL );
ini_set( "display_errors",1);
$title = "Beginning PHP 8 & MySQL";
$content = "Here is the main content for this page";
$html ="
<!doctype html>
<html lang='en'><?php
<head>
<meta charset='utf-8'>
<title>$title</title>
<meta name='description' content='Basic HTML5 Page'>
<meta name='author' content='Your name'>
</head>
20