一篇不错的PHP基础学习笔记


Posted in PHP onMarch 18, 2007

1、  PHP片段四种表示形式。
标准tags:<?php           ?>
short tags:<?              ?> 需要在php.ini中设置short _open_tag=on,默认是on
asp tags: <%             %>需要在php.ini中设置asp_tags=on,默认是off
script tags:<script language=”php”></script>
2、  PHP变量及数据类型
1)        $variable  ,变量以字母、_开始,不能有空格
2)        赋值$variable=value;
3)        弱类型,直接赋值,不需要显示声明数据类型
4)        基本数据类型:Integer,Double,String,Boolean,Object(对象或类),Array(数组)
5)        特殊数据类型:Resourse(对第三方资源(如数据库)的引用),Null(空,未初始化的变量)
3、  操作符
1)        赋值操作符:=
2)        算术操作符:+,-,*,/,%(取模)
3)        连接操作符:. ,无论操作数是什么,都当成String,结果返回String
4)        Combined Assignment Operators合计赋值操作符:+=,*=,/=,-=,%=,.=
5)        Automatically Incrementing and Decrementing自动增减操作符:
(1)$variable+=1 <=>$variable++;$variable-=1 <=>$variable-,跟c语言一样,先做其他操作,后++或-
(2)++$variable,-$variable,先++或-,再做其他操作
6)        比较操作符:= =(左边等于右边),!=(左边不等于右边),= = =(左边等于右边,且数据类型相同),>=,>,<,<=
7)        逻辑操作符:|| ó or,&&óand,xor(当左右两边有且只有一个是true,返回true),!
4、  注释:
单行注释:// ,#
多行注释:/*  */
5、  每个语句以;号结尾,与java相同
6、  定义常量:define(“CONSTANS_NAME”,value)
7、  打印语句:print,与c语言相同
8、  流程控制语句
1)        if语句:
(1)if(expression)
{
//code to excute if expression evaluates to true
}
(2)if(expression)
{
}
else
{
}
(3)if(expression1)
{
}
elseif(expression2)
{
}
else
{
}
2)        swich语句
switch ( expression )
{
case result
// execute this if expression results in result1
break;
case result
// execute this if expression results in result2
break;
default:
// execute this if no break statement
// has been encountered hitherto
}
3)        ?操作符:
( expression )?returned_if_expression_is_true:returned_if_expression_is_false;
4)        while语句:
(1) while ( expression ) 
{
              // do something
}
(2)do
{
// code to be executed
} while ( expression );
5)        for语句:
for ( initialization expression; test expression; modification expression ) {
// code to be executed
}
6)        break;continue
9、  编写函数
1)        定义函数:
function function_name($argument1,$argument2,……) //形参
{
//function code here;
}
2)        函数调用
function_name($argument1,$argument2,……); //形参
3)        动态函数调用(Dynamic Function Calls):
<html>
<head>
<title>Listing 6.5</title>
</head>
<body>
<?php
function sayHello() {   //定义函数sayHello
print "hello<br>";
}
$function_holder = "sayHello";  //将函数名赋值给变量$function_holder
$function_holder();  //变量$function_holder成为函数sayHello的引用,调用$function_holder()相当于调用sayHello
?>
</body>
</html>
4)        变量作用域:
全局变量:
<html>
<head>
<title>Listing 6.8</title>
</head>
<body>
<?php
$life=42;
function meaningOfLife() {
global $life;
/*在此处重新声明$life为全局变量,在函数内部访问全局变量必须这样,如果在函数内改变变量的值,将在所有代码片段改变*/
print "The meaning of life is $life<br>";
}
meaningOfLife();
?>
</body>
</html>
5)        使用static
<html>
<head>
<title>Listing 6.10</title>
</head>
<body>
<?php
function numberedHeading( $txt ) {
static $num_of_calls = 0;
$num_of_calls++;
print "<h1>$num_of_calls. $txt</h1>";
}
numberedHeading("Widgets");  //第一次调用时,打印$num_of_calls值为1
print("We build a fine range of widgets<p>");
numberedHeading("Doodads");  /*第一次调用时,打印$num_of_calls值为2,因为变量是static型的,static型是常驻内存的*/
print("Finest in the world<p>");
?>
</body>
</html>
6)        传值(value)和传址(reference):
传值:function function_name($argument)
<html>
<head>
<title>Listing 6.13</title>
</head>
<body>
<?php
function addFive( $num ) {
$num += 5;
}
$orignum = 10;
addFive( &$orignum );
print( $orignum );
?>
</body>
</html>
结果:10
传址:funciton function_name(&$argument)
<html>
<head>
<title>Listing 6.14</title>
</head>
<body>
<?php
function addFive( &$num ) {
$num += 5;  /*传递过来的是变量$num的引用,因此改变形参$num的值就是真正改变变量$orignum物理内存中保存的值*/
}
$orignum = 10;
addFive( $orignum );
print( $orignum );
?>
</body>
</html>
结果:15
7)        创建匿名函数:create_function(‘string1','string2'); create_function是PHP内建函数,专门用于创建匿名函数,接受两个string型参数,第一个是参数列表,第二个是函数的主体
<html>
<head>
<title>Listing 6.15</title>
</head>
<body>
<?php
$my_anon = create_function( '$a, $b', 'return $a+$b;' );
print $my_anon( 3, 9 );
// prints 12
?>
</body>
</html>
8)        判断函数是否存在:function_exists(function_name),参数为函数名
10、              用PHP连接MySQL
1)        连接:&conn=mysql_connect("localhost", "joeuser", "somepass");
2)        关闭连接:mysql_close($conn);
3) 数据库与连接建立联系:mysql_select_db(database name, connection index);
4) 将SQL语句给MySQL执行:$result = mysql_query($sql, $conn); //增删改查都是这句
5) 检索数据:返回记录数:$number_of_rows = mysql_num_rows($result);
将记录放入数组:$newArray = mysql_fetch_array($result);
             例子:
   <?php
   // open the connection
   $conn = mysql_connect("localhost", "joeuser", "somepass");
   // pick the database to use
   mysql_select_db("testDB",$conn);
   // create the SQL statement
   $sql = "SELECT * FROM testTable";
   // execute the SQL statement
   $result = mysql_query($sql, $conn) or die(mysql_error());
  //go through each row in the result set and display data
  while ($newArray = mysql_fetch_array($result)) {
      // give a name to the fields
      $id = $newArray['id'];
      $testField = $newArray['testField'];
      //echo the results onscreen
      echo "The ID is $id and the text is $testField <br>";
  }
  ?>
