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)

  1. What is PHP?什么是PHP?
  2. PHP SyntaxPHP语法
  3. Comments in PHPPHP注释
  4. PHP Variables ScopePHP变量范围
  5. PHP StringPHP字符串

什么是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.

每个语句以分号结尾,用于输出文本的两个基本语句是echoprint

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。

  1. A variable starts with the $ sign, followed by the name of the variable like $abc变量以$符号开头,后跟变量名称,例如$ abc
  2. Variable name must begin with a letter or the underscore character. Some valid names are $_abc, $a12 etc.变量名称必须以字母或下划线字符开头。 一些有效的名称是$ _abc,$ a12等。
  3. 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不是有效的变量名称。
  4. A variable name can’t contain spaces变量名不能包含空格
  5. Variable names are case sensitive ($a and $A are two different variables)变量名称区分大小写($ a和$ A是两个不同的变量)
  6. 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变量具有四个不同的作用域- 局部全局静态参数

  1. 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 scopefunction 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。

  2. 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 scopefunction 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 scopefunction test()
    {
    global $a,$b;
    $b=$a+$b;
    }echo $b; // output is 1
    test();
    echo $b; // output is 6
    ?>

    PHP还将所有全局变量存储在数组$ GLOBALS中,并使用变量名,可以使用此数组直接在PHP脚本中的任何位置访问它。

    以上PHP脚本也可以通过以下方式编写;

  3. 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.
    <?phpfunction 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”。

  4. 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 codeecho strlen($str1);   // prints 6
echo strlen($str2);   // prints 5echo 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.com/1472/php-tutorial-beginners

php初级入门教程

php初级入门教程_PHP初学者教程相关推荐

  1. Cocos2d-x初学者教程

    Cocos2d-x初学者教程 Cocos2d-x初学者教程 入门 分辨率设置 添加精灵 移动怪物 射击弹丸 碰撞检测与物理 画龙点睛 关于本项目在其他系统如Windows上的移植 参考资料 本文翻译自 ...

  2. mui初级入门教程(六)— 模板页面实现原理及多端适配指南

    文章来源:小青年原创 发布时间:2016-07-26 关键词:mui,webview,template,os,多端适配 转载需标注本文原始地址: http://zhaomenghuan.github. ...

  3. uni-app项目开发-----初级入门教程(从0到1制作打包自己的app)

    uni-app项目开发-----初级入门教程(从0到1制作打包自己的app) uni-app实现了一套代码,同时运行到多个平台.支持iOS模拟器.Android模拟器.H5.微信开发者工具.支付宝小程 ...

  4. 如何学习虚拟现实技术vr? vr初级入门教程开始

    vr虚拟现实技术应用广泛,是我们所有目共睹的,想要学的人越来越多,如何学习虚拟现实技术vr?千锋作为VR技术培训领先者,先给大家分享一下vr初级入门教程. 随着VR虚拟现实的爆发,越来越多的知名企业开 ...

  5. esp8266初级入门实用教程一之访问心知天气读取实时天气数据

    ESP8266作为一款功能强大的的物联网模块,已经被应用到了很多实用的物联网项目中.作为一个菜鸟,本人也捣鼓这个东西很长时间了,在捣鼓这个东西的同时我也总结了一些使用经验.在此借助CSDN平台出几个教 ...

  6. 视频教程-【吴刚】UI拟物图标设计初级入门标准教程-UI

    [吴刚]UI拟物图标设计初级入门标准教程 业内知名UID.UED.用户体验.品牌策略与创意设计师,十三年行业职业教育培训经验,业内"UI视频第一人",教学总监.视觉设计讲师. Ad ...

  7. 【吴刚】UI拟物图标设计初级入门标准教程-吴刚-专题视频课程

    [吴刚]UI拟物图标设计初级入门标准教程-200人已学习 课程介绍         本套教程在学员有基本的PS和AI软件基础的基础上,循序渐进,深入浅出,全篇干货,系统化的讲解UI拟物图标设计的特点. ...

  8. 视频教程-【吴刚】UI扁平化图标设计初级入门标准教程-UI

    [吴刚]UI扁平化图标设计初级入门标准教程 业内知名UID.UED.用户体验.品牌策略与创意设计师,十三年行业职业教育培训经验,业内"UI视频第一人",教学总监.视觉设计讲师. A ...

  9. 视频教程-【吴刚】AE(After Effects)初级入门标准教程-动画制作

    [吴刚]AE(After Effects)初级入门标准教程 业内知名UID.UED.用户体验.品牌策略与创意设计师,十三年行业职业教育培训经验,业内"UI视频第一人",教学总监.视 ...

  10. 【前端实例代码】Html5+css3创建拟物风格昏昏欲睡的云朵动画网页效果~前端开发网页设计基础入门教程~适合初学者~超简单~

    b站视频演示效果: [前端实例代码]Html5+css3创建拟物风格昏昏欲睡的云朵动画网页效果!前端开发网页设计基础入门教程!适合初学者~超简单~ 效果图: 完整代码: <!DOCTYPE ht ...

最新文章

  1. JavaSE的一些基础内容
  2. cogs 1456. [UVa 10881,Piotr's Ants]蚂蚁
  3. linux 相关的问题
  4. Codeforces 930 A. Peculiar apple-tree (dfs)
  5. 计算机分级时无法度量视频播放,Win7 64位系统电脑评分出现“无法度量视频播放性能”怎么解决...
  6. AI 脑补宋明清皇帝长相,四大模型构想真实五官
  7. 高性能MySQL读书笔记——开天辟地
  8. 纪念王盘声逝世一周年王派演唱会成功举行
  9. 很多人问中国网络安全行业怎么样?这篇文看完让你彻底了解中国网络安全行业的全景
  10. 同济第七版高数资料(教材+习题解答)
  11. js字符串分割处理的几种方法
  12. 单元三:阻抗匹配(电容电感,变压器,传输线变压器,附带硬件电路)
  13. 符号_网名特殊符号在线制作
  14. 限制input 输入框只能输入数字
  15. 人人都是网站分析师(从分析师视角理解网站和解读数据)-读书笔记4(完结)
  16. thinkphp6 验证码总是提示不正确
  17. 耗时2天,我自制了一台体感游戏机
  18. 计算机常用英语大全 (中英文对照)
  19. 每年等额本金,计算复利的方法
  20. 转:你不快乐是因为年少时得不到忘不了

热门文章

  1. input隐藏域传值给后台
  2. 微信小程序实现电商购物
  3. 中国 省市区 code码
  4. 汉字转【pinyin】
  5. bzoj 3717 [PA2014]Pakowanie
  6. 电脑桌面两个计算机图标怎么删除,电脑桌面上有个图标删除不掉|?怎么处理?
  7. 共享打印机服务器脱机状态,共享打印机脱机无法打印
  8. 搜狗输入法输出特殊符号快捷键
  9. java程序一图片为背景_利用Java处理图片,更换背景
  10. 谷歌地图网页版入口_巧用谷歌指令,利用Google Maps开发挖掘客户