search
HomeBackend DevelopmentPHP TutorialSummary of PHP basic syntax

Summary of PHP basic syntax

May 26, 2017 pm 04:06 PM
phpbasic grammar

这篇文章主要介绍了PHP基本语法总结,本文从PHP能做什么开始讲解,对PHP的语法、注释、变量、常量等内容做了总结,需要的朋友可以参考下

一、PHP能做什么?

PHP能做什么?我觉得它很强大,只要我能想到的,它都能做,只是我技术能力还不行╮(╯﹏╰)╭。好吧,一张图,基本了解一下吧(ps:PHP的功能不局限于此( ^_^ ))

图像有点模糊,凑合一下,(≧▽≦)/

二、PHP语言标记

1、结束和开始标记

1.1 :属于xml风格,是PHP的标准风格,推荐使用。

1.2 :长风格标记,不常用。若你的奇葩编辑器不支持其他php标记,就用它吧

1.3 //code ?>:简短风格,遵循SGML处理。需要在php.ini中将指令short_open_tag打开,或者在php编译时加入–enable-short-tags.如果你想你的程序移植性好,就抛弃这种风格,它就比1.1少了个php。

2、位置

怎么说呢?反正可以将PHP语言放在后缀名为.php的HTML文件的任何地方。注意了,是以.php结尾的HTML文件。

代码如下:

<html>
   <html>
        <head>
            <meta http-equiv="content-type" content="text/html;charset=utf-8">
            <!-- 在HTML标记中嵌入脚本 -->
            <title><?php echo "PHP语言标记" ?></title>
        </head>
            <!-- 在属性位置嵌入 -->
        <body <?php echo &#39;bgcolor="#ccc"&#39;?>>
            <!-- 来个高级点的吧 -->
           <?php
               if($exp){
           ?>
           <!-- 属性值中嵌入php -->
           <p align="<?php echo &#39;center&#39;?>">条件为true该做的</p>
           <?php
               }else{
           ?>
           <p>条件为FALSE该做的</p>
          <?php
               }
           ?>
       </body>
   </html>

3、注释

3.1   单行注释:// 或者 #           多行注释:/* 说明*/

3.2   多行注释不能嵌套,但是其中可以包含单行注释;单行注释也可以包含多行注释。就想这样子

代码如下:

<?php
       //echo "test";/*单行中包含多行注释符*/
        /*echo &#39;test&#39;; //多行注释符中包含单行注释符*/
    ?>

三、变量

1、变量的使用

代码如下:

 <?php
        $a = 1;   //声明一个变量a
       $b = "php";  //声明一个变量b
        $8d = 2;    //非法变量名,只能以字母或者下划线开头且不包含空格
   
        $i站点is = "php"; //合法变量名,可以使用中文
        /*
       *以下三个函数调用方式等效
       *关键字和内置函数及用户自定义的类名,函数名不区分大小写
      */
       phpinfo();
      PhpInfo();
       PHPINFO();
  
 
       /*
      *以下三个变量不一样
       *变量名是区分大小写滴
       */
      $name = "php1";
      $Name = "php2";
      $NAME = "php3";
 
     //可变变量:变量名可以动态的设置
      $hi = "hello";
      $$hi = "world";
      //以下均输出hello world
     echo "$hi $hello";
       echo "$hi ${$hi}";
     //变量赋值
      $foo = "B"  //传值赋值
     $bar = &$foo //引用赋值
      $bar = "LZ";
    echo "$foo";  //输出LZ
      $cde = $foo;  //传值赋值
      $cde = "E";
     echo "$foo";  //输出LZ
 ?>


2、变量的类型

四、常量

1、定义和使用

代码如下:

 <?php
       /*
         *boolean define(string name,mixed value[,bool case_insensitive)
         *name:常量名;value:常量值;第三个是个可选的布尔值,默认是FALSE(不区分大小写)
         */
        define("FLO",1000);
        echo FLO;          //输出1000
   
        //使用define函数检验FLO常量是否存在,存在则输出常量值
       if(define("FLO"))
       {
           echo FLO;
       }
   ?>

2、常量和变量

2.1   常量的作用域是全局的,可以再脚本的任何地方声明和访问常量。

2.2   常量前面没有$,且不能通过赋值语句定义常量。

2.3   常量一旦被定义,不能被重新定义或取消定义,直到脚本运行结束自动释放。

2.4   常量的值只能是标量(boolean,integer,float,string中的一种类型)

3、系统的预定义常量

4、常用的魔术常量

The above is the detailed content of Summary of PHP basic syntax. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?Apr 23, 2025 am 12:16 AM

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

How can you optimize PHP session performance?How can you optimize PHP session performance?Apr 23, 2025 am 12:13 AM

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

What is the session.gc_maxlifetime configuration setting?What is the session.gc_maxlifetime configuration setting?Apr 23, 2025 am 12:10 AM

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

How do you configure the session name in PHP?How do you configure the session name in PHP?Apr 23, 2025 am 12:08 AM

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

How often should you regenerate session IDs?How often should you regenerate session IDs?Apr 23, 2025 am 12:03 AM

The session ID should be regenerated regularly at login, before sensitive operations, and every 30 minutes. 1. Regenerate the session ID when logging in to prevent session fixed attacks. 2. Regenerate before sensitive operations to improve safety. 3. Regular regeneration reduces long-term utilization risks, but the user experience needs to be weighed.

How do you set the session cookie parameters in PHP?How do you set the session cookie parameters in PHP?Apr 22, 2025 pm 05:33 PM

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

What is the main purpose of using sessions in PHP?What is the main purpose of using sessions in PHP?Apr 22, 2025 pm 05:25 PM

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How can you share sessions across subdomains?How can you share sessions across subdomains?Apr 22, 2025 pm 05:21 PM

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.