加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

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

发布时间:2020-05-29 00:51:01 所属栏目:PHP 来源:互联网
导读:一篇不错的PHP基础学习笔记

1、PHP片段四种表示形式。
标准tags:<?php?>
shorttags:<??>需要在php.ini中设置short_open_tag=on,默认是on
asptags:<%%>需要在php.ini中设置asp_tags=on,默认是off
scripttags:<scriptlanguage=”php”>
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)CombinedAssignmentOperators合计赋值操作符:+=,
=,/=,-=,%=,.=
5)AutomaticallyIncrementingandDecrementing自动增减操作符:
(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)
{
//codetoexcuteifexpressionevaluatestotrue
}
(2)if(expression)
{
}
else
{
}
(3)if(expression1)
{
}
elseif(expression2)
{
}
else
{
}
2)swich语句
switch(expression)
{
caseresult
//executethisifexpressionresultsinresult1
break;
caseresult
//executethisifexpressionresultsinresult2
break;
default:
//executethisifnobreakstatement
//hasbeenencounteredhitherto
}
3)?操作符:
(expression)?returned_if_expression_is_true:returned_if_expression_is_false;
4)while语句:
(1)while(expression)
{
//dosomething
}
(2)do
{
//codetobeexecuted
}while(expression);
5)for语句:
for(initializationexpression;testexpression;modificationexpression){
//codetobeexecuted
}
6)break;continue
9、编写函数
1)定义函数:
functionfunction_name($argument1,$argument2,……)//形参
{
//functioncodehere;
}
2)函数调用
function_name($argument1,……);//形参
3)动态函数调用(DynamicFunctionCalls):


Listing6.5


<?php
functionsayHello(){//定义函数sayHello
print"hello
";
}
$function_holder="sayHello";//将函数名赋值给变量$function_holder
$function_holder();//变量$function_holder成为函数sayHello的引用,调用$function_holder()相当于调用sayHello
?>


4)变量作用域:
全局变量:


Listing6.8


<?php
$life=42;
functionmeaningOfLife(){
global$life;
/
在此处重新声明$life为全局变量,在函数内部访问全局变量必须这样,如果在函数内改变变量的值,将在所有代码片段改变/
print"Themeaningoflifeis$life
";
}
meaningOfLife();
?>


5)使用static


Listing6.10


<?php
functionnumberedHeading($txt){
static$num_of_calls=0;
$num_of_calls++;
print"

$num_of_calls.$txt

";
}
numberedHeading("Widgets");//第一次调用时,打印$num_of_calls值为1
print("Webuildafinerangeofwidgets

");
numberedHeading("Doodads");/
第一次调用时,打印$num_of_calls值为2,因为变量是static型的,static型是常驻内存的/
print("Finestintheworld

");
?>


6)传值(value)和传址(reference):
传值:functionfunction_name($argument)


Listing6.13


<?php
functionaddFive($num){
$num+=5;
}
$orignum=10;
addFive(&$orignum);
print($orignum);
?>


结果:10
传址:funcitonfunction_name(&$argument)


Listing6.14


<?php
functionaddFive(&$num){
$num+=5;/
传递过来的是变量$num的引用,因此改变形参$num的值就是真正改变变量$orignum物理内存中保存的值/
}
$orignum=10;
addFive($orignum);
print($orignum);
?>


结果:15
7)创建匿名函数:create_function(‘string1','string2');create_function是PHP内建函数,专门用于创建匿名函数,接受两个string型参数,第一个是参数列表,第二个是函数的主体


Listing6.15


<?php
$my_anon=create_function('$a,$b','return$a+$b;');
print$my_anon(3,9);
//prints12
?>


8)判断函数是否存在:function_exists(function_name),参数为函数名
10、用PHP连接MySQL
1)连接:&conn=mysql_connect("localhost","joeuser","somepass");
2)关闭连接:mysql_close($conn);
3)数据库与连接建立联系:mysql_select_db(databasename,connectionindex);
4)将SQL语句给MySQL执行:$result=mysql_query($sql,$conn);//增删改查都是这句
5)检索数据:返回记录数:$number_of_rows=mysql_num_rows($result);
将记录放入数组:$newArray=mysql_fetch_array($result);
例子:
<?php
//opentheconnection
$conn=mysql_connect("localhost","somepass");
//pickthedatabasetouse
mysql_select_db("testDB",$conn);
//createtheSQLstatement
$sql="SELECT
FROMtestTable";
//executetheSQLstatement
$result=mysql_query($sql,$conn)ordie(mysql_error());
//gothrougheachrowintheresultsetanddisplaydata
while($newArray=mysql_fetch_array($result)){
//giveanametothefields
$id=$newArray['id'];
$testField=$newArray['testField'];
//echotheresultsonscreen
echo"TheIDis$idandthetextis$testField
";
}
?>
11、接受表单元素:$_POST[表单元素名],
如<inputtype=textname=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
//connecttodatabase
$conn=mysql_connect("localhost","somepass")
ordie(mysql_error());
mysql_select_db("testDB",$conn)ordie(mysql_error());
$display_block="

MyCategories


Selectacategorytoseeitsitems.

";
//showcategoriesfirst
$get_cats="selectid,cat_title,cat_descfrom
store_categoriesorderbycat_title";
$get_cats_res=mysql_query($get_cats)ordie(mysql_error());
if(mysql_num_rows($get_cats_res)<1){//如果返回记录行数小于1,则说明没有分类
$display_block="

Sorry,nocategoriestobrowse.

";
}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.="

<a
href="$_SERVER[PHP_SELF][U1]?cat_id=$cat_id">$cat_title

//点击此url,刷新本页,第28行读取cat_id,显示相应分类的条目

$cat_desc

";
if($_GET[cat_id]==$cat_id){//选择一个分类,看下面的条目
//getitems
$get_items="selectid,item_title,item_price
fromstore_itemswherecat_id=$cat_id
orderbyitem_title";
$get_items_res=mysql_query($get_items)ordie(mysql_error());
if(mysql_num_rows($get_items_res)<1){
$display_block="

Sorry,noitemsin
thiscategory.

";
}else{
$display_block.="
    ";
    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.="
  • <a
    href="showitem.php?item_id=$item_id">$item_title
    ($$item_price)";
    [U2]}
    $display_block.="
";
}
}
}
}
?>


MyCategories


<?print$display_block;?>


16、PHP连接Access:
<?
$dbc=newcom("adodb.connection");
$dbc->open("driver=microsoftaccessdriver(.mdb);dbq=c:member.mdb");
$rs=$dbc->execute("select
fromtablename");
$i=0;
while(!$rs->eof){
$i+=1
$fld0=$rs->fields["UserName"];
$fld0=$rs->fields["Password"];
....
echo"$fld0->value$fld1->value....";
$rs->movenext();
}
$rs->close();
?>

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读