PHP and MySQL Introductions


PHP

Ok, first the basics of PHP.

The basics in PHP is the include command, the require command, the echo command, and the if/else command.

Ok, first things first. All PHP starts out like this, and you can insert this into HTML if you would like, or just have strict PHP...but it starts like this:

PHP Code:
<?php
?>
OR

PHP Code:
<?
?>
Ok, now that is out of the way. Another good thing to learn before we get started is commenting. I suggest while you are starting that you leave a lot of comments, or if you are sharing your work with someone you post comments so that they know where everything is. A comment works as such:

PHP Code:
<?php

// This is a comment stating that the code below will translate in HTML as
// Hi with a page break.

echo "Hi 
"
;
?>
The lines with // in front of it are comments. In PHP, // will make the document ignore whats in front of it.

Ok, now let's work with variables. To make it easier on you so that you don't have to type so much or to actually call down an advanced form of array (or group of variables), calling variables is the easy way to do it. Let's look at an example.

PHP Code:
<?php
// Making some variables
$jd="Joshua Drake";
$sic="Pat Andrew";
$aku="Jared White";

// Making my statement
echo "VD-Host Owner - SEO >>" $jd ;
echo 
"
VD-Host Web Developer >>" 
$sic ;
echo 
"
VD-Host Web Designer >>" 
$aku;
?>
Hopefully that will give you a good example of how to use variables. Now to require/include commands. These commands pretty much add codes of another page into your work, allowing you to write cleaner and more organized work. Ok, say we want to write out a lot of different variables, we can make a file (I'll call it var.php for this) and fill it with our info. This is an example of a var chart.

PHP Code:
<?php
// Making some variables
$jd="Joshua Drake";
$sic="Pat Andrew";
$aku="Jared White";
$d=date("D");
?>
Ok, now to some fun stuff. This next snippit will not only show you how to include that var chart, but I'm also going to explain if/else commands. This one is pretty easy to comprehend, if you noticed earlier I was going to have it call $d for the date. Well, to call a date from a variable, you use date("D"); One of those lovely things that come with PHP Ok, TO WORK!

PHP Code:
<?php
//  This is to call down the var.php.  Take note of the syntax.
require_once('./var.php');

// Note that I use require_once instead of require or include.
// That's because it's only variables in that file, not something
// that needs to be read over and over.

echo "Hello" $jd ;

// If today is friday, it will tell me to have a nice weekend...otherwise...

if ($d=="Fri")
{
echo 
"
Have a nice weekend"
;
} else {
echo 
"
Wow, this day sucks almost as bad as " 
$aku ;
}
?>
For the brackets {}, you only have to use those when the command is two lines or greater, but it's a good habit to follow, so do it anyway.

Hopefully that will get your foot in the door of PHP. While that may only be the beginning easy stuff, it will probably make some of the syntax you see in PHP make a lot more sense.

Coming soon in this tutorial:
  • MySQL Basics
  • PHP/MySQL - How to use them together.
  • PHP/MySQL - Advanced Ordering

Quick Tutorial Search