GlobalTraq 2006

DEVELOPER TOOL KIT

Welcome to our Developer Tool Kit!

Below is a resource of tools and explanations from various aspects of software development.

What you will find below is beneficial to programmers and developers of all experience levels.

Enjoy!

What is PHP
PHP Functions
Connecting to MySQL
Writing Functions
HTTP Status Codes Explained

................................................................

What is PHP?

PHP is a scripting language that is often imbedded into HTML to add functions HTML alone can't do. PHP allows you to collect, process and utilize data to create a desired output.

Essentially, it allows you interact with your pages.
PHP is able to pre-form a number of tasks including: printing data, making numeric calculations (such as addition or multiplication), making comparisons (which is bigger, are they equal, etc) and making simple choices.
From this you can create more complex loops and functions to make your page generate more specialized data.

Basic PHP Syntax

PHP is a server side scripting language used on the Internet to create dynamic web pages. It is often coupled with MySQL, a relational database server that can store the information and variables that the PHP files may use.

Together, they can create everything from a simple web site to an interactive web forum.

The Basics:

  • Create a blank file using any program that can save in plain text format.
  • Save your file as a .PHP file,
  • Saving a page with the .php extension tells your server that it will need to execute the PHP code.
  • Enter the statement <?php to let the server know that there is PHP code coming up.
  • Enter the body of our PHP program.
  • Enter the statement ?> to let the browser know the PHP code is done.
  • Every section of PHP code starts and ends by turning on and off PHP tags to let the server know that it needs to execute the PHP in between them.

Here is an example:

<?php //on
//and
//off ?>

Everything between the “?”s is read as PHP code. The <?php statement can also be phrased as simply <? if desired.

Anything outside of these PHP tags is read as HTML, so you can easily switch between PHP and HTML as needed

PHP Functions

A function is something that performs a specific task. People write functions if they plan on doing the same task repeatedly. This allows you to only write the code once, saving time and space.

<?php
$a = abs(-.43);
$b = sqrt(16);
$c = round(12.3);
print "The absolute value of -.43 is " .
$a . "<br>";
print "The square root of 16 is " . $b . "<br>";
print "12.3 rounded is " . $c . " and 12.5 rounded is " . round(12.5);

This gives an example of three functions; absolute value, square root, and rounding.

You can use the function in the print statement or assign it to a variable.

The Mail () Function

Many websites offer a way for you to send them an email from a form on their site.

Providing the form as opposed to simply listing your email address serves two purposes:

  1. This form lets the website owner decide what information it is important to collect and prompts the users to fill in the answers to each of their questions.

  2. If you list your email directly on your site it can be picked up by bots designed to 'farm' email addresses. What that means for you is SPAM. Nobody likes to have their inbox flooded with SPAM, and using a form can help prevent that.

mail ( "me@mysite.com", "Contact Us Form", "This is an email from your site", "From: you@yoursite.com" )

WHILE Loops

In PHP there are several different types of loops. What a loop does is evaluate if a statement is true or false.

If it is true, it executes some code and alters the original statement. It will then start all over again by re-evaluating it.

It will continue to loop through the code like this until the statement becomes false.

<?php
$num = 1;
while ( $num <=10 )
{
print $num . " ";
$num++;
}
?>

Here is an example in its simplest form:

What it does: While a number is greater than or equal to 10 it prints the number. The ++ adds one to the number, however this could also be phrased as $num = $num + 1;
Once the number becomes greater than 10, it stops executing the code within the {brackets}

FOR Loops

A FOR loop is similar to a WHILE loop in that it continues to process a block of code until a statement becomes false.

However in FOR Loops, everything is defined in a single line.

The basic structure for a FOR loop is:
for ( start; conditional; increment) { code to execute; }

Using the previous for the numbers 1 through 10, do the same thing using a FOR loop

<?php
$num = 1;
while ( $num <=10 )
{
print $num . " ";
$num++;
}
?>