11、              接受表单元素:$_POST[表单元素名],
如<input type=text  name=user>ó$_POST[user]
接受url中queryString中值(GET方式):$_GET[queryString]
12、转向其他页面:header("Location: http://www.samspublishing.com");
13、字符串操作:
1)explode(“-”,str)óJava中的splite
2)str_replace($str1,$str2,$str3) =>$str1要查找的字符串,$str2用来替换的字符串,$str3从这个字符串开始查找替换
3)substr_replace:
14、session:
1)打开session:session_start(); //也可以在php.ini设置session_auto_start=1,不必再每个script都写这句,但是默认为0,则必须要写。
2)给session赋值:$_SESSION[session_variable_name]=$variable;
3)访问session:$variable =$_SESSION[session_variable_name];
4)销毁session:session_destroy();
15、显示分类的完整例子:
<?php
//connect to database
$conn = mysql_connect("localhost", "joeuser", "somepass")
or die(mysql_error());
mysql_select_db("testDB",$conn) or die(mysql_error());
$display_block = "<h1>My Categories</h1>
<P>Select a category to see its items.</p>";
//show categories first
$get_cats = "select id, cat_title, cat_desc from
store_categories order by cat_title";
$get_cats_res = mysql_query($get_cats) or die(mysql_error());
if (mysql_num_rows($get_cats_res) < 1) { //如果返回记录行数小于1,则说明没有分类
$display_block = "<P><em>Sorry, no categories to browse.</em></p>";
} else {
while ($cats = mysql_fetch_array($get_cats_res)) { //将记录放入变量$cats中
$cat_id = $cats[id];
$cat_title = strtoupper(stripslashes($cats[cat_title]));
$cat_desc = stripslashes($cats[cat_desc]);
$display_block .= "<p><strong><a
href="$_SERVER[PHP_SELF][U1] ?cat_id=$cat_id">$cat_title</a></strong>//点击此url,刷新本页,第28行读取cat_id,显示相应分类的条目
<br>$cat_desc</p>";
if ($_GET[cat_id] == $cat_id) { //选择一个分类,看下面的条目
//get items
$get_items = "select id, item_title, item_price
from store_items where cat_id = $cat_id
order by item_title";
$get_items_res = mysql_query($get_items) or die(mysql_error());
if (mysql_num_rows($get_items_res) < 1) {
$display_block = "<P><em>Sorry, no items in
this category.</em></p>";
} else {
$display_block .= "<ul>";
while ($items = mysql_fetch_array($get_items_res)) {
$item_id = $items[id];
$item_title = stripslashes($items[item_title]);
$item_price = $items[item_price];
$display_block .= "<li><a
href="showitem.php?item_id=$item_id">$item_title</a>
</strong> ($$item_price)";
[U2]                   }
$display_block .= "</ul>";
}
}
}
}
?>
<HTML>
<HEAD>
<TITLE>My Categories</TITLE>
</HEAD>
<BODY>
<? print $display_block; ?>
</BODY>
</HTML>
16、PHP连接Access:
<?  
$dbc=new com("adodb.connection");  
$dbc->open("driver=microsoft access driver (*.mdb);dbq=c:member.mdb");  
$rs=$dbc->execute("select * from tablename");  
$i=0;  
while (!$rs->eof){  
$i+=1  
$fld0=$rs->fields["UserName"];  
$fld0=$rs->fields["Password"]; 
....  
echo "$fld0->value $fld1->value ....";  
$rs->movenext();  
}  
$rs->close();  
?> 

