Results 1 to 4 of 4

Thread: Introduction to PHP

  1. #1
    Join Date
    Feb 2010
    Posts
    136

    Introduction to PHP

    Introduction to PHP

    1. Introduction
    In this tutorial we will learn the principle of operation of PHP. We will try to make a web page in PHP. Do not expect extraordinary results (in fact the page that we create will not do anything special) but it will allow you to take our brands. You'll especially understand how it separates the XHTML standard PHP code. This tutorial considers that you know basis of HTML and XHTML.

    2. The PHP tags
    So you know that the source code of a page consists of XHTML tags (also called tags). For example <ul> is a tag.
    The PHP code is inserted in the middle of XHTML. We'll gradually put in our homepage pieces of PHP code within XHTML. These bits of PHP code will be the parties dynamic Page, that is to say that the parties can change all alone.
    Have a look at a simple example

    Code:
    <! DOCTYPE html PUBLIC "-//W#C//DTC XHTML 1.0 Strict //EN" "http://www.w3.org/TR/xhtm
    <them xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title> My page</title>
    <meta http-equiv="Content_Type" content="text/html; charset=iso-8859-1: />
    </head>
    <body>
    <h1? My web page</h1>
    
    <p>
    Welcome !
    </p>
    </body>
    </html>
    As you can see, there is the XHTML code that is well known and is inserted in the middle of more dynamic data.


    The form of a PHP tag
    If I talk about this, it is not by chance. To use PHP, you will have to introduce a new tag and it is a bit special. It begins by <? php and ends ?>. It is within that we put the PHP code.

    Here is a PHP tag empty
    Code:
    <? php   ?>
    Inside, you will then write the PHP source code
    Code:
    <? php     
    / * PHP code goes here * / 
    ?>
    We can safely write the PHP tag over several lines. In fact, it is essential because most of the time the PHP code will be several lines. This will give something like:
    Code:
    <? php
    / * PHP code goes here
    And here
    And also here * /
    ?>
    Note: There are other tags to use PHP, for example <? ?>, <% %>Etc.. Do not be surprised if you see them. Nevertheless, <? php?> is the most correct.

  2. #2
    Join Date
    Feb 2010
    Posts
    136

    Re: Introduction to PHP

    Insert a tag in the middle PHP XHTML
    The PHP tag that we just discovered fits in the middle of XHTML as said earlier. Consider the example below
    Code:
    DOCTYPE html PUBLIC "name of the site and directory">
    <html xmlns ="site name" xml: lang ="en" lang ="en">
        <head>
            <title> This is a test page </ title>
            <meta http-equiv ="Content-Type" content ="text / html; charset = iso-8859-1" />
        </ head>
        <body>
            <h2>Test </ h2>
            
            <p>
                This page contains code (x) HTML with PHP tags.<br />
                <? php / * Insert PHP code here * / ?>
                Here are some quick tests:
            </ p>
            
            <ul>
            <li style ="color: blue;">Blue text</ li>
            <li style ="color: red;">Text in red</ li>
            <li style ="color: green;">Text in green</ li>
            </ ul>
            
            <? php
            / * Still PHP
            Also PHP * /
            ?>
        </ body>
    </ html>
    Of course this page does not work because we have not yet written real PHP code (they are simply markers for example). All you need to remember here is that when you want to put PHP code. We can put a PHP tag anywhere in the code, not just in the body of the page also you can put a PHP tag in the header of the page.
    Code:
    DOCTYPE html PUBLIC "site name and directory ">
    <html xmlns ="site name" xml: lang ="en" lang ="en">
        <head>
            <title>This is a test page <? php / * PHP code * / ?></ title>
            <meta http-equiv ="Content-Type" content ="text / html; charset = iso-8859-1" />
        </ head>
    Even more strange is, you can even insert a PHP tag in the middle of a tag XHTML.
    Code:
    DOCTYPE html PUBLIC "site name and directory ">
    <html xmlns ="site name" xml: lang ="en" lang ="en">
        <head>
            <title>This is a test page</ title>
            <meta http-equiv ="Content-Type" <? php / * PHP code * / ?> content ="text / html; charset = iso-8859-1" />
        </ head>

  3. #3
    Join Date
    Feb 2010
    Posts
    136

    Re: Introduction to PHP

    3. Display text
    I think it is time to write some code in PHP. Well do not expect something extraordinary, your computer is not going to dance itself. But you'll better understand how PHP works, that is to say how it generates XHTML code. It is essential to understand this, so be careful!

    The echo statement
    PHP is a programming language, which was not the case for XHTML (it talked instead of description language because it allows to describe a web page). If you have programmed in other languages like C or Java, this should not surprise you. However, in this course, we start from zero so I'll assume that you have never done programming before.

    Any programming language contains so-called Instructions. We wrote a line in general, and they all end with a semicolon. An instruction to command the computer to perform a specific action. Here the first statement that we will discover to insert text into the webpage. This instruction is echo. The simplest and most basic of all the instructions you need to know.

    Here is an example of using this instruction:
    Code:
    <? php echo "This is text"; ?>
    As you can see, inside the PHP tag is writing instruction echo followed by the text to display in quotation marks. The quotes can delimit the beginning and end of the text, it helps the computer to identify. Finally, the statement ends with a semicolon, which means End of Instruction. You should know that also has the right to request to display tags. For example the following code works:
    Code:
    <? php echo "This is <strong> text </ strong>"; ?>
    The word "text" will be displayed in bold with the presence of tags <strong> and </ strong>

    If you need to display the quotation marks, it means that, for the computer to display the text goes. You might do better to plant your beautiful code and have a terrible "Parse error".
    The solution is to precede the quote a backslash \ :
    Code:
    <? php echo "It was written \"only\" in PHP. "; ?>
    You know the PHP code is inserted in the middle of XHTML. So go ahead, take a basic page in XHTML and place ourselves ahead of PHP code:
    Code:
    DOCTYPE html PUBLIC "site name and directory ">
    <html xmlns ="site name" xml: lang ="en" lang ="en">
       <head>
            <title>Our first statement: echo</ title>
            <meta http-equiv ="Content-Type" content ="text / html; charset = iso-8859-1" />
        </ head>
        <body>
            <h2>Displaying Text with PHP</ h2>
            
            <p>
                This line was written entirely in (x) HTML.<br />
                <? php echo "It was written entirely in PHP."; ?>
            </ p>
        </ body>
    </ html>
    Save a PHP page
    Save the page with the extension .php eg. some.php. In the case tests I've been creating. This is essential whatever your operating system may be, is that the file is saved in the www (or its sub-folders) if the PHP file will not run. If you use Notepad++, select PHP Hypertext Preprocessor file (*. php) in the window to save. Once the page is saved, it must now be tested.

    PHP Test Page
    To test your PHP page, depending on your operating system you can try the following. On Windows, start WAMP if not already done so. Go to menu Localhost. The homepage opens. Here, if you have created the file tests in the Directory www as indicated in the previous section, you should see a link to the file tests. A web page opens showing all the files located in the folder tests. You should have the file some.php. Click it, your computer then generates the PHP code and then opens the page. You have the results right before your eyes.

    The same result can be obtained by going directly to the address http://localhost/tests/some.php in your browser. The method should be virtually the same whether you're on Windows, Mac OS X or Linux.

  4. #4
    Join Date
    Feb 2010
    Posts
    136

    Re: Introduction to PHP

    How PHP generated XHTML code
    Instruction echo asks PHP to insert the text that you request. If we translate the instructions in any language, it would, Insert the text: "It was written entirely in PHP." We must never forget the semicolon at the end of a statement. If ever that happens, you get the error message: "Parse Error"
    Note that it crashes only if your PHP code is more than one line. So make a habit of always putting a ";" at the end of instructions. I have explained in the first section about the PHP generated XHTML code and related to the code XHTML (with its CSS if any).

    Here, concretely, is what is happening with our source code:
    - The PHP code is executed first and the computer does what you request. Suppose it was told to Display the text here.

    Once all instructions executed PHP, the page comes out is a page that contains only XHTML. It is this page "result" that is sent to the visitor, because it can read that XHTML.

    Remember, only the server can run PHP. The PHP is never sent to the visitor. So that we can execute PHP on our computer (to make our tests) we had to transform into a mini-server by installing a program like WAMP.


    4. Comments
    I want to talk about something that I believe has great importance in PHP, as in any programming language: comments. A Comment is a text that you set for yourself in the PHP code. This text is ignored, that is to say, it completely disappears when the page generation. There is only you who see this text. But then what good is a comment? It is for you. This helps you find your code in PHP, because if you do not touch it for weeks and you return, you may be a little lost. You can write anything and everything, the trick is to use it wisely.

    There are 2 types of comments:
    - The single-line comments
    - Multiline Comments

    It depends if your comment is short or long. Both are shown here


    The single-line comments
    To indicate that you are writing a comment on one line, you must type 2 slash: //. Type your comment.
    Here is an example
    Code:
    <? php
    echo "I live in this world."; / / This line indicates where I live
    
    / / The following line shows my age
    echo "I have PHP code with me.";
    ?>
    I've made two comments in different places:
    - The first is at the end of a line.
    - The second is on a line
    You decide where you put your comments if you are commenting on a particular route, it is better to put the comment at the end of the line.


    Multiline Comments
    These are the most practical if you do write a comment on several lines (but you can also use it to write reviews of a single line). We must begin by writing /* and close by */ :
    Here is an example for it
    Code:
    <? php
    / * The following line shows my age
    If you do not believe me ...
    it would be fine,* /
    echo "I have PHP code with me.";
    ?>

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,633,098.35236 seconds with 17 queries