php初级入门教程
In the last post we saw how can we setup PHP for Mac OS X and this is my first PHP tutorial for beginners. This tutorial is for beginners to get them started with PHP.
在上一篇文章中,我们看到了如何为Mac OS X设置PHP ,这是我的第一本面向初学者PHP教程。 本教程适用于初学者,以帮助他们开始使用PHP。
PHP初学者教程 (PHP Tutorial for Beginners)
- What is PHP? 什么是PHP?
- PHP Syntax PHP语法
- Comments in PHP PHP注释
- PHP Variables Scope PHP变量范围
- PHP String PHP字符串
什么是PHP? (What is PHP?)
PHP is a server side scripting language and is used widely for making dynamic pages. PHP stands for “PHP Hypertext Processor” and its an open source scripting language. PHP scripts are run on server and its supported by major web servers like Apache, ISS and Nginx, making it a great tool to create beautiful dynamic web pages.
PHP是一种服务器端脚本语言,被广泛用于制作动态页面。 PHP代表“ PHP超文本处理器 ”及其开放源代码脚本语言。 PHP脚本在服务器上运行,并得到Apache,ISS和Nginx等主要Web服务器的支持,使其成为创建精美动态网页的绝佳工具。
To learn PHP easily basic understanding of HTML and CSS is necessary. A PHP file contains HTML with php code that gets executed on the server and the result is a plain HTML page sent to the browser. PHP scripts can work with Files, Database and we can implement authentication and authorization for our web pages using PHP.
要轻松学习PHP,必须对HTML和CSS有基本的了解。 一个PHP文件包含在服务器上执行的带有php代码HTML,结果是发送到浏览器的纯HTML页面。 PHP脚本可以与文件,数据库一起使用,并且我们可以使用PHP对我们的网页实施身份验证和授权。
PHP语法 (PHP Syntax)
PHP script starts with <?php
and ends with ?>
and PHP file default extension is “.php”.
PHP脚本以<?php
开头,以?>
结尾,PHP文件的默认扩展名为“ .php”。
<?php
// PHP code
?>
Each statement ends with a semicolon and two basic statements used to output text is echo and print.
每个语句以分号结尾,用于输出文本的两个基本语句是echo和print 。
PHP注释 (Comments in PHP)
We can write comment in a line with two forward slashes or we can write comments in a block, syntax is shown below.
我们可以用两个正斜杠在一行中写注释,也可以在一个块中写注释,语法如下所示。
<html>
<body>
<?php
//PHP comment line
/*
PHP comment
block
*/
?>
</body>
</html>
PHP变量 (PHP Variables)
<?php
$x=3;
$y=4;
$z=$x+$y;
echo $z;
?>
The above PHP script will print 7.
上面PHP脚本将打印7。
- A variable starts with the $ sign, followed by the name of the variable like $abc 变量以$符号开头,后跟变量名称,例如$ abc
- Variable name must begin with a letter or the underscore character. Some valid names are $_abc, $a12 etc. 变量名称必须以字母或下划线字符开头。 一些有效的名称是$ _abc,$ a12等。
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ). So $a.b is not a valid variable name. 变量名称只能包含字母数字字符和下划线(Az,0-9和_)。 因此,$ ab不是有效的变量名称。
- A variable name can’t contain spaces 变量名不能包含空格
- Variable names are case sensitive ($a and $A are two different variables) 变量名称区分大小写($ a和$ A是两个不同的变量)
- We don’t need to declare the type of variable, PHP automatically converts the variable to correct data type. 我们不需要声明变量的类型,PHP会自动将变量转换为正确的数据类型。
PHP变量范围 (PHP Variables Scope)
PHP Variables has four different scopes – local, global, static and parameter.
PHP变量具有四个不同的作用域- 局部 , 全局 , 静态和参数 。
- Local Scope – If we declare a variable inside a function, then its scope is local and it can only be accessed with in the function. Local variables are deleted as soon as the function is executed.
<?php $var=5; // global scope function test() { echo $var; // local scope } test(); ?>
The above script won’t print anything because echo statement is trying to lookup the local scope variable $var that is not initialized.
局部作用域 –如果我们在函数内部声明变量,则其作用域是局部的,只能在函数中使用它进行访问。 函数执行后立即删除局部变量。上面的脚本不会显示任何内容,因为echo语句正在尝试查找未初始化的局部范围变量$ var。
- Global Scope – If the variable is declared outside of a function then its scope is global and if we want to access a global variable inside a function, we need to use global keyword.
<?php $a=5; // global scope $b=1; // global scope function test() { global $a,$b; $b=$a+$b; } echo $b; // output is 1 test(); echo $b; // output is 6 ?>
PHP also stores all the global variables in an array $GLOBALS and using variable name, it can be accessed using this array directly anywhere in the PHP script.
Above PHP script can also be written in following way;
全局范围 –如果变量在函数外部声明,则其范围是全局的,并且如果我们要访问函数内部的全局变量,则需要使用global关键字。<?php $a=5; // global scope $b=1; // global scope function test() { global $a,$b; $b=$a+$b; } echo $b; // output is 1 test(); echo $b; // output is 6 ?>
PHP还将所有全局变量存储在数组$ GLOBALS中,并使用变量名,可以使用此数组直接在PHP脚本中的任何位置访问它。
以上PHP脚本也可以通过以下方式编写;
- Static Scope – If you want a local variable not to be deleted after the function is executed, you can declare it as static. The static variable is however local to the function only and can’t be referenced in other functions. We can use static variable to count the number of times a function is executed.
<?php function test() { static $x=0; echo $x; $x++; } function test1() { echo $x; //this won't print anything because $x is static but local to test() $x++; } test(); test1(); test(); ?>
The above script will print “01”.
静态作用域 –如果您希望函数执行后不删除局部变量,则可以将其声明为静态。 但是,静态变量仅在函数本地,而不能在其他函数中引用。 我们可以使用静态变量来计算函数执行的次数。上面的脚本将打印“ 01”。
- Parameter Scope – A parameter is a local variable passed to the function by the code calling it. parameters are also called arguments.
参数范围 –参数是通过调用它的代码传递给函数的局部变量。 参数也称为参数。<?php $y=10; function test($x) //$x is a parameter and have parameter scope. { echo $x; $x=$x+1; } test($y); // passing parameter test($y); // value of $y still remains 10 test($y); ?>
PHP字符串 (PHP String)
String is a stream of characters and in PHP we can use single or double quote with value to create a String and we can assign it to any variable.
字符串是字符流,在PHP中,我们可以使用带值的单引号或双引号来创建字符串,并将其分配给任何变量。
We can use period (.) to concat two Strings. Some other useful String functions are strlen() that returns the length of the String and strpos()
that can be used to search any character or text in the String. Lets see these in below example.
我们可以使用句点(。)来连接两个字符串。 其他一些有用的String函数是strlen(),它返回String的长度,而strpos()
可以用来搜索String中的任何字符或文本。 让我们在下面的示例中看到这些。
<?php
$str1 = "Pankaj";
$str2 = 'Kumar';
echo $str1 . " " . $str2; // prints "Pankaj Kumar"
echo "<br>"; // for printing in new line, echoing HTML new line code
echo strlen($str1); // prints 6
echo strlen($str2); // prints 5
echo strpos($str1,"a"); //prints 1, index starts with 0
echo strpos($str2,'k'); //prints nothing, PHP is case sensitive
?>
Below image shows the output HTML produced when I execute it using PHP command.
下图显示了使用PHP命令执行输出时产生HTML输出。
I think it’s enough to end this PHP tutorial for beginners, in next post I will focus on PHP Operators, conditional statements, PHP Arrays and for/while loops.
我认为对于初学者来说,结束本PHP教程就足够了,在下一篇文章中,我将重点介绍PHP运算符 ,条件语句,PHP数组和for / while循环。
Reference: Official Doc
参考: 官方文件
翻译自: https://www.journaldev/1472/php-tutorial-beginners
php初级入门教程
更多推荐
php初级入门教程_PHP初学者教程
发布评论