Home php教程 php手册 PHP基础教程(php入门基础教程)一些code代码

PHP基础教程(php入门基础教程)一些code代码

Jun 13, 2016 am 11:55 AM
code php one What code getting Started Base I Tutorial of

在此教程之前,我就不长篇一律的说什么PHP的常用了。 关于什么是变量呀什么是判断语句呀什么的,请自行查询相关资料此教程值针对于有编程基础,且对PHP陌生的人看。文章比较简单。主要看结构。详细的还请大家自己多多研究
PHP环境安装:
  PHP通常组合是 :MySql+PHP+Apche 也有 IIS+PHP+MySQL或SqlServer
当然我们可以进行选择组合包来进行安装。 新手建议装AppServ或phpnow等。
iis下可以用这个安装运行一下就支持php了,mysql需要安装一下。
  也可以进行自己安装各个部分。然后自己进行配置。
  PHP各版本的下载地址:http://museum.php.net/php5/
  Apche下载地址:http://prdownloads.sourceforge.net/appserv/appserv-win32-2.5.10.exe?download
  MySQL下载地址:http://www.mysql.cn/
  配置安装教程:http://wenku.baidu.com/view/c6118b1810a6f524ccbf85f9.html
      或者 http://www.jb51.net/article/33062.htm
  编写工具:建议用Notepad++或者dreamweaver cs4
====================================================================
语法:
  PHP的语法很简单 --直接看代码: 这就是PHP代码的声明方式。 注: ?> 等这中写法也可以写,但是不建议这么写。
  标记语句的结束:分号是标记一条语句的结束 ";" --每条语句结束后要用“;”分号表示结束.
=====================================================================
PHP中的注释: --详见教程中的code
  php中的注释有单行注释: //这是注释
和大模块注释:/*这是注释*/
=====================================================================
变量:
PHP变量是松散的。但是它也区分大小写,这点大家要注意。 在使用它之前,无需声明 -根据变量声明方式,PHP会自动把变量转换成正确的数据类型.
在PHP中声明变量使用$关键字来声明 --所有的变量都是由$来标识的
变量命名规则:
变量名必须以字母或下划线 "_" 开头。
变量名只能包含字母数字字符以及下划线。
变量名不能包含空格。如果变量名由多个单词组成,那么应该使用下划线进行分隔(比如 $my_string),或者以大写字母开头(比如 $myString)。
注:(基本上所有的编程语言的变量命名规则都差不多!)

示例:

复制代码 代码如下:


       //声明变量
       $var_name = "snow";
       //使用变量
       echo $var_name;
      /*
        显示结果: snow
      */
?>


常量:
  PHP中常量的声明:
    在PHP中声明常量使用define函数来声明的 。直接看code

复制代码 代码如下:


     /*
      define函数有三个参数
       第一个参数:指定常量名 --不得使用关键字,常量不能有$符号
       第二个参数:指定常量的值 --只能是布尔、整数、浮点、字符串四个类型
       第三个参数:指定此常量是否对大小写敏感 --true忽略大小写,false区分大小写
    */
     define("Name","张三",true);
     echo name;
    /*显示结果:张三 --因为是true所以不区分大小写*/
?>


PHP中也有预定义常量 --大家可以查询PHP手册或者相关资料
=====================================================================
数组:    --PHP的数组还是比较简单好用的。
  PHP数组可以当作其它语言中的集合使用
  PHP数组里可以存放PHP支持的任何类型。当然也可以存放 类对象等 --直接看code

复制代码 代码如下:


        /*===================================================================*/
        //数值数组
         $nums = array(1,2,3);
        //或者等同于
         $nums[0] = 1;
        $nums[1] = 2;
        $nums[2] = 4;
        echo $nums[2]."
";
        /*输出:4*/
        /*===================================================================*/
        //关联数组  --其中的“=>”是PHP中的关联符号,就是指定键值对的。
         $ns = array("name"=>"张三","age"=>22,"sex"=>"man"); 
        //或者等同于
         $ns["name"] = "张三";
        $ns["age"] = 22;
        $ns["sex"] = "man";
        echo "姓名:".$ns["name"]."
年龄:".$ns["age"]."
性别:".$ns["sex"]."
";
        /*输出:
            姓名:张三
              年龄:22
            性别:man
        */
        /*===================================================================*/
        //多维数组 --数组里面还可以存放数组
         $bs = array("张三"=>array("爱好"=>"计算机","年纪"=>"23","性别"=>"男"),"小红"=>array("爱好"=>"吃饭","性别"=>"女"));
        //调一下格式,让大家看的清楚些
         $bs = array
        (
            "张三"=>array
            (
                "爱好"=>"计算机",
                "年纪"=>"23",
                "性别"=>"男"
            ),
            "小红"=>array
            (
                "爱好"=>"吃饭",
                "性别"=>"女"
            )
        );
        //或者等同于
         $bs["小红"]["性别"] = 2; $bs["小红"]["爱好"] = 2; //....
        //或
         $bs["张三"] = array("爱好"=>"计算机","年纪"=>"23","性别"=>"男"); $bs["小红"] = array("爱好"=>"吃饭","性别"=>"女");
        echo $bs["小红"]["性别"]."
