PHP
Variables
Strings
Arrays
Statements
RE
OOP
Forms
Sessions
MySQL
Links
  • $str = 'Hello World!';
    $str2 = "Hello World!";
    
  • $str3 = $str2.' '.$str1;
    
  • echo strlen($str3);
    
    echo $str3[5];
    
    
    echo substr($str3, 5, 6);
    
    echo strtoupper($str3);
    
  • extension: .php, .php5, or any other extension
  • tag:<?php and ?>
    <? can also bo used as an openning tag.
  • ; indicates the end of statements.
  • Examples
    Examples from http://php.net/
    First Code variables stucture Server Variables Class Forms

  • Variables have type but do not need to be declared. PHP guesses the type as you define a variable.
  • Basic Types: Boolean, Integer, Double, String
  • string: 'Hello $Name' is different from "Hello $Name".
  • Variable Variable: $$variable or ${$variable}
  • $str = 'Hello World!';
    $str2 = "Hello World!";
    
  • $str3 = $str2.' '.$str1;
    
  • echo strlen($str3);
    
    echo $str3[5];
    
    
    echo substr($str3, 5, 6);
    
    echo strtoupper($str3);
    
  • $ar1  = array(5, 4, 6, 9, "ali");
    
    $ar2 = array(3=>5, 4=>6, 'ali'=>'Ahmad');
    
  • foreach ($ar2 as $key =>$val)
      echo "$key : $val <br/>";
    
  • asort($ar1);
    
    ksort($ar1);
    
    array_kieys($ar1);
    
    array_values($ar1);
    
    array_combine($ar1, $ar2);
    
  • If
    $i = 100;
    
    if ($i < 10)
      echo "less than";
    else
      echo "greater than";
    
  • For
    for ($i=0; $i < 10; $i++) echo $i;
    
  • While
    $i = 0;
    
    while ($i < 10){
      echo $i;
      $i++;
    }
    
  • Syntax
  • int preg_match  ( string $pattern  , string $subject  [, array &$matches  [, int $flags  [, int $offset  ]]] )
    
    int preg_match_all  ( string $pattern  , string $subject  , array &$matches  [, int $flags  [, int $offset  ]] )
    
    mixed preg_replace  ( mixed $pattern  , mixed $replacement  , mixed $subject  [, int $limit  [, int &$count  ]] )
    
  • from: php.net
    $string = 'April 15, 2003';
    $pattern = '/(\w+) (\d+), (\d+)/i';
    $replacement = '$2 $1 $3';
    echo preg_replace($pattern, $replacement, $string);
    
  • Class
    class Student
    {
        public $id = '81232';
        public function getId()
        {
           return $this->id;
        }
        
        public function setId($new_id){
          $this->id = $new_id;
        } 
    }
    
    
  • Object
      $s = new Student;
      $s->setId('82346188');
      echo $s->getId();
      echo $s->id;
    
  • Basics
    <FORM ACTION="action.php" METHOD=POST>
    .
    .
    .
    </FORM>
    
  • Variables: $_GET and $_POST
  • Cookies
  • Sessions
    session_start();
    
    $_SESSION
    
  • User Authentication
  • Basics
    Server (username, password, server)
    
    Database
    
    Table. 
    
  • Database Server
  • <?php
    $username = "root";
    $password = "";
    $hostname = "localhost";

    $dbh = mysql_connect($hostname, $username, $password) ;
    $selected = mysql_select_db("mysql",$dbh) ;

    $result = mysql_query("SELECT * FROM user WHERE 1;");

    while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    print_r($row);
    echo "<hr/>";
    }

    mysql_close($dbh); ?>
  • Official Manual
  • W3C's tutorial