1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
<?php
/*显示输出*/
print "Hello./n"; // int print (string);
echo "Hello", " world/n"; //echo string[ , string [ , sring] ... ];
print date("D N j G:i:s T Y/n");
/*变量*/
$first_name = "Ellie";
$last_name = "Quigley";
$salary = 125000.00;
echo $first_name," ",$last_name,"/t",$salary, "/n";
/*预定义变量*/
/*
$_GLOBALS 一个数组,包含全部全局变量
$_FILES 包含通过HTTP提交文件上载向脚本提供的变量
$_SERVER 包含服务器变量, 如REMOTE_ADDR
$_ENV 包含环境变量
$_GET 包含由GET方法发送的表单变量
$_REQUEST GET变量、POST变量和COOKIE变量的合并
$_POST 包含由POST方法发送的表单变量
$_SESSION 包含由会话模块注册的HTTP变量
$_COOKIE 包含HTTP cookie变量
*/
/*常数*/
define("PI",3.1415926);
print(PI);
print("/n");
/*数字*/
$year = 2006; //int
$product_price = 29.53; //float
$favorite_color = 0x33CC99; //int hex
$distance_to_moon = 3.844e+5;
/*字符串*/
$question = 'How are you ?/n '; // /n在单引号里不会被解释
$answer = "Fine./n";
print "question: $question /nanswer: $answer /n";
$string = Null; //NULL
/*传统数组*/
$names = array("You","Me","Him");
print "$names[0] and $names[1]/n";
$names[3] = "Her";
/*关联数组*/
$address_book = array(
"You" => "1234567",
"Me" => "7654321",
"Him" => "0000000"
);
print $address_book["You"];
print "/n";
$address_book["Her"] = "9999999";
var_dump( $address_book ); // show all the array elements
/*if*/
$coin_toss = rand(1,2);
if($cion_toss == 1) {
echo "You tossed HEAD/n";
}
elseif ($coin_toss == 2) {
echo "You tossed TAIL/n";
}
else {
echo "You tossed MID/n";
}
/*switch,while,for is identical to c.*/
/*foreach*/
/*
foreach ($array_name as $value) {
codearea;
}
foreach (array_name as $name=>$value ) {
codearea;
}
*/
$dessert = array("ice cream","cake","pudding","fruit");
foreach($dessert as $value) {
echo "Desert choice is $value <br />/n";
}
foreach($address_book as $name=>$phone) {
echo "$name, $phone <br />/n";
}
//function
function greetings($name) {
print "Welcome ~ ,$name";
}
greetings("qiangrw");
/*class is identical to c++ */
class Pet {
private $pet_name;
public
function set_name($string_of_text) {
$this->pet_name = $string_of_text;
}
function get_name() {
return $this->pet_name;
}
}
$cat = new Pet;
$cat->set_name("MIMI");
echo "/nYou cat is rightly named " , $cat->get_name(), "/n";
/*file related*/
//利用require 和include语句可以包含外部文件
/*
//用文件内容替换请求
require("copyright.inc");
//只用文件内容替换第一个请求
require_once("header.inc");
//类似于替换,但只发生在程序执行期间
include("disclaimer.inc");
//在程序执行期间只发生一次
include_once("title.inc");
$filehandle = fopen("filename","r");
$filehandle = fopen("filename","w");
$filehandle = fopen("filename","wb");
fclose($filehandle);
$string = fget($filehandle); //read a line
$char = fgerc($filehandle); // read a char
$text = fread($filehandle,$bytes); // read data block
$text = file_get_contents("filename"); // read all the content of the file
fwrite($filehandle,$string);
file_put_contents("filename",$string);
*/
//正则表达式
$result = preg_match("/needle/","looking for a needle in a haystack",$matches);
echo $result,"/t",$matches[0],"/n";
if(preg_match("/^[Nn]..dl[Ee]/","Needle in a haystack") ) {
echo "Found match./n";
}
//new_array 包含:normal , mama , man
$new_array = preg_grep("/ma/",array("normal","mama","man","plan") );
var_dump( $new_array );
//new_array 包含:plan
$new_array = preg_grep("/ma/",array("normal","mama","man","plan"),PREG_GREP_INVERT );
var_dump( $new_array );
$new_string = preg_replace("/blue/","upbeat","I'm feeling blue,blue,blue.");
echo $new_string,"/n"; //I'm feeling upbeat,upbeat,upbeat.
$new_string = preg_replace("/blue/","upbeat","I'm feeling blue,blue,blue.",1);
echo $new_string,"/n"; //I'm feeling upbeat,blue,blue.
$new_string = preg_replace("/blue/i","upbeat","I'm feeling BLue,BLUE,blue."); //i:大小写等同
echo $new_string,"/n"; //I'm feeling upbeat,upbeat,upbeat.
$new_string = preg_replace("/(Peace) and (War)/i","$2 and $1","Peace and War");
echo $new_string,"/n"; //War and Peace
$new_string = preg_replace("/5/e","6*7","He gave me 5 dollars."); //e:第二项计算后替换
echo $new_string,"/n"; //He gave me 42 dollars.
/*
^ 行首匹配
$ 行尾匹配
a.c 匹配一个a,任意单个字符及一个c
[abc] 匹配一个a或b或c
[^abc] 匹配一个不是a或b或c的字符
[0-9] 匹配一个0-9之间的单个数字
ab*c 匹配一个a,随后是一个0或者多个字母b和一个c
ab+c 匹配一个a,随后是一个或者多个字母b和一个c
ab?c 匹配一个a,随后是一个0或者一个b和一个c
*/
/*《PHP and MySQL EXAMPLE》*/
?>
|