Geekwise: PHP




Greg Goforth
Decipher Inc.


greg.goforth+geek@gmail.com


PHP - What is it?


PHP: Hypertext Preprocessor

  • Allows manipulation of HTML before it's output to the browser
  • Commonly used on web servers
  • Open source
  • Multiple OS / Servers

PHP - What can it do?


  • Dynamic web pages
  • Database interaction
  • Processing of user supplied data
  • Email
  • File manipulations
  • Text Processing
  • Image manipulation
  • .....

Getting started


What is required to create a PHP website?

  • PHP
  • Webserver (apache 2)
  • MySQL*
  • phpMyAdmin*

* not required, just nice to have

install all the things


Windows:
http://www.wampserver.com/en/

Mac:
http://www.mamp.info/en/

Ubuntu:
sudo apt-get install lamp-server^
sudo apt-get install phpmyadmin

Ok, I've got wamp, mamp, or lamp installed, now what?


Point your browser to:

http://localhost/

You should have some kind of page that shows you that PHP, apache and mysql have been installed and are ready for use.

our first PHP webpage

 <html>
    <head>
        <title>My First PHP Page</title>
    </head>
    <body>
        Hello World!!!
    </body>
</html>

Save this file as hello.php inside your www directory.  The location of this directory may vary based on your os.  Then navigate your browser to: http://localhost/hello.php


Windows: c:\wamp\www
Mac: /Applications/MAMP/htdocs
Linux: /var/www

our first PHP webpage

 <html>
    <head>
        <title>My First PHP Page</title>
    </head>
    <body>
        Hello World!!!
    </body>
</html>

Some things to note here:

  • The file is displayed in the browser, meaning PHP is successfully parsing the document and outputting the proper markup.
  • The markup thus far is standard HTML.  We have not yet begun utilizing PHP to do anything interesting. 
  • In order to do that we need to understand how PHP works...

getting started using PHP

PHP works by parsing the html document looking for anything inside  <?php ... ?> tags.  


The content found between the opening and closing tags is evaluated by PHP.  This is where things start getting fun.



some fundamentals


  • PHP content always lives inside  <?php ... ?> tags.
  • If your server has the setting enabled you can just use <? ... ?>
  • PHP will not output anything (aside from errors) unless you tell it to do so.
  • It does not have to live inside html tags, but it often does, more on this later.
  • All commands in PHP must be terminated with a semicolon.
  • The "echo" command is one way of outputting content to your browser.

using PHP

Lets change our hello.php file to start using some PHP.  
 <html>
    <head>
        <title>My First PHP Page</title>
    </head>
    <body>
        <?php echo "Hello World"; ?>
    </body>
</html>
Notice we are now including a snippet of PHP.  The above code tells php to use the echo command and output the string "Hello World".  

Refresh your browser and the result should be the same as the static page, only this time, PHP was responsible for outputting our mesage.

PHP: Strings, numbers


  • Strings in php must be quoted, like "this is a string" or 'this is also a string'
  • Numbers are not enclosed in quotes, use them like: 1, 50, 500.3213, etc...

PHP: comments

Commenting your code IS EXTREMELY IMPORTANT.  Failure to do so will result is much banging of your head against a desk 6 months from now when you can't remember what some code was supposed to be doing.  

PHP comments look like:

//This is a comment/* This is a multi line comment */ 

 Seriously, comment your code.

PHP: displaying data

There are a couple ways to display data from your php code.  These are echo  and  print .

echo "<span class='foo'>Hello World</span>";echo("Hello World<br />");
print "<p>Hello World</p>";print("Hello World");

 Data output from PHP code will be interpreted by the browser as html.

PHP: escaping characters

There are some characters in PHP considered "special" and these need to be escaped in order to use them.
  • Escape special characters with a \
  • Failure to escape special characters will cause errors that you would be notified of when you run your PHP script.
  • Some examples of characters that may need escaping are double and single quotes:
    echo 'This is Greg's php page.'; //errorecho 'This is Greg\'s php page.'; //correct escaping
    echo "The man said, "Hello world!!!""; //errorecho "The man said, \"Hello world!!!\""; //correct escaping

PHP: variables

Variables are a key concept in working with php.

Variables let us store values (be it a string, number, boolean, etc...) for use later in our script.

  • All normal php variables start with the $
  • Variable names are case sensitive, $myname is different than $myName.
  • Variable names must start with a letter or underscore
  • You assign a value to a variable using the =

PHP: using variables

Lets adjust our hello.php script to try out using some variables.
 <html>
    <head>
        <title>My First PHP Page</title>
    </head>
    <body>
        <?php 
            $hello = "hello world";
            echo $hello;
        ?>
    </body>
</html>
In the php snippet above we create a variable called $hello, and assign it a value of "hello world".  Then we output (echo) the string to the browser.  Try changing the variable value and refreshing your browser.

PHP: constants

There are special variables in PHP called constants.  These are variables that after they are created, can not be modified.
Constants are declared using the define command.
  • By convention they are UPPERCASE
  • No $
  • Not changeable after declaration

define('FOO', 'bar');echo FOO; //bar

PHP: which quotes?

In PHP, there is a difference between single and double quotes.  

In double quotes, $variables are "expanded" to their values, while in single quotes they will not be.  For example:

$word = 'bar';echo "Foo $word"; //Foo barecho 'Foo $word'; //Foo $word

Title

Made with Slides.com