Quakenet/#php Tutorial

Note: If you opened this page from an external URL pay attention that all chapters are linked together. Be sure you also read all prior chapters of this tutorial, otherwise you will miss relevant content explained before.

Basics

  1. Layout of a php file
  2. Functions

1. Layout of a php file

PHP code can be used everywhere in html. To switch into php mode write the character sequence <?php. To leave the php mode again write ?>. Another way is to stop the php mode with the file ending. You can switch into php mode everywhere in your file, its even possible to use the whole file in php mode (file beginns with <?php and ends with ?>). The webserver only handle files as php scripts if the file got the .php extension. This is the default settings for webservers with php support, but can be changed by the admin of course.

A sample php script looks like this:

<?php
echo "Example of a php script\n";
?>

In this example the whole file is in php mode. But as mentioned above the php mode can be used only at possition where needed, too.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" lang="en">
    <head>
        <title>Hi all</title>
    </head>
    <body>
        <?php
        
echo "<p>The php mode</p>\n";
        
?>
    </body>
</html>

Any text which is outside of php mode is send to the client without interpreted by php. But it can be controlled with a php construct like loops.

The second version isn't useable for changing the headers. With the first character (which doesn't belongs to <?php) which is send as content to the client (in this example the < from the line <?xml version="1.0" encoding="utf-8" ?>) the headers are also send to the client. So its impossible to change the headers with setcookie oder header anymore.

Using a xml declaration can be problematic in some cases. The xml declaration starts with <?xml which is not the same as <?php. But there exists a setting called short_open_tag which allowes you to enable php mode just with <?. This will interfere with the xml declaration if the setting is enabled as php tries to interprete the code xml version="1.0" encoding="utf-8" as php code. Since this isn't valid php code a error message is shown. To solve this problem you can echo the xml declaration with php. A easier way is to deactivate this setting.

2. Functions

To test php create a file with the following content.

<?php
phpinfo
();
?>

Save this file as phpinfo.php and upload it to the webserver. If you open this file in your browser you can get two possible outputs.

  1. You get an empty page. In this case the following must be checked.

    • The file must got the .php extension so the php interpreter loads this file.

    • The file contains valid php code.

    • The webserver supports php.

    • The file is loaded via http://. Beginners often try to open the file over an URL like file://C|/....

    In this case check the source code you get from the server. Depending on your browser you can see the source code at View -> Source. If you see your php code then the webserver doesn't support php.

  2. In the more common case you see a lot of information about the installed php version. You can even check the configuration with phpinfo. This is the first location to check which php modules are installed and how the configuration settings are. Its important to check the php version, but also important to check settings like error_reporting and display_errors.

In this example you see how to use a function. Each function call starts with the function name, in this case its phpinfo. It is followed by an opening parenthese (. It is possible to supply parameters for the function. These parameters changes the processing of the function accordingly to the definition of the function in the manual. If a function accept more than one parameter the parameters are seperated by a comma (,). After all parameters (if any) the parameter list is closed by a closing parenthese ). These parentheses must be used even if no parameter are used (like in phpinfo();). Each statement in php must be terminated by a semicolon ;. A single function call like this one is a valid statement in php any must be terminated by semicolon.

<?php
name_of_function
(parameter1,parameter2,...);
?>

Each part of a function call can be seperated by any number for spaces. To be more specific each part can be seperated by any number of whitespaces. These includes spaces but also includes newlines and tabs. The following function call are all equivalent.

<?php
name_of_function
(parameter1,parameter2);
name_of_function(parameter1parameter2);
name_of_function     (parameter1    ,parameter2)     ;
name_of_function(
    
parameter1,
    
parameter2);
name_of_function       (      parameter1      ,
    
parameter2
)             ;
                      
name_of_function        (parameter1,parameter2)         ;
?>

It depends on your own programm style which one is the best for you to read. In php there exists a standard called PEAR Coding Standard. You should use this standard to write and indent your code so you and any other php programmer can read and understand your code.

Questions about the chapter

1. Which condition must be met so a php file is parsed?

A php file is parsed if it met the following conditions:

Back to Next to
Copyright © to the OPs of #php/QuakeNet Valid XHTML 1.0 Strict Valid CSS!