Home  >  Article  >  Backend Development  >  php basic syntax

php basic syntax

jacklove
jackloveOriginal
2018-06-11 23:35:377450browse

Basic PHP syntax

PHP scripts can be placed anywhere in the document.

PHP scripts start with 90e37f858369a60d54cfd06e99e91084:

5012bde1433cf467e4a49490c0661f6f

The default file extension for PHP files is ". php".

PHP files usually contain HTML tags and some PHP script code.

PHP case sensitivity

In PHP, all user-defined functions, classes and keywords (such as if, else, echo, etc.) are not case-sensitive.

In the following example, all three echo statements are legal (equivalent):

Example

76c82f278ac045591c9159d381de2c57
100db36a723c770d327fc0aef2ce13b1
6c04bd5ca3fcae76e30b72ad730ca86d
47d7eb335630d6a53166c99c2b8066d3";
echo "Hello World!0c6dc11e160d3b678d68754cc175188a";
EcHo "Hello World!0c6dc11e160d3b678d68754cc175188a";
?>
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e
<br>

However, in PHP, all variables are valid Case Sensitive.

In the following example, only the first statement will display the value of the $color variable (this is because $color, $COLOR, and $coLOR are considered three different variables):

Example

76c82f278ac045591c9159d381de2c57
100db36a723c770d327fc0aef2ce13b1
6c04bd5ca3fcae76e30b72ad730ca86d
<?php
$color="red";
echo "My car is " . $color . "0c6dc11e160d3b678d68754cc175188a";
echo "My house is " . $COLOR . "0c6dc11e160d3b678d68754cc175188a";
echo "My boat is " . $coLOR . "0c6dc11e160d3b678d68754cc175188a";
?>
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

PHP Variables

Variables are containers for storing information:

Example

1a7ee26385b743681d7373277e135fe0

Running Example

Similar to Algebra

x=5
y=6
z=x+y

In algebra we use letters (like x) to hold values ​​(like 5).

From the above expression z=x y, we can calculate that the value of z is 11.

In PHP, these three letters are called variables.

注释:请把变量视为存储数据的容器。

PHP 变量规则:

  • 变量以 $ 符号开头,其后是变量的名称

  • 变量名称必须以字母或下划线开头

  • 变量名称不能以数字开头

  • 变量名称只能包含字母数字字符和下划线(A-z、0-9 以及 _)

  • 变量名称对大小写敏感($y 与 $Y 是两个不同的变量)

PHP 是一门类型松散的语言

在上面的例子中,请注意我们不必告知 PHP 变量的数据类型。

PHP 根据它的值,自动把变量转换为正确的数据类型。

在诸如 C 和 C++ 以及 Java 之类的语言中,程序员必须在使用变量之前声明它的名称和类型。

PHP 变量作用域

在 PHP 中,可以在脚本的任意位置对变量进行声明。

变量的作用域指的是变量能够被引用/使用的那部分脚本。

PHP 有三种不同的变量作用域:

  • local(局部)

  • global(全局)

  • static(静态)

Local 和 Global 作用域

函数之外声明的变量拥有 Global 作用域,只能在函数以外进行访问。

函数内部声明的变量拥有 LOCAL 作用域,只能在函数内部进行访问。

下面的例子测试了带有局部和全局作用域的变量:

实例

3883e15f991ee4b596ca9e43cb1fad80测试函数内部的变量:94b3e26ee717c64999d7867364b1b4a3";
  echo "变量 x 是:$x";
  echo "0c6dc11e160d3b678d68754cc175188a";
  echo "变量 y 是:$x";
} 
myTest();
echo "e388a4556c0f65e1904146cc1a846bee测试函数之外的变量:94b3e26ee717c64999d7867364b1b4a3";
echo "变量 x 是:$x";
echo "0c6dc11e160d3b678d68754cc175188a";
echo "变量 y 是:$x";
?>

PHP global 关键词

global 关键词用于访问函数内的全局变量。

要做到这一点,请在(函数内部)变量前面使用 global 关键词:

实例

b599c2f8a1ece1477c9a56e239495f00

PHP echo 和 print 语句

echo 和 print 之间的差异:

  • echo - 能够输出一个以上的字符串

  • print - 只能输出一个字符串,并始终返回 1

echo "I'm about to learn PHP!<br>";
echo "This", " string", " was", " made", " with multiple parameters.";

本文讲解了php基本语法,更多相关内容请关注php中文网。

相关推荐:

PHP获取客户端信息

PHP 时间处理<br>

php编辑用户信息<br>

The above is the detailed content of php basic syntax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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