0% found this document useful (0 votes)
100 views

Chapter 2 PHP Fundamentals

The document discusses PHP fundamentals and variables. It explains that PHP does not require declaring variables, and variables can be used directly. It provides an example of a basic PHP file with HTML markup and PHP code to output strings and variables. The document also discusses using error reporting functions to display PHP errors, and gives examples of how errors provide debugging information.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

Chapter 2 PHP Fundamentals

The document discusses PHP fundamentals and variables. It explains that PHP does not require declaring variables, and variables can be used directly. It provides an example of a basic PHP file with HTML markup and PHP code to output strings and variables. The document also discusses using error reporting functions to display PHP errors, and gives examples of how errors provide debugging information.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 2 PHP Fundamentals

Unlike other programming languages, PHP does not have a specific


command for declaring variables. You must pay attention to where and
how variables are declared and used.
Let's write some code and see how the browser handles it. Go into your
beginning-php8-and-mysql directory and under chapter 2, create a file
called newtest.php. Inside of this file, write

<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>

Navigate to localhost:8000/chapter2/vartest.php and look at the


results (Figure 2-1).

16
Chapter 2 PHP Fundamentals

Figure 2-1. URL result web page

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.

Using Errors As Tools


In PHP, you do not always see the errors that occur. This is because there
are three different levels with configurations for how and where to display
them. Let’s go back into vartest.php and add these lines to the top:

<?php
error_reporting( E_ALL );
ini_set( "display_errors", 1);

//this next line is an error


echo "these pretzels are making me thirsty;

echo "<p>variable test</p>";


$color = "blue";
$item = "pants";
echo "Today I am using $item which happen to be the color
$color";

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

// Turn off all error reporting


error_reporting(0);

// Report simple running errors


error_reporting(E_ERROR | E_WARNING | E_PARSE);

18
Chapter 2 PHP Fundamentals

// Reporting E_NOTICE can be good too (to report uninitialized


// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE


error_reporting(E_ALL & ~E_NOTICE);

// Report all PHP errors


error_reporting(E_ALL);

// Report all PHP errors


error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

The next line of ini_set() is used in PHP to overwrite configuration


options that are set in the php.ini file. This is helpful when you need to do
one-off configurations or are on a server where you do not have access to
the ini file. The next line is the error line. Do you see it? Go ahead and pull
up the file in your browser and see what it says.

<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'>

  <link rel='stylesheet' href='css/styles.css?v=1.0'>

</head>

20

You might also like