Jun
10
2013

Getting Started with PHP

I figured that since I spend most of my time buried DEEP in complex php functions, classes, and tips, it may be helpful to provide some background on what the hell you’re actually supposed to do if you have no idea!

That being said, I’m going to begin adding some more introductory content that will hopefully provide some insight and tips on where to get started, so that you can more effectively work with more complex procedures, functions, and classes.

PHP Syntax & Rules

  • These are the rules that must be followed to write properly functioning and structured code.
  • Similar to ‘most’ other programming languages in that all code must be contained within the PHP tags (i.e. <?php ?>)
  • PHP files must be saved with a .php extension (not .html, not .asp, not .jsp) for browsers to interpret the code and execute it properly
  • PHP lines or statements MUST end with a semicolon (” ; “)!
  • PHP files must have NO excess line breaks following the closing PHP tag (?>)
  • Short comments in PHP code must be prefixed with either double slashes or hash marks,
  • While longer comments should be wrapped in /* */, or /** comment here */ tags
/* I am a longer comment.
I take up multiple lines,
even though in this example
it isn't necessary */

Example of a Basic HTML page:

<html>
    <head>
        <title>This is a standard HTML page, and Im a standard static title</title>
    </head>
    <body>
        <h2>This is just HTML, while Im just a second level heading.</h2>
        <p>Nothing special to see here, Im just a paragraph.</p>
    </body>
</html>

Note that each tag (<html>, <head>, <title>) has both an open (i.e. <title>), and a close (i.e. </title>).

The same applies to PHP.  What opens must always close!

So how do we include PHP in HTML?

Simple…

<html>
    <head>
        <title>This HTML page has PHP!</title>
    </head>
    <body>
        <h1>Taking it up a notch</h1>

        <p>
        <?php
        echo "BAM. Here comes the PHP!";

        function tell_me( $name )
        {
            $tellme = "My name is ". $name;
            return $tellme;
        }

        echo tell_me("Bennett Stone");

        ?>
        </p>
    </body>
</html>

PHP code structure and practices

<?php if($someaction==true){echo "Just PHP doing my thing";} else {echo "I may or may not be doing my thing";}?>

The above is an example of how not to code for a number of reasons:

  1. Readability.  If you can’t easily read it, no one else can either!
  2. Debugging.  An error within a line such as the one above is very hard to track down due to #1.
  3. Valid use of spaces.  Even though PHP does require spaces between all functions and calls, failure to provide spaces between certain operators (“(/)“, “{/}”, “!=/==”) is capable of causing errors depending on server requirements and PHP installation.

The example above should actually be coded as…

<?php
if( $someaction == true )
{
    echo "Just PHP doing my thing";
    include "includes/trueaction.php";
}
else
{
    echo "I may or may not be doing my thing, as this will only show if \$someaction is == false";
    include "includes/falseaction.php";
}
?>

See the difference?

Usage and Escaping Special Characters

A special consideration when using PHP is that special characters often have to be escaped, or be used in conjunction with a backslash (“\”) in order to not ‘break’ the code as these are reserved for use by the PHP itself.
Fortunately, these characters are limited to the dollar sign ($), the double quote (“), and the single quote (‘).

Example:

<?php
echo "Escaping special characters is \"awesome\", and I'm so glad to be adding the backslash that I feel like \$!";
?>

Leave a comment