";
        /*输出:女*/
        /*===================================================================*/
    ?>


=====================================================================
 PHP运算符: --摘录w3school的教程
  
本部分列出了在 PHP 中使用的各种运算符:
算数运算符
运算符 说明 例子 结果
+ Addition x=2
x+2
4
- Subtraction x=2
5-x
3
* Multiplication x=4
x*5
20
/ Division 15/5
5/2
3
2.5
% Modulus (division remainder) 5%2
10%8
10%2
1
2
0
++ Increment x=5
x++
x=6
-- Decrement x=5
x--
x=4
赋值运算符
运算符 说明 例子
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y

比较运算符

运算符 说明 例子
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
> is greater than 5>8 returns false
is less than 5
>= is greater than or equal to 5>=8 returns false
is less than or equal to 5

逻辑运算符

运算符 说明 例子
&& and x=6
y=3

(x 1) returns true

|| or x=6
y=3

(x==5 || y==5) returns false

! not x=6
y=3

!(x==y) returns true


程序判断语句:

   和C#、java、C等判断语句一样。有if..else/else..if、switch语句 --直接看Code

复制代码 代码如下:


         $name = "张三"; //声明变量
           /*if..else只会语句只会执行其中一个,一个条件成立。就算后面的也成立,都会被忽略掉*/
         //判断名字是否是张三
          if($name == "张三")
         {
               echo "张三";
         }
         else if($name == "李四") //接着判断
          {
               echo "李四";
         }
         else //以上都不是就走进else
         {
              echo "其它";
         }
         print('
'); //打印输出
          $num = 1;
         /*
          switch选择结构 可if的原理差不多。只是在case里要加break --当然也可以不加。
            这样的话执行玩case 1后并不会跳出去,而是继续执行下一个case分支。直到遇到break才跳出去、。大家可以自己试下
          */
         switch($num)
         {
            case 1:
                echo "一";
                break;
            case 2:
                echo "二";
                break;
            default:  //默认分支。当条件都不成立的时候执行。
                echo "其它";
         }

         /*
         最终执行的结果是:
                         张三
                         一
           */
    ?>


PHP循环:

  和其它强类型的编程语言一样。php也有while、do while、for、foreach --直接看code

复制代码 代码如下:


        $index = 1;
        while($index         {
            echo "第".$index."次"."
";
            $index++; //累加
         }
        /*以上结果输出10次*/

        echo '
';
        $index = 1;
        do
        {
            echo "第".$index."次"."
";
            $index++;
        }
        while($index
        /*以上结果输出1次*/
        echo '
';
        for($index = 1;$index         {
                echo "第".$index."次"."
";
        }

        /*以上结果输出3次*/
        echo '
';
        $index = array("1","2","3");
        foreach($index as $temp) //遍历数组
         {
            echo "值:".$temp."
";
        }
        /*以上结果输出3次*/
    ?>


PHP函数:

  php函数的声明很简单,只要前面加上关键字function后面跟函数名就行了。--具体格式直接看code

复制代码 代码如下:


        /*PHP函数*/
        //无参函数
         function MyEcho()
        {
            echo "无参函数
";
        }

        //有参函数 --传入的参数也可以是类对象
         function MyEcho2($str)
        {
            echo $str;
        }

        MyEcho(); //输出:无参函数
         MyEcho2("嘻嘻哈哈!"); //输出:嘻嘻哈哈!
    ?>

PHP类:

  php也像其它高级语言一样,支持面向对象编程。在这里我说基础部分php类的声明。有关于面向对象的编程方式,大家自行研究

  php声明类的方式,也要加关键字 class  --具体看code -(其中包括静态函数。函数调用等)

复制代码 代码如下:


        class MyClass //类的声明
         {
            private $jum1; //定义私有变量
              private $jum2;
            static public $test = "测试静态方法"; //定义公有变量
              function Calc() //类函数
              {
                return $this->jum1+$this->jum2; // "->" 符号是类调用的意思
              }

            function SetNum($Num1,$Num2) //有参类函数
              {
                $this->jum1 = $Num1;
                $this->jum2 = $Num2;
                return $this; //这里要返回类对象本身
              }

            static function Tt()
            {
                echo "
".MyClass::$test."
";   
            }
        }

        /*实现计算功能*/
        $temp = new MyClass;
        echo $temp->SetNum(2,8)->Calc(); //输出:10
        MyClass::Tt(); //"::"静态调用 //输出:测试静态方法
    ?>


PHP表单处理:

  在页面用户提交值的时候用 $_GET 和 $_POST 或 $_REQUEST (它包含了$_GET、$_POST和$_COOKIE)系统定义的变量来读取提交过来的值 --看code

复制代码 代码如下:



            echo $_POST["xx"]."
";  //读取post值
         echo $_REQUEST["xx"];
        //用get读取值。自己试
    ?>
   

       
       
   



暂时就这么多了...如果有时间,我会写下PHP常用的应用。高级部分。(包括会话、cookie、面向对象、常用函数等等)
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles