1. 首页
  2. php

PHP – 语法概述

导读:零久将向您介绍PHP的基本语法,并使您的PHP基础变得非常重要。

PHP - 语法概述

获取PHP

PHP解析引擎需要一种方法来区分PHP代码与页面中的其他元素。这样做的机制被称为“获取PHP”。有四种方法可以做到这一点 –

Canonical PHP标签

最普遍有效的PHP标签样式是 –

<?php...?>

如果您使用此样式,您可以肯定您的标签将始终被正确解释。

短开(SGML风格)标签

短或短开标签看起来像这样 –

<?...?>

正如人们所料,短标签是最短的选项你必须做两件事之一才能让PHP识别标签 –

  • 在构建PHP时,请选择–enable-short-tags配置选项。
  • 将php.ini文件中的short_open_tag设置设置为on。必须禁用此选项才能使用PHP解析XML,因为XML标记使用相同的语法。

ASP风格的标签

ASP样式标记模仿Active Server Pages使用的标记来描述代码块。ASP风格的标签看起来像这样 –

<%...%>

要使用ASP样式标记,您需要在php.ini文件中设置配置选项。

HTML脚本标记

HTML脚本标签看起来像这样 –

<script language = "PHP">...</script>

评论PHP代码

PHP - 语法概述

一个意见是,只存在于人的阅读器和显示程序结果之前剥离出来的程序的一部分。PHP中有两种评论格式 –

单行注释 – 它们通常用于与本地代码相关的简短说明或注释。以下是单行注释的示例。

<?
   # This is a comment, and
   # This is the second line of the comment
   
   // This is a comment too. Each style comments only
   print "An example with single line comments";
?>

多行打印 – 以下是在单个打印语句中打印多行的示例 –

<?
   # First Example
   print <<<END
   This uses the "here document" syntax to output
   multiple lines with $variable interpolation. Note
   that the here document terminator must appear on a
   line with just a semicolon no extra whitespace!
   END;
   
   # Second Example
   print "This spans
   multiple lines. The newlines will be
   output as well";
?>

多行注释 – 它们通常用于提供伪代码算法,并在必要时提供更详细的解释。注释的多行样式与C中的相同。以下是多行注释的示例。

<?
   /* This is a comment with multiline
      Author : Mohammad Mohtashim
      Purpose: Multiline Comments Demo
      Subject: PHP
   */
   
   print "An example with multi line comments";
?>

PHP对空格不敏感

PHP - 语法概述

空格是您键入的内容,通常在屏幕上不可见,包括空格,制表符和回车符(行尾字符)。

PHP空格不敏感意味着它几乎不会影响你连续多少个空白字符。一个空白字符与许多这样的字符相同。

例如,以下每个将2 + 2的总和分配给变量$ 4的PHP语句都是等效的 –

$four = 2 + 2; // single spaces
$four <tab>=<tab2<tab>+<tab>2 ; // spaces and tabs
$four =
2+
2; // multiple lines

PHP区分大小写

是的,PHP是一种区分大小写的语言。试试下面的例子 –

<html>
   <body>
      
      <?php
         $capital = 67;
         print("Variable capital is $capital<br>");
         print("Variable CaPiTaL is $CaPiTaL<br>");
      ?>
      
   </body>
</html>

这将产生以下结果 –

[Variable capital is 67]
[Variable CaPiTaL is]

语句是以分号结束的表达式

PHP中的语句是任何后跟分号(;)的表达式。由PHP标记包含的任何有效PHP语句序列都是有效的PHP程序。这是PHP中的典型语句,在这种情况下,将一个字符串分配给一个名为$ greeting的变量 –

[$greeting = "Welcome to PHP!";]

表达式是令牌的组合

PHP的最小构建块是不可分割的标记,例如数字(3.14159),字符串(.two。),变量($ 2),常量(TRUE),以及构成PHP本身语法的特殊单词,如if ,等等,等等

大括号制作积木

尽管语句不能像表达式一样组合,但是您始终可以将语句序列放在语句可以放在任何位置,只需将它们括在一组花括号中即可。

这两个陈述都是等价的 –

if (3 == 2 + 1)
   print("Good - I haven't totally lost my mind.<br>");
   
if (3 == 2 + 1) {
   print("Good - I haven't totally");
   print("lost my mind.<br>");
}

从命令提示符运行PHP脚本

PHP - 语法概述

是的,您可以在命令提示符下运行PHP脚本。假设您在test.php文件中有以下内容

<?php
   echo "Hello PHP!!!!!";
?>

现在运行此脚本作为命令提示符如下 –

[$ php test.php]

它将产生以下结果 –

[Hello PHP!!!!!]
零久SEO希望您现在掌握PHP语法的基本知识,让您可以在工作中随心所欲不会被难题所困惑。

原创文章,作者:零久SEO,如若转载,请注明出处:http://www.seo0j.com/272.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注

评论列表(14条)

  • air max 97 2020年2月25日 上午11:42

    Thank you a lot for giving everyone such a breathtaking opportunity to read critical reviews from this website. It is always so pleasurable and also packed with amusement for me personally and my office acquaintances to visit your blog at least thrice per week to read through the latest items you will have. Not to mention, I’m certainly motivated concerning the special strategies served by you. Selected 1 tips on this page are certainly the most impressive I have had.

  • kyrie 5 shoes 2020年2月28日 上午6:02

    Thanks so much for giving everyone an extraordinarily superb possiblity to read from this site. It can be very beneficial and as well , stuffed with a good time for me personally and my office acquaintances to visit the blog on the least thrice a week to study the newest issues you have. Not to mention, I’m just at all times motivated with your brilliant solutions served by you. Certain 2 facts in this posting are in truth the most beneficial we have had.

  • vapormax 2020年3月1日 下午11:12

    I am just writing to let you be aware of what a beneficial discovery our child enjoyed browsing your blog. She mastered a lot of pieces, which include how it is like to have a great coaching character to let many others without difficulty completely grasp a variety of grueling things. You undoubtedly surpassed my expected results. Thank you for presenting those practical, dependable, edifying and even unique thoughts on this topic to Lizeth.

  • converse outlet 2020年3月4日 上午10:38

    Thank you for your entire labor on this site. Ellie takes pleasure in doing investigations and it’s really easy to see why. I notice all concerning the compelling mode you offer helpful secrets on this blog and even welcome contribution from other individuals on that matter then my child is certainly studying a whole lot. Have fun with the rest of the new year. You are always doing a really good job.

  • kobe shoes 2020年3月7日 上午3:32

    I simply wanted to write a brief word so as to say thanks to you for the marvelous recommendations you are placing here. My time intensive internet look up has now been recognized with really good insight to talk about with my relatives. I ‘d point out that most of us visitors are unquestionably fortunate to be in a great community with very many brilliant people with beneficial tactics. I feel extremely fortunate to have encountered your entire web page and look forward to so many more awesome times reading here. Thanks a lot once again for all the details.

  • westbrook shoes 2020年3月9日 上午10:50

    I actually wanted to type a brief remark in order to express gratitude to you for all the wonderful ideas you are writing at this website. My long internet search has at the end been compensated with really good facts and techniques to share with my guests. I ‘d mention that we website visitors are rather blessed to dwell in a fine website with very many awesome professionals with insightful tricks. I feel somewhat lucky to have encountered your website page and look forward to many more thrilling moments reading here. Thanks a lot once more for all the details.

  • adidas nmd 2020年3月13日 上午3:42

    I simply wished to appreciate you again. I am not sure the things that I could possibly have created in the absence of the entire creative concepts documented by you regarding such concern. It had been a intimidating issue in my circumstances, however , coming across a new expert strategy you treated it made me to jump for contentment. I am just thankful for your assistance and as well , have high hopes you realize what an amazing job you have been undertaking teaching some other people all through your webblog. I am certain you’ve never met all of us.

  • moncler 2020年3月16日 上午3:15

    I as well as my guys have been checking the good hints found on your web site and then got an awful feeling I had not thanked you for those tips. All the guys were definitely totally warmed to see all of them and now have without a doubt been taking advantage of them. Appreciate your actually being considerably helpful and also for deciding upon these kinds of great subject areas millions of individuals are really needing to be aware of. My personal sincere regret for not expressing gratitude to you earlier.

  • canada goose 2020年3月18日 下午12:28

    I want to express appreciation to you just for bailing me out of this type of matter. Just after surfing through the the net and finding techniques which are not pleasant, I was thinking my life was over. Existing devoid of the strategies to the problems you’ve fixed by means of your site is a crucial case, as well as the kind that might have negatively affected my entire career if I had not encountered your website. The talents and kindness in controlling the whole lot was very helpful. I’m not sure what I would have done if I had not discovered such a step like this. It’s possible to now relish my future. Thanks a lot so much for the reliable and effective guide. I will not be reluctant to endorse the website to any person who will need recommendations about this subject matter.

  • adidas yeezy 2020年3月21日 上午7:06

    I am also commenting to make you understand of the notable discovery my child undergone studying your blog. She picked up such a lot of things, most notably what it is like to possess a great teaching mindset to make men and women without hassle fully grasp a number of very confusing subject areas. You truly did more than our expected results. Thanks for rendering such necessary, healthy, educational not to mention cool thoughts on your topic to Janet.

  • off white hoodie 2020年3月24日 上午4:09

    I am just writing to make you know what a cool discovery my daughter found studying your webblog. She learned several pieces, with the inclusion of what it is like to have a wonderful coaching spirit to make many more just thoroughly grasp several extremely tough topics. You undoubtedly exceeded people’s expectations. Thank you for distributing such necessary, dependable, informative and unique thoughts on the topic to Lizeth.

  • curry 7 shoes 2020年3月26日 下午3:29

    I wish to express appreciation to this writer for rescuing me from this incident. Just after checking through the world wide web and meeting views which were not powerful, I assumed my entire life was well over. Being alive devoid of the strategies to the difficulties you’ve resolved through your main posting is a crucial case, as well as the kind that would have badly affected my entire career if I hadn’t encountered the website. Your ability and kindness in dealing with all the things was useful. I’m not sure what I would’ve done if I hadn’t discovered such a subject like this. It’s possible to at this point look ahead to my future. Thanks a lot very much for this professional and results-oriented guide. I won’t think twice to endorse the blog to anyone who would like direction about this situation.

  • supreme new york 2020年3月30日 上午4:22

    I as well as my guys have already been checking the best helpful tips on your site while instantly got an awful feeling I had not thanked the site owner for those secrets. My men had been joyful to see them and now have simply been taking advantage of those things. Appreciate your simply being well helpful as well as for considering variety of extraordinary subject areas most people are really wanting to be aware of. My very own sincere apologies for not expressing appreciation to earlier.

  • nike max shoes 2020年4月2日 上午4:01

    I want to express my affection for your kindness in support of persons who really want guidance on this theme. Your real commitment to passing the solution around had been unbelievably insightful and have specifically encouraged men and women just like me to reach their objectives. Your personal important key points means a lot a person like me and somewhat more to my mates. Warm regards; from everyone of us.

联系我们

15555333984

在线咨询:点击这里给我发消息

邮件:lxa6@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code