You must understand PHP usage and operation (with detailed tutorial)

慕斯
Release: 2023-04-10 07:20:01
forward
23089 people have browsed it

For many people who want to learn PHP, they don’t know where to start or how to learn. Today’s article will lead you to learn how to create PHP files and how to run them (very suitable for beginners!!!) Come and learn!

You must understand PHP usage and operation (with detailed tutorial)

PHP files and operations

PHP files can contain HTML, JavaScript code and PHP code, in other words PHP code can be embedded in HTML documents. PHPThe file name has php as the suffix .

##The PHP code starts with "". There is PHP code in the middle. All lines of code must end with a semicolon.

When parsing a file, PHP (Hypertext Preprocessor) will look for opening and closing tags, namely , which tells PHP Start and stop parsing the code in between. This parsing method allows PHP to be embedded in various documents, and any part other than the opening and closing tags will be ignored by the PHP parser.

PHP file example: aa.php, its content is as follows:

<?php
   //这里可以写任何php代码
    echo "2*3=";
    echo 2*3;
?>
<html>
<head>
<title><?php echo "这是网页标题"; ?></title>
</head>
<body>
<?php
     //这里也可以写任何php代码
     echo  "\n";  //输出换行
echo "2-3=";
     echo 2-3;
?>
</body>
</html>
Copy after login

PHP The statement ends with a semicolon (;).

PHP supports three types of comments:

// This is a single-line comment

# This is also a single-line comment

/*This is a multi-line

## example of a comment

*/

php files can also call other .php files

include ( "File path");

or

require ("File path");

The difference is that if the require import file does not exist, the script will stop, but if it is included, it will continue to run.

I won’t give an in-depth introduction to this point here.

How to run PHP code

1. Run as a server script


This is the most traditional and main target area of ​​PHP. You need the following three things: PHP parser, web server, and web browser. The PHP code is executed on the server and the results are returned to the browser in plain HTML.

This method requires configuring the web server and PHP parser. Wamp integrates Apache/MySQL/PHP/PhpMyadmin, which saves developers from spending time on tedious environment configuration processes, and the installation is relatively simple. It is convenient for users to establish a PHP running environment on their own PC.

Step one: Download Wampserver

warmserver provides an environment for PHP to run, please download and install it. Download Wampserver address:

http://www.wampserver.com/<span style="color:#000000;"> </span><span style="color: rgb(0, 0, 0);"></span>

Step 2: Write a php file using notepad or notepad text editor. A simple php code is as follows

<!DOCTYPE html> 
<html> 
<body> 
<?php 
echo "Hello World!"; 
?> 
</body> 
</html>
Copy after login

or

<?php
echo "Hello World!"; 
?>
Copy after login

可以把这个文件暂时保存在桌面。注意文件后缀为.php,如new1.php。在使用记事本程序编辑PHP文档的过程中,需要注意保存方法和技巧。在“另存为”对话框中输入文件名称,后缀名为.php,另外,“保存类型”设置为“所有文件”即可。

第三步:打开Wampserver ,找到Wampsertver的图标,左键,找到www目录,单击

You must understand PHP usage and operation (with detailed tutorial)

You must understand PHP usage and operation (with detailed tutorial)

把刚才写的new1.php复制进去。

第四步:打开浏览器

在浏览器输入:localhost/new1.php

按Enter键就可以看到效果了

You must understand PHP usage and operation (with detailed tutorial)

二、以CLICommand Line Interface)命令行接口方式运行

这种方式,不需要任何服务器或者浏览器来运行它,只需要 PHP 解析器来执行。这种方式只解析执行PHP代码,一般用于PHP代码调试。

创建一个简单的文本文件,其中包含有以下PHP代码,并把它保存为test.php:

<?php
echo "test PHP CLI";
?>
Copy after login

为了方便应将php.exe(PHP引擎,解析器)的所在目录,加入windows的环境变量path中(见后面的附录)。在命令行提示符下运行这个程序,方法是:

Php  test.php
Copy after login

显示:

test PHP CLI

PHP.exe 参数

参数说明

-a

交互式运行Run interactively

-c

path 从path读取php的.ini文件

-n

不用读取php的.ini文件就直接运行

-m

列出经过编译的模块

-i

显示有关PHP构建的信息

-l

检查PHP脚本的句法

-s

以彩色方式显示源代码

-w

显示去掉注释之后的源代码

-h

显示帮助

在Windows中CMD和PowerShell都是命令行执行窗口,PowerShell 从 Windows7 时代开始内置于 Windows 系统当中,可以看作是微软对CMD的大升级,两者并存于 Windows 系统中。

在PHP交互模式(Interactivemode)里,你在命令行窗口里输入PHP代码,当你输入了所有PHP代码后,按下 Ctrl-Z 键(windows里),或按下 Ctrl-D键 (linux里),执行之并退出交互。

php -a

提示

Interactive mode enabled

这时,可输入:

<?php
     //这里可以写任何php代码
      echo "2*3=";
      echo 2*3;
      echo "\n";  //输出换行
      echo "2-3=";
      echo 2-3;
 ?>
Copy after login

 按下Ctrl-Z 键(windows里)执行之并退出交互。

You must understand PHP usage and operation (with detailed tutorial)

php大小写问题

  • 变量名(所有变量)区分的大小写,变量名以美元符($)开头

  • 常量名默认区分大小写  通常都大写

  • 函数名、方法名、类名不区分大小写,但推荐使用与定义时相同

  • 魔术常量不区分大小写 通常都大写,如__LINE__、__FILE__、__DIR__

  • NULL、TRUE、FALSE不区分大小写

  • 类型强制转换,不区分大小写

另外,顺便指出,php.ini配置项指令区分大小写

 Windows10中PATH环境变量的设置

右键 此电脑 依次单击“ 属性”,“ 高级系统设置”

You must understand PHP usage and operation (with detailed tutorial)

单击“高级”选项卡的“环境变量”,将出现如下对话框:

You must understand PHP usage and operation (with detailed tutorial)

按上图标注操作,找到“path”单击选中,再单击“编辑”,就可以编辑环境变量“path”的值。

推荐学习:《PHP视频教程

 

The above is the detailed content of You must understand PHP usage and operation (with detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!