Pin Me

PHP Tutorials for Dummies

written by: Darrin Koltow•edited by: Simon Hill•updated: 2/16/2010

This article shows you the basics of the PHP language, including its variables, sample programs, development options, and more.

  • slide 1 of 9

    PHP Tutorials for Dummies

    PHP tutorials for dummies can be found all over the Web. But, if you're interested in programming in PHP, you can't be all that dumb. PHP is a scripting language used widely on the Web. With PHP, you can process web page forms, search your server's directories, fetch content from other servers, execute queries on your MySQL database, and more.

    One benefit of developing in PHP is fairly rapid development time--if you have a rich IDE (integrated development environment) with syntax highlighting and PHP library lookups, similar to Microsoft's Intellisense. Also, PHP is an interpreted, rather than compiled, language, meaning that you don't have to build the executable from the script. Instead, you hand your script off to the PHP interpreter for execution.

    1 

    Code completion in a PHP IDE

  • slide 2 of 9

    Set up your development environment

    Undertake one of the following two development options to begin coding in PHP.

    Option one involves signing up with a hosting company that has PHP. You'll write PHP code on your computer, upload it to your server, then request the PHP page in your browser to have it executed on the server. Option two involves simulating a server on your computer by using server software, such as Apache.

    You can do either option for free. Find a listing and review of free PHP hosting companies here.

    To do the server emulation option, first get a free PHP IDE from these sites: NetBeans.org, Eclise.org, or Aptana.org. Then, get a package that includes MySQL, PHP and server software bundled together. Here's one resource for that approach.

    The remainder of this article will assume you've set up one of the development environment options just mentioned.

  • slide 3 of 9

    Howdy, Planet

    Using your IDE or windows Notepad, paste the following PHP program into your code window:

    <?php

    echo "Hail, citizens! Well met.";

    ?>

    Save the file as "MyFirstPHPApp.php," then upload it to your server, (or your simulated server on your hard drive). Open up a browser window on the file you just uploaded, to cause the server to execute the script. Notice the resulting message from the script.

    How it works

    Take a look at the script's content, which starts and ends with the "<?php" and "?>" tags, as all PHP scripts must. We gave the PHP another hint that our script was PHP and not HTML or something else: the filename extension of the script: .php. Be sure each of your PHP scripts has that extension in its filename.

  • slide 4 of 9

    Use Echo for output

    There's little doubt about what the "Echo" statement is doing, but learn a few things about this statement besides its obvious function.

    First, you can use other output statements besides echo. Replace that "echo" in the script with this statement: "print." Re-run the script using the instructions given previously. The output will be the same.

    Realize that you could have written "PRINT" or "ECHO," in place of the lowercase text you typed--but that doesn't mean PHP is case-insensitive. Try the following example to reveal PHP's case sensitivity. Replace the code in your script with this code:

    <?php

    $T = "citizens";

    $t = "people";

    PRINT "Hail ".$T."! Well met."."<br>";

    PRINT "Hail ".$t."! Well met.";

    ?>

    Run the script as you ran its previous incarnation, and notice the output: two different statements, coming from two different variables. The variable names are both "T"s, but differ in case. The PHP interpreter would not have printed two different statements if it were case-insensitive. In short, pay attention to case when you code in PHP. Your IDE's syntax highlighting will help you with this.

  • slide 5 of 9

    Punctuation

    Notice the punctuation used in the foregoing script, the period and semicolon in particular. You can probably guess correctly that semicolons are needed to terminate each PHP statement.

    Guessing the role of the period might be a little tougher: it's a concatenator of strings; it joins them together. Compare this with JavaScript's syntax:

    alert ("hello " + "world");

    JavaScript uses the plus to join strings. Be careful if you're a veteran JavaScript programmer, just learning PHP. Change your pluses to periods.

  • slide 6 of 9
    Learn How to Write PHP Programs that create HTML, CSS and JavaScript contentThe PHP language can create fresh HTML, CSS and JavaScript content, among its many other abilities. Reading PHP tutorials for dummies will show you how to leverage PHP to create this content. PHP Tutorials for Dummies don't often have this content, but we do. Learn it here.
  • slide 7 of 9

    Variables

    Take a look at the variable assignment statements in the foregoing script. If you sense there's something missing, you're right. The variable declarations aren't there. In PHP, you don't have to declare variables; you can just start using them. A variable comes into existence when you assign something to a token. That assignment looks like this:

    $x = "stuff"; or $x = 2;

    The PHP interpreter is smart enough to treat the variable as it should be based on its type--be it integer, floating-point number, string or some other type.

    Read more about PHP variables here.

  • slide 8 of 9

    Creating HTML content

    If you look back at the script with the PRINT statement, you'll notice a tiny bit of HTML in one of those statements: "<br>." This statement is a hint that you can use PHP to display any HTML content. Try this example:

    <?php

    echo "<html>"; echo "<head>";

    echo "<script>alert('Here is some javascript.');

    </script>";

    echo "<style type='text/css'>

    .style1 { font-style: italic; text-decoration: overline underline; font-family: Verdana; }

    </style>";

    echo "</head>"; echo "<body>";

    echo "<P class='style1'>Here's some formatted text.</P>";

    echo "</body>";

    echo "</html>";

    ?>

    This snippet features PHP creating HTML, JavaScript and CSS. The result is web content that looks like a normal HTML page, once you download it to your browser.

    You can start to get a sense of PHP's ability to create dynamic HTML content by replacing the line "Here's some formatted text" with this one:

    echo "<P class='style1'>Here's the date: ".date('M-d-Y')."</P>";

    See this page for detailed information about PHP's date functions and how to format them.

    These last few fragments point out another detail about how PHP handles strings. You can surround strings with either double or single quotation, as long as you end a double quote with a double quote and a single with a single.

  • slide 9 of 9

    Form processing

    One common application of PHP is to validate and process the input from HMTL forms. The following script gives an example of that process.

    Paste the following script into a new PHP file, then use the previous examples' instructions to test it in your browser.

    <?php

    if ( ($em = $_POST["email"]) == "" ) {

    showFormPage();

    } else { if (strpos($em, "@") == FALSE ) {

    showFormPage();

    } else {

    showSuccessPage();

    }

    }

    function showFormPage() {

    echo ' <html >

    <body>

    <form action="t.php" method="POST">

    <p> Enter a valid email address.</p>

    <p> <input id="Text1" type="text" name="email" /></p>

    <p>

    <input id="Button1" type="submit" value="button" />

    </p> </form> </body> </html> ';

    }

    function showSuccessPage() {

    echo ' <html > <body> <H2> Thank you for your information! </H2> </body> </html> ';

    }

    ?>

    Notice how the PHP displays complete HTML pages, and how it chooses which page to display based on what's in the predefined $_POST variable.

    The $_POST variable is an array that's populated with the names and values of all the elements inside an HTML form element. This populating happens when the user presses the submit button on the form, which calls the PHP form-processing script.

    In this example, the script first checks for an empty $_POST element, which would indicate that the form must first be displayed to the user. That element will contain user input text only after the user hits the HTML form's submit button. Once the PHP script detects that input via a non-empty $_POST["email"], it will validate the email and display a new HTML page appropriate to the results of the validation.

    Learn more about PHP form submission here.

    Search the Web for PHP tutorials for dummies, but come to Bright Hub to learn PHP for smarties.

    References

    Home page of the PHP Hypertext Processor.