Home>Article>Backend Development> PHP foundation consolidation basic syntax, variables and constants (summary sharing)

PHP foundation consolidation basic syntax, variables and constants (summary sharing)

WBOY
WBOY forward
2022-04-11 18:43:05 5165browse

This article brings you relevant knowledge aboutPHP, 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.

PHP foundation consolidation basic syntax, variables and constants (summary sharing)

Recommended learning: "PHP Video Tutorial"

Foreword:

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.

1. Basic Grammar

(1) Language tags

  • Basic tag:
  • ##Short tag:
  • Script tag:
  • ASP tag: <% //PHP code; %>

#Note : It is recommended to use unclosed basic tags for pure PHP script files: ## (2) Instruction delimiter

    PHP 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
(4) The difference between HTML and PHP

##PHP is An upgraded form of HTML language, the language structure still takes HTML as the core; HTML is a markup language used to specify web page content, while PHP is a scripting language

Web pages created using HTML are static web pages, and PHP files can create dynamic web pages.
  1. PHP files can contain text, HTML tags, scripts, etc.
  2. 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!
  3. (5) PHP line break

"
"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 supports multiple characters String output, separated by commas (,), print only supports one string output;
  • 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
  • "; echo "xiaofeng"," ","is"," ","cool","!"; ?> 
    "; 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开始

(八)基础语法实例说明

   
"; #表示换行,浏览器和源代码都显示换行。 echo "My name is xiaofeng"//最后一行可以不用 ?>
"; echo "常州加油\n常州加油\n常州加油"; //也表示换行,只是浏览器不显示换行,源代码显示换行。 ?>


二、变量

(一)变量包含的三个内容

  • 变量名
  • 变量值(变化)
  • 数据类型,变量值的类型

注:PHP脚本语言是一种弱类型语言,和其他语言不同的是变量(常量)的数据类型由程序的上下文决定(给予一个变量赋予什么样的值,就是什么样的数据类型)

(二)变量的声明和释放

"; unset($name); //unset()函数释放指定变量 echo $name;//释放了,输出肯定报错 ?>

(三)变量命名和赋值

  • 变量名严格区分大小写
  • 变量名由字母、数字、下划线组成,不能以数字开头,也不能包含其他字符(空白字符、特殊字符、空白符等等)
  • 变量命名时,最好采用驼峰式命名法或者下划线命名法,做到“见面知意

1.小驼峰,第一个单词首字母小写,之后单词首字母大写. $ userName (一般用户变量,函数,方法名)
大驼峰,每个单词首字母大写. $ UserModel (一般用于类和类文件命名)
2.下划线命名法,$ _user_name

"; $name = "zhangsan"; $name1 = "lisi"; $my_name = "xiaofeng"; $your_name = "chuhe"; echo $name,"
",$name1,"
",$my_name,"
",$your_name; ?>

注意:命名的时候最好不要使用常量函数名或者类名,PHP是可以使用的,但为了后期代码审计,建议还是不要用PHP关键字作为变量名称。

(四)可变变量$$

  • 变量名字可以动态的设置和使用
  • 由于$$是php的特性,产生变量覆盖漏洞
"; echo $name; echo "
"; echo $xiaofeng;//输出hello world! ?>

(五)变量引用赋值

简单理解为变量起了一个别名!

";//b的值变为了21 echo "a的值还是";echo $a;//a的值没变还是20 echo "
"; $c = &$a; //应用赋值 $c++; //相当于给$a起了一个别名,$c就是$a的本身 echo "c的值变为了";echo $c;echo "
"; //c的值为21 echo "a的值变为了";echo $a;//a的值也变为了21 ?>

(六)预定义变量(全局变量)

系统定义的变量,都是数组,用户可以直接使用(后期更新博客细说!)

$_SERVER

$_GET

$_POST

$_REQUEST

$_FILE

$_SESSION

$_COOKIE

$_ENV

$GLOBALS


三、变量类型(数据类型)

(一)分类

(二)整型

  • 规则
  1. 整数必须有至少一个数字(0-9)
  2. 整数不能包含逗号或空格
  3. 整数不能有小数点
  4. 整数正负均可
  • 三种格式规定整数:十进制、十六进制(前缀是 0x)或八进制(前缀是 0)

(三)浮点型

浮点数是有小数点或指数形式的数字。

(四)布尔型

$a = true; $a = false;

(五)字符串型

';echo $str; echo '
'; //双引号定义字符串 $name = "chuhe"; $str = "I'm chuhe!";//双引号定义字符串,直接输出即可 $str = "{$name},I'm chuhe";//双引号定义的字符串中出现变量名,会引用变量的值。 echo $str; echo '
'; //定界符定义字符串 $str = <<xiaofengdada

dada; //定界符结尾后面不能有任何字符,包括注释,并且闭合定界符必须位于行的开头! echo $str; ?>

定义字符串时需要注意:

  • 单引号`` :内部的内容只是作为字符串。

  • 双引号"" :如果内部是PHP的变量,那么会将该变量的值解析。如果内部是html代码,也会解析成html。

  • 定界符:第一个定界符行后不能有任何字符(包含空格),闭合定界符必须位于行的开头

(六)数组型

数组在一个变量中存储多个值。

(七)特殊数据类型——NULL

特殊的 NULL 值表示变量无值。NULL 是数据类型 NULL 唯一可能的值。NULL 值标示变量是否为空。也用于区分空字符串与空值数据库。


四、常量

(一)常量和变量的区别

  • 常量前面没有美元符号($)
  • 常量只能用 define() 函数定义,而不能通过赋值语句
  • 常量可以不用理会变量范围的规则而在任何地方定义和访问
  • 常量一旦定义就不能被重新定义或者取消定义
  • 常量的值一般是bool,int,float,string类型

(二)预定义常量

PHP已经定义好了的,可以直接使用的常量,一般代表特殊的含义。

系统常量:PHP_VERSION,PHP_INT_MAX,PHP_INT_SIZE

魔术常量:__DIR__,__FILE__,__LINE__,__CLASS__,__METHOD__,__NAMESPACE__

"; #name = "chuhe";//Parse error: syntax error, unexpected '=' echo __FILE__;echo "
";//输出当前文件名完整路径 echo __LINE__;echo "
";//输出php源码中的行号 echo PHP_OS;echo "
";//输出当前系统UNIX或者WINNT echo PHP_VERSION;echo "
";//输出当前php版本 echo DIRECTORY_SEPARATOR;echo "
";//输出操作系统决定目录的分隔符\/ ?>

推荐学习:《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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete