Korea Hangeul Help Sitemap Calc Link KB
PHP
This page summarizes Hypertext Preprocessor (PHP), a server-side HTML embedded script language like PERL (Practical Extraction and Report Language).

Regular Expression · Function · Object & Class · Netwrok · Control · File · MySql


Gnome icon PHP BASICS
  • PHP is casesensitive. Each command ends with semi-colon (;).
  • The script begins with "<?php" and ends with "?>"
  • The // or # is used for single-line comments, while /* */ is generally used for comments in PHP.
Gnome icon REGULAR EXPRESSION

Variables

  • Predefined Variables (e.g., $_SERVER)
  • $int=3; float=3.14; $str="Name"; $bool=TRUE;
  • $x=int; $y=SQR($x);
  • echo gettype($int); echo is_int($int); echo is_string($str);
  • define("PI", "3.14"); echo PI; // definition of a constant
  • is_null()

Array

  • $grade=array(1,2,3,4); echo $grade[0];
  • $male=array("female"=>0, "male"=>1); $white=array(0=>"others" 1=>"white");
  • $score=array('Korean'=>95, 'Math'=>80);
  • unset($score["Korean"]); unset($score);
  • $computer=array("H/W"=>array("CPU", "RAM"), "S/W"=>array("OS"=>"Linux", "Appls"=>"Word"));
  • is_array()

Operators

  • Assignment: =, += (for numeric), .= (for string)
  • Arithmatic: +, - *, /, ^, %
  • Incrementing: $a++; ++$a; $b--; --$b;
  • Relational: ==, != or <>, <, <=, >, >=, === (identical), !===
  • $y=(empty($_GET['website'])) ? 'www.masil.org' : $_GET['website'];
  • Logical: && or and, || or or, !, xor
  • $str2=$str."Concatenate'; $str.="Concatenate";
  • $run=`ls -al`; echo "$run\n";
  • pointer: & /* &$var; */

Pattern Syntax

Gnome icon FUNCTIONS

String Functions

  • chr(); explode(); implode(); join();
  • echo(); print(); printf();
  • ltrim(); rtrim(); trim(); strtoupper(); strtolower()
  • str_repeat(); str_replace(); str_split();
  • strlen(); strpos(); strstr();
  • substr(); substr_count(); substr_compare(); substr_replace()
  • is_string(), is_null()

Mathematics Functions

  • abs(), floor(), ceil(), round()
  • exp(), pow(b, p), log(), log10(), sqrt()
  • sin(), cos(), tan()
  • max(), min()
  • rand()
  • ls_int(), is_float(), is_long(), is_real()
  • is_bool(), is_binary(), is_numeric()

Date Functions

  • date(); time();
  • gmdate(); getdate();
  • localtime(); mktime();

User-defined Functions

function F_name(argements) {
   (statements);
   return ...;
}

Gnome icon OBJECT AND CLASS

Class | Object

class C_name {
   (variable definition)

   function __construct() {
      (statements);
   }

   function F_name() {
      (statements);
   }
...
}

$instant = new C_name();
$instant->F_name();

class Child extends Parent {
   (variable definition)

   function __construct() {
      (statements);
   }

   function F_name() {
      (statements);
   }
...
}

Gnome icon NETWORK AND WWW

HTTP

  • Network | Http | Ftp
  • header(); setcookie(); setrawcookie();
  • setcookie ("log", $todate, time()+86400);
  • echo $HTTP_COOKIE_VARS['log'];

WWW

  • $key = $HTTP_POST_VARS['returned']
  • $ip_address=$HTTP_SERVER_VARS['REMOTE_ADDR'];
  • $domain_name=gethostbyaddr($ip_address);
  • $browser=$HTTP_SERVER_VARS['HTTP_USER_AGENT'];
$header=<<<EOD
... HTML scripts...
EOD;

print $header;

Gnome icon CONTROL STRUCTURES

The "break" and "continue" may be used in a loop to respectively end and resume execution.

if () {...}

if (score > 50) {
   statemens;
}

if () {...} else {...}

if (condition) {
   statements;
} elseif (conditon) {
   statements;
} else {
   statements;
}

while () {...}

while (condition) {
   statements;
}

while (condition) :
   statements;
endwhile;

do {...} while ()

do {
   statements;
} while (condition);

for () {...}

for (init; condition; step) {
   statements;
}

for ($i=1; $i<=100; $i++) :
   statements;
endfor;

for ($i=1; $i<=100; $i++) {
   statements;
}

foreach ($array as $value) {...}

foreach ($array as $value) {
   echo $value;
}

while ( list($key, $value) = each($array) ) {
   echo "The $key. --> $value \n";
}

switch () case

switch (condition) {
case value1:    statements;
   break;
case value2:    statements;
   break;
... default:    statements;
   break;
}

switch ($mode) :
case 1:    ...;
endswitch;

switch ($week) {
case "Monday":    ...;
}

Gnome icon File Handling (IO)

File Handling

  • basename($path); dirname()
  • chmod(); fileperms();
  • copy(); delete(); mkdir(); rename(); rmdir();
  • disk_free_space(); disk_total_space();
  • file(); feof(); file_exists()
  • fileatime(); filectime(); filemtime(); filesize(); filetype();
  • fread(); fclose(); fputs(); fwrite(); fopen();

Reading Files

require 'header.php';
require('header.php');
require $external_file;
include 'header.php';

$file = "message.txt";
$fb = fopen($file,'a+');
$content = fread($fb,filesize($file));
fclose($fb);
$record = explode("\t",$content);
$total = intval($record[0]);

Writing Files

$file = "message.txt";
$fb = fopen($file,'w');
fwrite($fb, $text);
fclose($fb);

There are eight modes of file openning: "r" and "r+" for reading, "w" and "w+" for writing, "a" and "a+" for appending, and "x" and "x+" for creating and writing. "r" places file pointer at the beginning of the file, while "a" locate the pointer at the end of file.

Gnome icon MYSQL CONNECTION

MySql Connection

$conn = mysql_connect('host', 'id', 'pw')
   or die('Error: ' . mysql_error());
mysql_select_db('database', $conn)
   or die('Connection failure');
...
mysql_close($conn);

Running Queries

$sql = 'SELECT a AS id, b, c FROM student WHILE c>=90';
$db = mysql_query($sql, $conn) or die('Query failure');
$record_num = mysql_num_rows($db);

while ( list($id, $name, $score) = mysql_fetch_array($db) ) {
   echo "$id $name $score \n";
}