Home > Backend Development > PHP Tutorial > PHP:相对于C#,PHP中的个性化语法

PHP:相对于C#,PHP中的个性化语法

WBOY
Release: 2016-06-23 14:37:57
Original
887 people have browsed it

背景

今天把PHP的基本语法结构熟悉了一下,包括:变量、类型、常量、运算符、字符串、作用域和函数等,本文列举一些我需要强化记忆的结构(和C#不同)。

一些个性化的结构

foreach结构

 1 <?php  2 $array = array("a"=>1, "b"=>2); 3  4 foreach($array as $value) { 5     echo "value$value<br/>"; 6 } 7  8 foreach($array as $key=>$value) { 9     echo "$key=>$value<br/>";10 }11 ?>
Copy after login

1 value12 value23 a=>14 b=>2
Copy after login

global和static作用域

在函数外声明的变量都属于全局作用域,函数内的$global $var相当于$var = $Globals["var"]。 static作用域的变量在函数首次调用的时候执行初始化,函数执行完不会清理,后续的函数调用不会再执行初始化。

 1 <?php  2 $global_x = 0; 3  4 function scope_test() { 5     static $count = 0;     6     global $global_x; 7      8     $count++; 9     $global_x++;10     11     echo "$count, $global_x<br/>";12 }13 scope_test();14 scope_test();15 ?>
Copy after login

1 //输出内容2 1, 13 2, 2
Copy after login

变量的变量

1 <?php 2 $name = "Tom";3 $$name = "I am tom";4 5 echo $$name."<br/>";6 echo $Tom."<br/>";7 ?>
Copy after login

1 I am tom2 I am tom
Copy after login

常量定义

1 <?php 2 define("HAPPY", "幸福");3 4 echo HAPPY."<br/>";5 ?>
Copy after login

1 //输出内容2 3 幸福
Copy after login

字符串插入

1 <?php 2 $name = "Tom";3 $$name = "I am tom";4 5 echo "$Tom,${$name},{$$name}<br/>";6 ?>
Copy after login

1 I am tom,I am tom,I am tom
Copy after login

heredoc

1 <?php 2 echo <<<DOC3 段4 光5 伟6 DOC;7 ?>
Copy after login

1 //输出内容2 3 段 光 伟
Copy after login

函数级别的作用域

1 <?php 2 for($i = 1; $i <= 10; $i++) {    3 }4 5 echo $i;6 ?>
Copy after login

1 //输出内容2 3 11
Copy after login

函数返回多个结果

1 <?php 2 function muti_return() {3     return array(1, 2);    4 }5 6 list($x, $y) = muti_return();7 8 echo"($x, $y)";9 ?>
Copy after login

1 //输入内容2 3 (1, 2)
Copy after login

备注

还有一个问题我不是很明白,明天得查查资料,这个问题是:PHP的内存是如何分配和管理的?

 

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template