Class

Projects

  • Free MP3s on Last.fm

Sponsors

Beginning PHP

Printer-friendly versionPrinter-friendly versionSend to friendSend to friend

Beginning PHP

The syntax of PHP is very similar to that of Javascript. The difference is use, we use PHP for any backend functionality for our website: sending email, adding customers to a database, logging in to our website...

You want to be sure that the file you are writing this php code in is named .php ie: index.php, php code will not work in an .html file. You can however, have html code inside a php file. PHP also needs to run on a web server. So testing out your PHP file from your normal working folder will not work. You can test out your php code by either uploading it to a webserver or using a program like XAMPP to simulate a webserver on your own computer

Let's check out some of the very basic syntax:

Commenting:

//Just like javascript, we can have a single comment line with two forward slashes
/* Or multi-line comments by
starting with a forwardslash+asterisk

and ending with an asterisk+forwardslash */

Writing to the screen

Most of your php code will never be seen to the user, but when we do need to output results or messages, we use either the print or echo functions:

print("Hello There!<br/>");

Or

echo("Hello There!<br/>");

There is very little difference between the two, and you will see developers use both in their code. So find one that you are comfortable with but understand that both essentially do the same thing.

Variables

Variables are containers for data, we can hold numbers, strings, boolean values, as well as more complex data. Unlike Javascript, there is no need to initially define a variable, we can just begin using it. Note that every variable in PHP will have a $ symbol before it:

$myname = "Jeff";
$myfavnumber = 12;
echo($myname);

Concatenator (Combining Strings)

Use a period to combine strings:

echo("<br/>Hello ".$myname."!");

String functions

There are a few nice functions PHP has that we can use:

To find the length of a string:

echo(strlen($myname));

To find where a character appears in a string:

echo(strpos($myname,"e"));

Operators

Operators are very similar to those of javascript. We can use basic math operators such as (+,-,*,/) and condition operators such as (||-OR, &&-AND,==-EQUAL TO, !=-NOT EQUAL TO)

$newnumber = $myfavnumber+12;
echo($newnumber);

If Statements

Again, if statements in PHP are very similar to Javascript if statements:

if($myname == "Jeff"){
    echo("<br/>");
    echo("Hello Jeff!");
}else{
    echo("<br/>");
    echo("Hey, you aren't Jeff!");
}

For loops

We want to use a for loop when looping through many items. We can define how many times the code will run. There are three elements of a for loop condition (Initial Variable; When Loop Will Stop; After Each Loop). So in the below example, we define a new variable $i and set it to zero. In the next section we say "As long as $i is less than 10, keep looping". Then the last section says "After each loop, add one more to $i".

for($i = 0; $i<10; $i++){
    echo("<br/>".$i);
}

Arrays

Once an array is defined, arrayname[] will put it in the next item

$colors = array();
$colors[] = "Red";
$colors[] = "Blue";
$colors[] = "Green";
$colors[] = "Orange";

While Loop

We can easily loop through an array

foreach($colors as $color){
    echo("<br/>".$color);
}

Getting User Information

There are certain things we can find out about the user

echo("<br/>IP Address: ".$_SERVER['REMOTE_ADDR']);
echo("<br/>Browser Info: ".$_SERVER['HTTP_USER_AGENT']);

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.