Intro to PHP

Example

    1. "Hello world" in HTML

      1:<html>
      2: <head>
      3: <title>Hello, World</title>
      4: </head>
      5:
      6: <body>
      7: Hello World!
      8: </body>
      9:</html>


      In the case of a PHP page the HTML that a browser receives is partly generated when the request for the page occurs. This is why these and CFML pages are called "dynamic."

    2. "Hello world" in PHP:

      Here is a similar page, this time relying on PHP to create some of the output that will be sent to the browser:


      1:<html>
      2: <head>
      3: <title>Hello, World</title>
      4: </head>
      5:
      6: <body>
      7: <?php echo "Hello World from PHP!"; ?>
      8: </body>
      9:</html>

      Example 2
      View the output: hello_world.php

      There are a couple of important things to note about Example 2 . First, the document has the extension ".php" - this is what tells the HTTP server to hand over processing of this page to the PHP engine. Nevertheless, most of the file is still processed exactly is if it were ordinary HTML. The exception is line #7. Here there is some PHP code, located between

      <?php

      and

      ?>

      These tags turn "on" and "off" processing of the document as PHP - outside of these tags the document is treated like normal HTML and is sent directly to the client's browser. Inside of these tags the file is processed as PHP code. The code is executed and the output is built into the document.

      Line #7 relies on the PHP echo() function to create output almost identical to that of Example 1. Note as we said that print() does essentially the same thing.

    3. Inside-out languages

      PHP and other modern web programming languages (such as CFML) can be mixed with, or "embedded" into, normal HTML. Because code ends up getting inserted into HTML-like files (as opposed to generating HTML from scratch) languages like PHP are referred to as "inside out".


    4. But is this dynamic?

    5. This page might not seem truly dynamic, because it will always produce the same output. PHP of course can interact with a database as we did with CFML so that the content changes as the date does. Also, the following simple PHP program which prints out the current date and time will have content that is dynamic as well:

    6. Today's date in PHP

      1:<html>
      2: <head>
      3: <title>PHP Date Example</title>
      4: </head>
      5:
      6: <body>
      7: <?php
      8:
      9: echo "Hello again from PHP!";
      10: echo "<br>";
      11: echo "It is now: ";
      12: echo date ("l dS of F Y h:i:s A");
      13:
      14: ?>
      15: </body>
      16:</html>

      Example 3
      View the output: simple_date.php

      There are two things to note about Example 3 ... First, on line #10 an HTML tag is sent as part of the text coming from PHP. This illustrates how HTML can live either outside the PHP tags, or inside: if inside then it needs to be dynamically created by something like the echo() function.

      On line #12 the PHP date() function is called, which returns a string corresponding to the current date/time on the sever at the moment the page output is generated. This content is not only technically dynamic but also results in unique output each time the page is requested.

      The string ("l dS of F Y h:i:s A") that is passed into the date() function tells PHP what date format to use for the output.




    7. A. Set up a bookmark or get a local copy of the official PHP documentation

      PHP Manual - http://www.php.net/manual/en

      To quickly look up any PHP command (function), open the manual in a browser window, enter the name of the function into the search box and hit enter .

      A Basic Tutorial - http://www.php.net/tut.ph
      History of PHP - http://www.php.net/history

      There is also a page in the online manual comparing PHP with some other languages:
      http://www.php.net/manual/en/faq.languages.php