PHP 相关文章推荐
PHP实现分页的一个示例
Oct 09 PHP
PHP 多维数组的排序问题 根据二维数组中某个项排序
Nov 09 PHP
用php实现百度网盘图片直链的代码分享
Nov 01 PHP
PHP执行批量mysql语句的解决方法
May 02 PHP
强制PHP命令行脚本单进程运行的方法
Apr 15 PHP
浅析ThinkPHP中execute和query方法的区别
Jun 13 PHP
PHP产生不重复随机数的5个方法总结
Nov 12 PHP
php curl 上传文件代码实例
Apr 27 PHP
PHP错误Warning:mysql_query()解决方法
Oct 24 PHP
thinkphp微信开之安全模式消息加密解密不成功的解决办法
Dec 02 PHP
在Mac OS的PHP环境下安装配置MemCache的全过程解析
Feb 15 PHP
PHP实现微信红包金额拆分试玩的算法示例
Apr 07 PHP
PHP_MySQL教程-第一天
Mar 18 #PHP
PHP 中dirname(_file_)讲解
Mar 18 #PHP
PHP session常见问题集锦及解决办法总结
Mar 18 #PHP
用PHP生成html分页列表的代码
Mar 18 #PHP
用PHP生成静态HTML速度快类库
Mar 18 #PHP
PHP实现采集程序原理和简单示例代码
Mar 18 #PHP
Dedecms V3.1 生成HTML速度的优化办法
Mar 18 #PHP
You might like
PHP的switch判断语句的“高级”用法详解
2014/10/01 PHP
php如何执行非缓冲查询API
2016/07/22 PHP
学习ExtJS table布局
2009/10/08 Javascript
javascript 拖放效果实现代码
2010/01/22 Javascript
javascript event 事件解析
2011/01/31 Javascript
from表单多个按钮提交用onclick跳转不同action
2014/04/24 Javascript
Jquery动态替换div内容及动态展示的方法
2015/01/23 Javascript
实例分析浏览器中“JavaScript解析器”的工作原理
2016/12/12 Javascript
详解vee-validate的使用个人小结
2017/06/07 Javascript
基于jQuery的$.getScript方法去加载javaScript文档解析
2017/11/08 jQuery
浅谈node模块与npm包管理工具
2018/01/03 Javascript
jQuery实现碰到边缘反弹的动画效果
2018/02/24 jQuery
node实现登录图片验证码的示例代码
2018/04/20 Javascript
Angularjs中date过滤器失效的问题及解决方法
2018/07/06 Javascript
nodejs初始化init的示例代码
2018/10/10 NodeJs
vue两组件间值传递 $router.push实现方法
2019/05/15 Javascript
原生js实现表格翻页和跳转
2020/09/29 Javascript
[01:05:07]DOTA2-DPC中国联赛 正赛 DLG vs Dragon BO3 第一场2月1日
2021/03/11 DOTA
Python中针对函数处理的特殊方法
2014/03/06 Python
Python高级应用实例对比:高效计算大文件中的最长行的长度
2014/06/08 Python
基于Django filter中用contains和icontains的区别(详解)
2017/12/12 Python
Python2 Selenium元素定位的实现(8种)
2019/02/25 Python
python实现QQ批量登录功能
2019/06/19 Python
基于Python词云分析政府工作报告关键词
2020/06/02 Python
通过实例解析Python文件操作实现步骤
2020/09/21 Python
KIKO MILANO英国官网:意大利知名化妆品和护肤品品牌
2017/09/25 全球购物
玉兰油美国官网:OLAY美国
2018/10/25 全球购物
类的返射机制中的包及核心类
2016/09/12 面试题
中间件的定义
2016/08/09 面试题
应届生求职简历的自我评价怎么写
2013/10/23 职场文书
运动会邀请函范文
2014/01/31 职场文书
2015年端午节活动策划书
2015/05/05 职场文书
六一文艺汇演主持词
2015/06/30 职场文书
致创业的您:这类人不适合餐饮创业
2019/08/19 职场文书
python源码剖析之PyObject详解
2021/05/18 Python
JS + HTML 罗盘式时钟的实现
2021/05/21 Javascript