2. Sending Emails
PHP must be configured correctly in the php.ini file with the details of how
your system sends email. PHP makes use of mail() function to send an
email
Syntax: mail( to, subject, message, headers, parameters );
Parameter
Description
to
Required. Specifies the receiver / receivers of the email
subject
Required. Specifies the subject of the email.
message
Required. Defines the message to be sent. Each line should be separated
with a (n).
headers
Optional. Specifies additional headers, like From, Cc, and Bcc. The
additional headers should be separated with a CRLF (rn)
parameters
Optional. Specifies an additional parameter to the sendmail program
3. Debugging and Error Handling
General Programming Errors
1) Syntax errors 2)Runtime errors 3) Logical
errors
Syntactical errors are the most common and the easiest to
fix. You'll see them if, for example, omit a semicolon. PHP
will show an error, including the line PHP thinks it's on.
This type of error stops the execution of the php script.
Also, SQL errors are normally a matter of syntax.
Parse error: syntax error, unexpected T_VARIABLE in
C:serverwwwfile_name.php on line 6
4. Runtime errors
Run-time errors include those things that don't stop a PHP
script from executing but do stop the script from doing
everything it was supposed to do.
This occurs when, for example, it is called a function using
the wrong number or types of parameters.
Missing argument 1 for function_name(), called in
C:serverwwwfile_name.php on line 6 and defined in
C:serverwwwfile_name.php on line 5
5. Logical errors
Logical errors are actually the worst,
because PHP won't necessarily report it to
you. These are outand-out bugs: problems
that aren't obvious and don't stop the
execution of a script.
6. PHP Error Handling
There are four type of errors are present in PHP
1-Notices : These are trivial, non-critical errors. that does not
terminate script .
Condition: Accessing a variable that not define
2-Warning : These are more serious errors
Condition:attempting to include() a file which does not exist.
3-Fatal errors (Runtime errors)
These are critical errors that terminate script and stop
Condition:1- Calling a non-existent function
2-Missing semicolon 3-missing braces
4- Parse Error (Syntax errors)
When we make mistake in PHP code like, missing semicolon or any
unexpected symbol in code
7. PHP Error Handling (con’t)
Following are the different error handling
methods:
1.
2.
3.
Simple "die()" statements
Custom errors and error triggers
Error reporting
8. Basic Error Handling: Using the
die() function
simple script that opens a text file
<?php
$file=fopen("welcome.txt","r");
?>
If the file doesnot exist youmight get an error like this:
Warning: fopen(welcome.txt) [function.fopen]: failed to open stream:
No such file or directory in C:webfoldertest.php on line 2
9. To avoid that the user gets an error message
like the one above, we test if
the file exist before we try to access it:
9.
<?php
if(!file_exists("welcome.txt"))
{
die("File not found");
}
else
{
$file=fopen("welcome.txt","r");
}
10.
?>
1.
2.
3.
4.
5.
6.
7.
8.
File not found
10. Creating a Custom Error Handler
Creating a custom error handler is quite simple.
We simply create a special function that can be
called when an error occurs in PHP.
This function must be able to handle a minimum
of two parameters (error level and error
message) but can accept up to five parameters
(optionally: file, line-number, and the error
context):
11. Custom Error Handler
Syntax:error_function(error_level,error_message,
error_file,error_line,error_context)
Parameter
Description
error_level
Required. Specifies the error report
level for the user-defined error. Must
be a value number. See table below
for possible error report levels
error_message
Required. Specifies the error
message for the user-defined error
error_file
Optional. Specifies the filename in
which the error occurred
error_line
Optional. Specifies the line number in
which the error occurred
error_context
Optional. Specifies an array
containing every variable, and their
values, in use when the error
occurred
13. Trigger an Error
In a script where users can input data it is useful to trigger
errors when an illegal input occurs. In PHP, this is done by
the trigger_error() function.
In this example an error occurs if the "test" variable is
bigger than "1":
1.
2.
3.
4.
5.
6.
7.
<?php
Notice: Value must be 1 or below
$test=2;
in C:webfoldertest.php on line 6
if ($test>1)
{
trigger_error("Value must be 1 or below");
}
?>
14. Send an Error Message by E-Mail
1.
2.
3.
4.
5.
6.
7.
8.
9.
By using the error_log() function you can send error logs to
a specified file or a remote destination.
<?php
//error handler function
function customError($errno, $errstr)
{
echo "<b>Error:</b> [$errno] $errstr<br />";
echo "Webmaster has been notified";
error_log("Error: [$errno] $errstr",1,
"someone@example.com","From:
webmaster@example.com");
}
15. Generating an XML Document
XML is a markup language that looks a lot like HTML. An XML
document is plain text and contains tags delimited by < and
>.There are two big differences between XML and HTML:
XML doesn't define a specific set of tags you must use.
HTML list that's not valid XML:
<ul>
<li>Braised Sea Cucumber
<li>Baked Giblets with Salt
<li>Abalone with Marrow
and Duck Feet
</ul>
HTML list that is valid XML:
<ul>
<li>Braised Sea Cucumber</li>
<li>Baked Giblets with Salt</li>
<li>Abalone with Marrow
and Duck Feet</li>
</ul>
16. PHP XML DOM
The built-in DOM parser makes it possible to
process XML documents in PHP.
What is DOM?
The W3C DOM provides a standard set of objects
for HTML and XML documents, and a standard
interface for accessing and manipulating them.
*Core DOM - defines a standard set of objects for
any structured document
* XML DOM - defines a standard set of objects for
XML documents
* HTML DOM - defines a standard set of objects
for HTML documents
17. Parsing an XML Document
To read and update - create and manipulate - an XML document,
you will need an XML parser.
There are two basic types of XML parsers:
1.
Tree-based parser: This parser transforms an XML document
into a tree structure. It analyzes the whole document, and
provides access to the tree elements
2.
Event-based parser: Views an XML document as a series of
events. When a specific
The DOM parser is an tree-based parser.
To create a SimpleXML object from an XML document stored in a
string, pass the string to simplexml_load_string( ). It returns
a SimpleXML object.