Windows安装PHP及在VScode中配置插件,使用PHP输出HelloWorld

安装PHP

PHP官网下载地址(8.3版本):PHP For Windows:二进制文件和源代码发布

点击下载.zip格式压缩包:

历史版本在Old archives中下载。推荐在Documentation download中下载官方文档,方便学习。

下载完成后在一个顺眼的地方解压压缩包,随后将PHP文件夹的路径添加进系统环境变量中的path中。

使用 win + r 快捷键打开 cmd ,输入以下代码查看是否安装成功

php 复制代码
php -v

我使用的是以前安装的7.3.4版本,没有更新

VScode配置PHP环境

随后即可打开VScode,在拓展中下载所需要的插件:Code Runner 和 PHP Server

设置下web服务器的端口号为80 ,因为http协议使用的tcp端口号是80。钱师傅一直惦记的modbus-tcp用的是502端口,在协议层无需校验码,格式是01(设备地址:1),03(功能码:3),0001(寄存器地址:1),0002(寄存器数量:2),modbus-RTU协议会多出循环冗余校验码(CRC码)比如 55 FE。

配置完插件后会在右上角显示一个gay蓝色的图片,这时重启下VScode

重启VScode后新建一个名为GGBond.php的文件,在里面输入:

php 复制代码
<?php
phpinfo();
?>

在一个顺眼的地方右键,随后点击PHP Server:Server project

在本地浏览器出现以下界面表示配置成功

随后在VScode空白处右键点击 PHP Server:Stop server关闭服务,即可再运行其他PHP代码。

PHP输出Hello World

新建一个.php文件,php文件中可以加如HTML、CSS、Javascript代码,服务器会将文件视为PHP脚本,并解析其中的PHP代码。

在VScodez中可以在文件开头输入一个 ,随后再按tab 键,这样可以快速构造一个HTML的基本结构,使用快捷键 ctrl + / 可以便捷的添加注释。

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GGBond</title>
    <!-- 网页标题是GGBond -->
</head>
<body>
    
</body>
</html>

在里面插入一些CSS代码来润色下网页中显示的"hello world",随后整点花活,建立一个文本框和按钮,在php代码中插入Javascript代码,用来实现一下检测用户在文本框中是否输入了字符串"helloworld",如果用户输入了含有"helloworld"的字符串,会弹出一个提示框显示用户输入的字符串。

php 复制代码
<!DOCTYPE html>  
<html lang="en">  
<head>  
<meta charset="UTF-8">  
<meta name="viewport" content="width=device-width, initial-scale=1.0">  
<title>GGBond</title>
<!-- 钱师傅每天都爱看的GGBond当做标题 -->
<style>
  /* 来点css润色下 */
  @keyframes rotate {  
    from {  
      transform: rotate(0deg);  
    }  
    to {  
      transform: rotate(360deg);  
    }  
  }  
  
  .container {  
    width: 200px;  
    height: 200px;  
    margin: 50px auto;  
  }  
  
  .circular-text {  
    animation: rotate 5s linear infinite;  
  }  
  
  .textPath {  
    font-family: Arial, sans-serif;  
    font-size: 24px; /* 文字大小和颜色 */  
    fill: pink;  
  }  
</style>  
</head>  
<body>  
  
<div class="container">  
  <svg viewBox="0 0 200 200" width="200" height="200" class="circular-text">  
    <path id="circle" d="M100,100 a50,50 0 1,0 100,0 a50,50 0 1,0 -100,0" fill="transparent" />  
    <text>  
      <textPath xlink:href="#circle" class="textPath" startOffset="50%">  
        钱师傅硬邦邦  
      </textPath>  
    </text>  
  </svg>  
</div>  
  
<form action="" method="post">  
    <textarea name="user_input" rows="4" cols="50"></textarea><br>  
    <input type="submit" name="submit" value="Check">  
</form>  
  
<?php  
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) {  
    $user_input = $_POST['user_input'];  
  
    if (strpos(strtolower($user_input), 'helloworld') !== false) {  
        // 里面还有javascript  
        echo "<script>  
                function showAlert(message) {  
                    alert(message);  
                }  
                showAlert('输入里面有: " . json_encode($user_input) . "');  
              </script>";  
    } else {   
        echo "奥利给,干了兄弟们";  
    }  
}  
?>  
  
</body>  
</html>

运行结果

只输出"Hello World"的方式

php 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>奥利给</title>
</head>
<body>

<?php echo "Hello World";?>

</body>
</html>

纯PHP代码的实现方式,点击运行按钮可以在终端输出

php 复制代码
<?php
echo "hello world";
?>

网页显示结果

终端运行结果

PHP可以在代码中取消结束标记**?>**符号,省略结束标记能避免意外的空白字符或其他输出被发送到浏览器,从而减少可能会出现的一些乱七八糟的问题。

php 复制代码
<?php
echo "hello world";
相关推荐
ServBay21 小时前
告别面条代码,PSL 5.0 重构 PHP 性能与安全天花板
后端·php
JaguarJack3 天前
FrankenPHP 原生支持 Windows 了
后端·php·服务端
BingoGo3 天前
FrankenPHP 原生支持 Windows 了
后端·php
JaguarJack4 天前
PHP 的异步编程 该怎么选择
后端·php·服务端
BingoGo4 天前
PHP 的异步编程 该怎么选择
后端·php
JaguarJack5 天前
为什么 PHP 闭包要加 static?
后端·php·服务端
ServBay6 天前
垃圾堆里编码?真的不要怪 PHP 不行
后端·php
用户962377954486 天前
CTF 伪协议
php
BingoGo8 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack8 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端