Home > php教程 > php手册 > body text

php学习笔记(一)

WBOY
Release: 2016-06-06 19:42:19
Original
1286 people have browsed it

1.执行运算符是反引号 (``), 相当于 shell_exec() 函数,执行一个系统命令 $out=`dir`;//列出当前的目录和文件信息 2.定义常量 define(str,this is a const variableBR);echo str; 3.字符串 用单引号或双引号括起来。区别是在双引号中出现变量时会替换为变量

1.执行运算符是反引号(``),相当于shell_exec()函数,执行一个系统命令

$out=`dir`;//列出当前的目录和文件信息
Copy after login

2.定义常量

define("str","this is a const variable<br>");
echo str;
Copy after login

3.字符串

用单引号或双引号括起来。区别是在双引号中出现变量时会替换为变量的值。

$a=10;
echo("variable is $a");
echo('variable is $a');
//结果为:variable is 10variable is $a 
Copy after login


4、接受数据

使用变量$_POST读取表单数据。如果控件的name属性为c1,则用下面的方式获取他的值:

$_POST[`c1`]

Index.php文件

Copy after login

ShowInfo.php文件

if(isset($_POST['submit']) && $_POST['submit']){
echo("<br>用户名:".$_POST['username']."<br>");
   }
Copy after login

5、接受网址中的参数信息

在网址中传递参数,格式为:

http://网址/网页文件?参数1>=&参数2>=

接受参数:使用变量:$_GET['参数名']

<a href="ShowInfo.php?name=Johney&sex=man">submit</a>
ShowInfo.php文件
echo ("name: ".$_GET['name']."<br>");
echo ("sex: ".$_GET['sex']);
Copy after login

6、定义数组

array([key=>]value

,...

)

关键字可以是整数和字符串。关键字省略后以下一个数字为关键字,从0开始。

$arr=array("first"=>"Cpu","disk",2=>"memory","screen");
print_r($arr);
//结果:
Array ( [first] => Cpu [0] => disk [2] => memory [3] => screen )
 
Copy after login

多维数组

$arr[0][0]="CPU";
$arr[2][3]="memory";
print_r($arr);
//结果:
Array ( [0] => Array ( [0] => CPU ) [2] => Array ( [3] => memory ) )
Copy after login

7、数组函数

print_r($arr);//输出数组

$tmp=array_change_key_case($arr,CASE_LOWER);//OR CASE_UPPER

返回字符串关键字名全为小写或大写的数组

$tmp=array_count_values($arr);

统计数组中所有value出现的次数,结果返回到另一个数组中

array_fill使用指定的值填充数组

array_fill(int start_index, int num, value);

$arr=array_fill(2,2,"element");
print_r($arr);
结果为:Array ( [2] => element [3] => element )
Copy after login

bool array_key_exists(key, $arr);//检查数组中指定的关键字是否存在

$tmp=array_keys($arr [,value]);//返回一个数组,列出所有关键字;若指定value,则只返回值为value的关键字

$tmp=array_merge($arr1,$arr2,...);//将多个数组合并,若关键字相同,前面的将被覆盖

array_pop($arr);//将数组的最后一个元素弹出

array_push($arr,value1,value2,...);//将多个元素压入数组尾部,数字作为关键字

array_reverse($arr);//数组反序排列

$tmp=array_sum($arr);//元素求和

array_unique($arr);//删除数组中重复的元素

arsort($arr);//降序排序

asort($arr);//升序排序

8、遍历数组

$arr=array('a'=>'a','b'=>'b','c'=>'c',"CPU","memory",'4');
foreach($arr as $val){
echo $val." ";
}
//OR:
for($i=0;$i<sizeof echo current next><br>

<p><span>两种方法效果相同,结果为:a b c CPU memory 4 </span></p>
<p><span><strong>sizeof($arr)</strong><span>获取数组元素数量,</span><span><strong>next($arr)</strong></span><span>函数将指针移动到下一个位置,</span><span><strong>current($arr)</strong></span><span>获得当前指针下的元素。</span></span></p>
<p><span>9、变长参数列表</span></p>
<p><span>在定义函数时,不指定参数。在函数体内通过以下<span>3</span><span>个函数获得参数信息:</span></span></p>
<p><span><strong>func_num_args()</strong><span><strong>:</strong>返回参数数量</span></span></p>
<p><span><strong>func_get_arg($i)</strong><span><strong>:</strong>返回下标为</span><span>i</span><span>的参数</span></span></p>
<p><span><strong>func_get_args()</strong><span><strong>:</strong>返回参数数组</span></span></p>
<p><span> </span></p>
<pre class="brush:php;toolbar:false">function sum(){
$num=func_num_args();
$sum=0;
for($i=0;$i";
}
或者:
function sum(){
$num=func_num_args();
$sum=0;
$arg_list=func_get_args();
for($i=0;$i";
}<span> </span>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!