Differences in syntax: 1. PHP uses the define function to define constants, but C language does not; 2. All variable names in PHP must be preceded by a "$", which is not used in C language; 3. PHP does not care about constants Neither variables need to be defined before use (except variables in classes).
The operating environment of this tutorial: windows7 system, PHP7.1&&c99 version, DELL G3 computer
Syntax, php The difference between the C language and It is implemented by a define function. PHP's functions are really powerful. We will see later that defining arrays is also completed by functions. We really have to thank PHP for its extremely rich function library, which makes PHP easy to use and powerful. define function prototype:
bool define(string name, value, [, bool case_insensitive])If the third parameter is TRUE, it means that the constant name is the size Write insensitive, otherwise it will be sensitive, and the default is sensitive. Let’s follow the custom and keep constant names case-sensitive.
Example:
<?php define(“C1”,”Hello PHP World!”); echo C1; ?>
The running result of the above code is to output the value of constant C1 (Hello PHP World!). By the way,
are the delimiters of PHP code, which distinguish PHP code from HTML code. PHP code The statement ends with a semicolon, which is what we are used to. In addition, PHP's comment symbols are the same as those of C. You should know that, but I won't go into details... This echo is equivalent to C's printf or C's cout, but strictly It is said that the output is not formatted. It is very easy to format it. You just need to write the HTML code. For example, if you want to break the line after the output, then you would write
define("C1","Hello PHP World !Okay. Note that PHP also supports '\n' as a newline character, but its meaning is only equivalent to hitting Enter in HTML, and it is still on one line in the browser.
Variables
In addition to not having to define it, the most unaccustomed thing to C language users is that all variable names must be preceded by a $. You may complain that PHP designers are too You are addicted to money, but after complaining, you still have to write like this. When writing PHP programs, the usage of the Shift key has increased sharply! I suddenly discovered an advantage of doing this: constants do not have $, so the distinction between constants and variables becomes obvious. More importantly, you no longer have to worry about custom variable names conflicting with PHP keywords. You can define A variable is called $if, $for, etc. (Of course, it is better not to do this in formal situations, just enjoy it).
The same as C, PHP variable names are case-sensitive, and the naming method follows the naming principles of C identifiers. Do you remember?
If you know some C and the concept of references, you can skip the following paragraph, just remember that PHP also supports references. Otherwise, take some time to read the following paragraph or take a look at the concept of C references. <?php
$var1=100;
$var2=&$var1;
$var2=200;
echo $var1.”<br>”;
echo $var2;
?>
Why only 200 is assigned to $var2, and $var1 also becomes 200? You can also try to only change the value of $var1. You will find that these two variables seem to be tied together. . In fact, the meaning of the ampersand in the statement $var2=&$var1; is to point the variable name $var2 to the storage space where $var1 is located. That is to say, $var2 is not a new variable, but another name of $var1. They correspond to the same memory space, and no matter who uses it, they access the same variable. $var2 is called a reference to $var1. What are the benefits of citing? talk about it later. If the basic concepts of C language (variable name, variable address, variable value) are clear, there should be no difficulty in understanding it. If the basic concepts are unclear, it is better to "review the past" first and then "learn the new".
By the way, an interesting thing, if you write the above echo statement as:
echo “The value of $var1 is ”.$var1.”<br>”;
, then the output you get will be:
The value of 200 is 200
Do you remember, in When you first learned C language, you probably made the mistake of putting the variable name in a string and trying to output its value, but what you got was the variable name. But now, your "ideal" has been realized, but you are not used to it, haha . It turns out that when PHP uses double quotes as the delimiter of a string, it will automatically replace the variable name with the corresponding value. This approach makes it very convenient for us to output a string containing variables. Just a pair of double quotes can . If you want to keep the variable name output as it is, you can use single quotes as the delimiter of the string (of course there are other ways, you can study PHP strings carefully). We still have to thank C for its strict requirements. If C was like PHP, we might still not be able to figure out the concepts of variable names and variable values.
Scope of variablesPHP的变量作用域与C很类似,在此不准备多说。局部变量在所存在的函数内部有效,全局变量作用域是文件作用域(限于单个PHP文件),若局部变量同全局变量重名,同C的处理方式,全局的被屏蔽。稍微麻烦一点的是,要想在函数内访问全局变量,需使用global关键字声明,否则的话PHP就认为是一个与全局变量同名的局部变量。
<?php $a=1; function func() { echo "The value of a is:".$a; } func(); ?>
以上代码没有输出1,为什么呢?PHP把函数里的$a看作是一个全新的局部变量,此$a非彼$a(函数体外的全局变量),此$a未赋值,因此输出为空。如果我们要想在函数体内部访问全局变量$a的值,须在访问之前声明一句:global $a; 这样PHP就知道要访问的是全局变量$a了。
还有一种方法是利用PHP的系统数组$GLOBALS,该数组是PHP系统自动生成和维护的,它记录了所有全局变量。echo $GLOBALS[“a”];
即可输出其中$a的值。
PHP的静态变量仅在局部函数域中存在,同C是完全相同的。
PHP有一种特殊的动态变量,名字听起来很容易让人认为就是C语言的普通变量(动态的、局部的),但这个动态的含义有点特殊,它是指变量名可以动态赋予!也就是说,可以让应用程序的最终用户为变量指定名字!
<?php $a="newname"; a="content";//newname本是$a的值,但是a的含义是以$a的值为名的变量 echo $newname; //$$a等价于 $newname echo $a; ?>
运算符和表达式
PHP在运算符和表达式方面和C极其类似,有算术、逻辑、关系、位运算符及相应的表达式,也有条件运算符(:?)及其表达式,同样还是我们开始提到的那个思想,实用为上,很多限制被取消了,相对较宽松。举个例子:
<?php $a="1"; $b=1; if ($a==$b) echo "Equal!"; else echo "Not Euqal!"; ?>
按照C的思维方式来看,$a是字符,$b是整型,两者比较是字符的ASCII码值与整数比较,显然不等,而PHP运行结果却是“Equal!”,看到这里,是不是有点冲破封建大家庭的礼教与心上人私奔的感觉?呵呵,抑或是惊呼PHP无法无天?接下来还有让你目瞪口呆的,若$a="1e3"; $b=1000; 依然是“Equal!”,也就是说PHP在比较时能自动将字符串“按照人的理解方式”进行转换,1e3不就是科学计数法表示的1000吗?甚至$a=”1e3HelloWorld”;都会相等,PHP将字符串转换为数值时会自动截取全部它能理解为数值的部分!其实C也允许跨越数据类型的比较,比如字符型和整型比较,C也会自动转换类型,但它是“按照计算机的思维方式”转换的,因为字符在计算机里就是按照ASCII码存储的。
PHP的“人性化”也带来了麻烦,当我们想严格比较两个变量的时候怎么办呢?于是PHP提供了一对特殊的运算符:全等(===)和非全等(!==),只有当值相当且数据类型相同时,才叫全等,两者有一不符,则非全等。此时,即使1与1.0也是非全等的,原因你一定知道吧。如果你觉得只有这样才踏实,那么恭喜你,你已经被C洗脑了。
运算符有优先级和结合性的概念,这方面和C是类似的,你也许记不清所有的优先级关系,但是没关系,我们有括号呢。
PHP的数据类型
PHP与C的一个显著不同是:不管常量还是变量都不需要先定义后使用(类中的变量除外),PHP根据第一次赋值的情况自动决定数据类型。想想当年学习C语言的时候,我们曾感到先定义后使用很麻烦(但是“长大后”(我是指学会C后)都体会到了它的好处),常常忘记定义,而现在少了这条规矩,反而有点不适应了。
PHP的基本变量类型有整型(Integer)、浮点型(Float)、字符串(String)(这是C没有的,但C++有String类)以及布尔型(Boolean)(C没有,但是C++有)。构造类型有数组和对象。此外还有两个特殊类型:空值(NULL)(C也有)和资源(Resource)。
推荐学习:《PHP视频教程》
The above is the detailed content of What is the difference between php syntax and c. For more information, please follow other related articles on the PHP Chinese website!