Archive for February, 2006

Introduction to PHP - Mini tutorial on the basics of PHP

Saturday, February 11th, 2006

PHP Hypertext Preprocessor is an advanced scripting language that is very usefull for web developers. I am some what new to the PHP syntax but I will try to write some helpfull tutorials on it as I learn.

Ok, Let’s get started …

PHP is used in conjunction to HTML or XHTML and other flavors of hyper-text-markup languages. PHP enables static html web pages to contain dynamic content and preform many usefull functions when it comes to user interactivity. For a full resource guide on PHP please visit PHP Resources for a more indepth look at PHP syntax and functions. The most common reason to use PHP in your web pages is the ability to interact with databases such as SQL.

SQL is beyond the scope of this post however I plan to add another section to cover SQL in the near future.

Anyone who has ever programmed anything is probably familiar with the “Hello World” program. It is usually the first thing you learn to do when learning a new language. So here is how to write a simple “Hello World” program using PHP.

To initiate a PHP script you first tell the browser that you are using PHP with the PHP script open command < ?php will tell the browser that any code after the and the parser will stop parsing the code as PHP and render the rest of the document as standard HTML.

Hello World

< ?php

echo “Hello World”;

?>

In the above script we used the PHP begin tag < ?php the echo command and the php close tag ?> .

The echo command will echo the data between the quotes to the screen.

Strings - php like many other scripting languages offers the ability to store information in a string. A string is simply some unique name begining with the $ by placing a $ before the string name it tells php to create a string using the name following the $ . To define a string you would simply write.

< ?php

$mystring = “Hello World”;

?>

Ok so lets take a look at what has been done here. We simply made a string called mystring and gave it a value of Hello World then we terminated using the ever so familiar ;

Now that we have a variable defined let’s use it to run a script.

< ?php

$mystring = “Hello World”;

echo = $mystring ;

?>

The above script would return Hello World to the screen where ever you place the code.

Pretty basic so far right. Ok so what if you have more than one string and you would like to echo them both to the screen using one echo statement. You would do in programming what is called concate the two strings using the . to concate the two strings. Example

< ?php

$firstname = “Tim”;

$lastname = “Matheson”

echo $firstname . $lastname;

?>

The above script would return Tim Matheson to the screen at the location of the script within the document. Now moving forward say we want to echo a string and some text at the same time. Simple just use the ” ” and the . to seperate each piece of information. Example

< ?php

$firstname = “Tim”;

$lastname = “Matheson”;

echo “Hello” . $firstname . ” ” . $lastname . ” ” . “, welcome to my PHP mini tutorial.”;

?>

Lets take our echo statement and break it down first echo tells php to put the contents to the screen. Anything in quotes will be displayed as text so dont quote your strings or you will get plain text output to the screen. (i.e. echo “$firstname” ; returns $firstname but echo $firstname; would return Tim) Ok moving forward the period’s seperate strings and text and are required for the script to work. The period will concate two strings or two pieces of text together. However to concate two pieces of text together would be a bit redundant. (i.e. echo “Hello” . “World” ;) would work but could be stated as echo “Hello World”; and save some kilobytes in the file size. Remember the larger the file the longer the parse time and the longer the delay from server to client. There for save yourself the agony of long parse times and also write clean code and only use whats needed.

Well that about wraps it up for this tutorial next time I will get into $_POST['string']; and $_GET['string']; and how to use them in conjunction to a form.

I hope you enjoyed this tutorial and found it usefull. If so please provide a link to my site so that others can find it usefull also.

Happy Coding Cheers!

JavaScript Add Dashes to phone numbers in forms ( Auto Hyphen Script)

Saturday, February 4th, 2006

Ok I am somewhat a novice when it comes to JavaScript. I just never really saw any reason to learn JavaScript. Then one day I was developing a form for a client and they said why am I getting thousands of blank emails? I quickly investigated the matter only to find that spam bots and automated sign up software running on a users machine was generating the redundant anoying emails. So what do you do to solve this problem? JavaScript to the rescue.

With JavaScript you can use a function to do almost anything you can think of. For the most part I use alot of JavaScript in my forms for validation and checking if fields are empty. However I recently found a task that no one on the internet seemed to know anything about. I wanted a form to add dashes to a phone number when a user clicked a button or blurred the element. So I wrote one and decided to share it with you. Here it is…

Enter a telephone number :

Basically what it does is takes the input of the field and stores it in a variable. I then start to break that variable into smaller variable. Those of you who have never programmed before. A variable is just a container for some value, property or other piece of information to be stored. So anywhay i then use the JavaScript command substr (short for sub string) example variable phone has the value of the phone number field stored in it. I want to create a new variable with the first three numbers of the field. How would I do that? Well this is where the command substr comes in to play. Simply define a new variable called areacode or whatever like so

var areacode = phone.substr(0,4);

Phone has a stored value of whatever the phone field data was. By using the substring command we grab the first three digits from that value. If the value was Hello World the new variable areacode would contain “Hel” . So if i only want the first three numbers why do I use 4 for the ending number? Well the last number is not used when indexing a substr command. So you would use the number immediatly after the one you wish to be last in your new variable. I then do that for the prefix and then of course the last four of the number. Once again I create a variable called final and define it like so…

var final = areacode + “-” + prefix + “-” lastfour;

What this does is simply take all the variables we made with all the pieces of information back together. You will notice the + “-” that adds in the dashes which are the real reason why I wrote this script in the first place.
Click here to download my JavaScript file to see the script.

  • Javascript Objects Used
  • subString();
  • var
  • Alert()