The FOR loop can also be used in conjunction with a conditional:

<?php
for ($num=1; $num <= 10; $num++ )
{
if ($num < 5)
{
print $num .

" is less than 5 <br>";
}
else
{
print $num . " is not less than 5 <br>";
}
}
?>

Other PHP Functions

date(), mail() and more

date()
This function returns a local time (date) formatted according to the parameters within the format string.

$date = date("Y-m-d");
print "Today is: $date";

An example:

The script would output: Today is: 2001-06-19 assuming today is June 19th, 2001.

mail()
An example:

$email = youremail@server.com;
mail("$email", "PHP e-mail example.", "Message goes here.");

The above script will send an e-mail to youremail@server.com with the PHP e-mail example in the subject line of the e-mail message. The Message goes here as the actual message.

The mail() function will work only if SENDMAIL is installed on your server. Contact your host before using this function.

mysql_connect()

An example:

$hostname = "localhost:3306";
$username = "yourusername";
$password = "yourpassword";
$mysql_connect($hostname, $username, $password);

In order to connect to MySQL database you must know the hostname, username and password of the database.

The next function closes the last opened database:
mysql_close()
Create your first table in the database using the following complete PHP script:

<?php

$dbname = "databasename";
$tablename = "mytable";

mysql_connect("$hostname", "$username", "$password") or die("Unable to connect to database");

@mysql_select_db("$dbname") or die("Unable to select database");

$query = "CREATE TABLE $tablename (name VARCHAR(25), email VARCHAR(25))";

$result = mysql_query($query);

print "Table created<BR>";

mysql_close();
?>

Connecting to MySQL

Interacting with MySQL makes PHP a more powerful tool. Below is some of the most common ways PHP interacts with MySQL.

To follow along, create a database table by executing this command:

CREATE TABLE friends (name VARCHAR(30), fav_color VARCHAR(30), fav_food VARCHAR(30), pet VARCHAR(30));
INSERT INTO friends VALUES ( "Rose", "Pink", "Tacos", "Cat" ), ( "Bradley", "Blue", "Potatoes", "Frog" ), ( "Marie", "Black", "Popcorn", "Dog" ), ( "Ann", "Orange", "Soup", "Cat" )

This will create a table that has friends' names, favorite colors, favorite foods, and pets to work with.

The first thing to do in the PHP file is connect to the database. Do this using this code:

<?php
// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
?>

You will replace server, username, password, and Database_Name with the information relevant to your site. If you are unsure what these values are, contact your hosting provider.

Writing Functions

While having pre-made functions is useful, for most things you are going to want the flexibility to write your own custom functions. A function you create takes this shape: function functionname () { your code }
Below is an example of two simple functions:

<?php
function examplefunction ()
{
print "Hi, I'm a Function <br>";
}
function sqr( $num )
{
$NumSqr = $num * $num;
return $NumSqr;
}
Print "Sample Line 1 <br>";
examplefunction();
Print "Sample Line 3 <br>";
$a = 9;
$b = sqr( $a );
Print $a . "^2 = " . $b;
?>

As seen above, the code defined between the { and } is all executed when calling the function below.

In the first example (examplefunction) the function was used to simply print text. In the second function (sqr) you needed an operand to complete the math within the function.

Put this inside the parenthesis () after the function name. Then, add in the RETURN line, so that the value is outputted to the place in the code where you originally called the function.

 





spacer Careers Home

spacer Next Step
spacer Apply for a job

spacer Applicant Resources
logo Learn More About Endai
spacer
logo Messages from our
Leadership Team
logo

Developers Tool Kit

logo

Our Office Space

pen Endai Newsletter
spacer

Submit your question:
Name:
Email:
Type your question
   
 



Site © 2008 Endai Worldwide • 217 Water Street • Third Floor • New York, NY 10038 • 1-866-897-MKTG
Privacy PolicyContact UsBecome an Affiliate Sitemap