PHP learning every day(2)

parse_url — Parse a URL and return its components


(PHP 4, PHP

ip2long — Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address

(PHP 4, PHP 5)

list — Assign variables as if they were an array

<?php

$info 
= array('coffee''brown''caffeine');

// Listing all the variables
list($drink$color$power) = $info;
echo 
"$drink is $color and $power makes it special.\n";

// Listing some of them
list($drink, , $power) = $info;
echo 
"$drink has $power.\n";

// Or let's skip to only the third one
list( , , $power) = $info;
echo 
"I need $power!\n";

// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar); // NULL
?>


(PHP 4, PHP 5)

explode — Split a string by string

array explode    ( string $delimiter   , string $string   [, int $limit  ] )

Returns an array of strings, each of which is a substring of   string formed by splitting it on   boundaries formed by the string delimiter.

Returns an array of strings   created by splitting the string parameter on   boundaries formed by the delimiter.

If delimiter is an empty string (""),   explode() will return FALSE.   If delimiter contains a value that is not   contained in string and a negative   limit is used, then an empty array will be   returned, otherwise an array containing   string will be returned.

?php
// Example 1
$pizza  "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces explode(" "$pizza);
echo 
$pieces[0]; // piece1
echo $pieces[1]; // piece2

// Example 2
$data "foo:*:1023:1000::/home/foo:/bin/sh";
list(
$user$pass$uid$gid$gecos$home$shell) = explode(":"$data);
echo 
$user// foo
echo $pass// *

?>


str_replace — Replace all occurrences of the search string with the replacement string

mixed str_replace    ( mixed $search   , mixed $replace   , mixed $subject   [, int &$count  ] )

This function returns a string or an array with all occurrences of   search in subject   replaced with the given replace value.

If you don't need fancy replacing rules (like regular expressions), you   should always use this function instead of preg_replace().

This function returns a string or an array with the replaced values. 


Using PDO to communicates with MySQL

/***********Make an Connection*******************/

$dbh = new PDO("mysql:dbname=".$dbname.";host=".$db_host", $dbuser, $dbpasswd);

/**********Close A connection**********************/

$dbh = null;

/**************Exception****************/

PDOException $e

    $e->getMeesage();


After create a connection, we can execute the SQL commands using the connection

PDOStatement::fetch();// retrieve a row of the data from the database.

PDOStatement::fetchAll();// retrieve multiple rows of the data from the database

PDOStatement::execute() // Be used to execute the INSERT, UPDATE, DELETE query commands that don't return results.

PDOStatement::prepare();// prepare an SQL query to be executed


你可能感兴趣的:(PHP learning every day(2))