函数详解php运行环境配置phpini配置及php基础教程

php环境套件  时间:2021-02-28  阅读:()

资料内容仅供您学习参考如有不当或者侵权请联系改正或者删除。

详解php运行环境配置php. ini配置及php基础讲解

1、 PHP变量及数据类型

1) $variable ,变量以字母、 _开始,不能有空格

2) 赋值$variable=value;

3) 弱类型,直接赋值,不需要显示声明数据类型

4) 基本数据类型: Integer, Double,String,Boolean,Object(对象或类) ,Array(数组)

PHP片段四种表示形式。

标准tags: <?php ?>short tags: <? ?>需要在php. ini中设置short_open_tag=on,默认是onasp tags: <% %>需要在php. ini中设置asp_tags=on,默认是offscript tags: <script language=” php” ></script>

5) 特殊数据类型: Resourse(对第三方资源(如数据库) 的引用) , Null (空,未初始化的变量)

3、 操作符

1) 赋值操作符: =

2) 算术操作符: +, -, *, /, %(取模)

3) 连接操作符: . ,无论操作数是什么,都当成String, 结果返回String4) Combined Assignment Operators合计赋值操作符: +=, *=, /=, -=, %=, .=

5) Automatically Incrementing and Decrementing自动增减操作符:

( 1) ++$variable, -$variable,先++或-,再做其它操作

( 2) $variable+=1 <=>$variable++; $variable-=1 <=>$variable-,跟c语言一样,先做其它操作,后++或-

6) 比较操作符: = =(左边等于右边) , ! =(左边不等于右边) , = = =(左边等于右边,且数据类型相同) , >=, >, <, <=

资料内容仅供您学习参考如有不当或者侵权请联系改正或者删除。

7) 逻辑操作符: | | ó or, &&óand, xor(当左右两边有且只有一个是true,返回true) , !

4、 注释:

单行注释: // , #

多行注释: /* */

5、 每个语句以;号结尾, 与java相同

6、 定义常量: def ine(” 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 result1break;case result

// execute this if expression results in result2break;default:

// execute this if no break statement

// has been encountered hitherto

}

3) ?操作符:

( expression )?returned_if_expression_is_true:returned_if_expression_is_false;

4) wh i l e语句:

( 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>

<?phpfunction sayHello() { //定义函数sayHelloprint "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值为1print("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>

<?phpfunction 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>

<?phpfunction 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 usemysql_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 datawhile ($newArray = mysql_fetch_array($result) ) {

// give a name to the fields

$id = $newArray[' id' ] ;

$testField = $newArray[' testField' ] ;

//echo the results onscreenecho "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: ") ;

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、 显示分类的完整例子:

HaBangNet(6.95美元/月)美国vps 5TB流量/德国vps 香港双向CN2 GIA VPS

HaBangNet支持支付宝和微信支付,只是价格偏贵,之前国内用户并不多。这次HaBangNet推出三个特价套餐,其中美国机房和德国机房价格也还可以,但是香港机房虽然是双向CN2 GIA线路,但是还是贵的惊人,需要美国和德国机房的可以参考下。HaBangNet是一家成立于2014年的香港IDC商家,中文译名:哈邦网络公司,主营中国香港、新加坡、澳大利亚、荷兰、美国、德国机房的虚拟主机、vps、专用...

LOCVPS全场8折,香港云地/邦联VPS带宽升级不加价

LOCVPS发布了7月份促销信息,全场VPS主机8折优惠码,续费同价,同时香港云地/邦联机房带宽免费升级不加价,原来3M升级至6M,2GB内存套餐优惠后每月44元起。这是成立较久的一家国人VPS服务商,提供美国洛杉矶(MC/C3)、和中国香港(邦联、沙田电信、大埔)、日本(东京、大阪)、新加坡、德国和荷兰等机房VPS主机,基于XEN或者KVM虚拟架构,均选择国内访问线路不错的机房,适合建站和远程办...

老薛主机VPS年付345元,活动进行时。

老薛主机,虽然是第一次分享这个商家的信息,但是这个商家实际上也有存在有一些年头。看到商家有在进行夏季促销,比如我们很多网友可能有需要的香港VPS主机季度及以上可以半价优惠,如果有在选择不同主机商的香港机房的可以看看老薛主机商家的香港VPS。如果没有记错的话,早年这个商家是主营个人网站虚拟主机业务的,还算不错在异常激烈的市场中生存到现在,应该算是在众多商家中早期积累到一定的用户群的,主打小众个人网站...

php环境套件为你推荐
二叉树遍历写出二叉树的先序遍历、中序遍历、后序遍历。镜像文件是什么系统镜像是什么ios7固件下载ios 7及以上固件请在设备上点“信任”在哪点?免费免费建站最好的免费建站虚拟机软件下载谁有好用的虚拟机软件?宽带接入服务器互联网的接入方式有哪几种?qq等级表谁能告诉我QQ等级列表?cisco防火墙思科路由器上自带防火墙功能吗?熊猫直播频道哪里可以查询熊猫频道每一个直播镜头是哪一只熊猫腾讯免费升级win10腾讯免费升级win10可以用多久
猫咪永久域名收藏地址 注册cn域名 全球付 网站保姆 淘宝双十一2018 typecho java空间 100m独享 网站在线扫描 drupal安装 dnspod lamp怎么读 阿里云手机官网 实惠 数据湾 weblogic部署 西部主机 linuxvi rewrite规则 vim命令 更多