CPTE 212 Rosetta Stone

Believe it or not, I don't recommend that you use this as a source of information! It's a format for organizing information about a new language. I recommend that you copy the left-hand column, then fill in the blanks for whatever language you are learning as you cover each concept.

The Rosetta Stone, now on display in the British Museum in London, unlocked an ancient language because it had the same text in both a known language and an unknown language. This document is where I'll make note of some of the differences between the two environments we are using. The format is informal - sometimes I show syntax and sometimes I show examples.

Scripting Languages

 
PHP
Script tags
<?php
?>

End of a program statement
Semicolon
Send information to the Web client
print ("Hello world");
Creating a variable
Just use it - PHP
Concatenate two strings
. (dot)
Extract from a string

substr(string,start[,length]

Note: First character of a string has an index of 0

Variable name
Starts with a dollar sign
Comment
Double-slash - //
Simple repeating loop
for(indexvar; endcondition; increment) {
...
}

Loop through an entire array

Example shown: Server variables


foreach ($_SERVER as $x => $y) {
   print "<tr><td>" . $x . "</td><td>" 
         . $y . "</td></tr>";
} 
Declare a function

function foo($arg_1, $arg_2, /* ..., */ $arg_n)
{
$retval=42;
echo "Example function.\n";
return $retval;
}

If statement

if (condition) {
...
}

While statement

while (expression) {
...
}

Include statement

In the PHP:

include (filename)

Switch
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "Wrong i"
}
Current minute of the hour
date("i")
Current hour of the day (24 hours)
date("H")
Create an array
$variable = array(value0, value1, value2...);
Address an array element
$variable[index]
Rounding
round(number[,decimalplaces])
Equal test
== (double equal sign)
Modulus
x % y
Square root
sqrt(x)
String Append
.= $string (adding to an existing string)
Escaping characters
Escape characters and sequences
e.g. print("\"Hello\"");
Length of a string
strlen()
Uppercase/Lowercase
strtoupper()/strtolower()
Trim blanks on end of a string
trim(),ltrim(),rtrim()
Change part of a string
str_replace(), str_ireplace(), substr_replace()
Break down a string based on a delimiter
str_split(),explode(),implode()
Read data from a Web site

$variable = file(url)

Note: This gives you an array of strings.

Print all values in the POST array

print "<pre>";
print_r($_POST);
print "</pre>";

This method can be easily used to print any array.

 

Quit, giving user a message


die("anything you want to tell the user")
Finding client IP address
print ($_SERVER['REMOTE_ADDR']);
Finding server address
print ($_SERVER['SERVER_ADDR']);
Read from a text file

// Read the file into an array

$filecontents = file("filename")

Current page's URL
$_SERVER['PHP_SELF'